使用 jest 运行测试时的可选链接问题

问题描述 投票:0回答:3

我正在尝试使用 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"
    }
}

我如何使用可选链和玩笑可能会出现什么问题?

reactjs jestjs babeljs enzyme babel-jest
3个回答
9
投票

对我来说,问题是在我的

tsconfig.json
中,我使用的是
"lib": ["ES2020"]
,这与我的节点版本冲突。将节点版本更改为 v14 解决了该问题。
在这里您将找到带有节点版本的 lib 目标的链接:

https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping


7
投票
使用

tsconfig.json

 更改 
"lib": ["ES2020"], "target": "ES2020"
,也对我有用。谢


0
投票
代替这个“lib”:[“dom”,“dom.iterable”,“esnext”],“target”:“ES2017”, 我添加了这个“lib”:[“ES2020”],“target”:“ES2020”这对我有用

© www.soinside.com 2019 - 2024. All rights reserved.