可选链接在我尝试将 tsConfig 目标设置为 ESNext、ES2021 或 ES2020 时产生问题。 但当我将目标设置为 ES2019 时,效果很好。 不确定我做错了什么。
下面是我的 Jest 配置的外观:
{
...,
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
},
globals: {
'ts-jest': {
tsConfig: {
target: 'ES2019',
},
},
}
最佳答案
This is the link for the comment which portrays the complete issue
Jest 使用 JS DOM 渲染组件并在其中运行测试用例。
JS DOM 内部使用 Node JS 来执行上述过程。
ES2019 之后,条件链默认添加到 ECMAScript,因此它不会转换为通常的三元条件。但 Node JS 默认还不支持条件链。因此,在使用 ES2019 作为目标时,它会在 React App 和 Jest 上进行转换并工作,并且在 ES2019 之后设置目标时,jest 会抛出条件链接错误,因为它不会被 Node js 识别
关于node.js - 如果 tsConfig 目标为 ESNext、ES2021 或 ES2020,ts-jest 可选链接不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69010201/