You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React TypeScript项目无IDE编译错误但Jest测试报语法错误

Fixing the async abstract Syntax Error in Jest Tests

The error you're seeing occurs because the Babel parser used by your current react-scripts version doesn't recognize the combination of async and abstract keywords in class methods—even though this is valid TypeScript. Here are two straightforward fixes:

Abstract methods only define a method signature, not an implementation. The async keyword is redundant here because the return type (Promise<Block> / Promise<void>) already indicates that the implemented method should return a promise. You can add async when you implement the method in your concrete class instead.

Here's the corrected ChromeVolume.ts code:

import Keys from "./Keys";
import Volume from "./Volume"
import Block from './Block'
import EventHandler from "../utility/EventHandler"

abstract class ChromeVolume implements Volume {
  readonly error: EventHandler<Volume, Error> = new EventHandler<Volume, Error>(this);

  abstract load<T extends Keys>(keys: T): Promise<Block>
  abstract save<T extends object>(items: T): Promise<void>
  abstract clear(): Promise<void>
}

export default ChromeVolume

When you implement these methods in a concrete class, you can still use async as needed:

class ConcreteVolume extends ChromeVolume {
  async load<T extends Keys>(keys: T): Promise<Block> {
    // Your async implementation here
    return someAsyncOperation(keys);
  }

  // ... other implementations
}

Solution 2: Update react-scripts

If you prefer to keep the async keyword in your abstract methods, update react-scripts to a version that uses a Babel parser supporting this syntax. Versions of react-scripts v5.0.0 and later include Babel 7.10+, which handles async abstract methods correctly.

Run this command to update:

npm install react-scripts@latest
# or
yarn add react-scripts@latest

Either solution will resolve the syntax error in your Jest tests.

内容的提问来源于stack exchange,提问作者Daniel Douglas Soyka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:49:09