我正在尝试使用 Jest 运行测试,目前我在 React 应用程序中使用 jsx 和 tsx(从 js 更改为 ts),但是当我运行测试时,除了 tsx 中具有可选阻塞的测试之外,所有 jsx 测试都成功。我的输入总是出现错误
Unexpected token
,看起来像
...
<Input
value={parent?.child}
...
这是我的笑话配置
{
"verbose": true,
"roots": [
"<rootDir>/app"
],
"setupFiles": [
"./config/webpack/jest/config.js"
],
"moduleNameMapper": {
"^components(.*)": "<rootDir>/app/javascript/components$1",
"^utils(.*)": "<rootDir>/app/javascript/utils$1",
"^types(.*)": "<rootDir>/app/javascript/types$1",
"\\.(css|pcss|scss)$": "<rootDir>/spec/__mocks__/styleMock.js",
"\\.(gif|ttf|eot|svg|png)$": "<rootDir>/spec/__mocks__/fileMock.js"
},
"snapshotSerializers": [
"<rootDir>/node_modules/enzyme-to-json/serializer"
],
"testRegex": "(/__tests__/.*|\\.(test))\\.(ts|tsx|js|jsx)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx"
],
"transform": {
"\\.[jt]sx?$": "babel-jest"
}
}
我如何使用可选链和玩笑可能会出现什么问题?
对我来说,问题是在我的
tsconfig.json
中,我使用的是"lib": ["ES2020"]
,这与我的节点版本冲突。将节点版本更改为 v14 解决了该问题。https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping
tsconfig.json
更改
"lib": ["ES2020"], "target": "ES2020"
,也对我有用。谢