the*_*bit 24 reactjs jestjs next.js react-testing-library ts-jest
由于某种原因,我的自定义 tsconfig 文件没有在 jest.config.ts 中被选取。这是我的 jest.config.ts:
module.exports = {
setupFilesAfterEnv: [`./jest.setup.ts`],
testEnvironment: `jsdom`,
roots: [
`<rootDir>/test`
],
testMatch: [
`**/__tests__/**/*.+(ts|tsx|js)`,
`**/?(*.)+(spec|test).+(ts|tsx|js)`
],
transform: {
"^.+\\.(ts|tsx)$": `ts-jest`
},
globals: {
"ts-jest": {
tsConfig: `tsconfig.jest.json`
}
}
}
Run Code Online (Sandbox Code Playgroud)
我知道该配置的其他部分正在应用。例如,如果我删除全局变量上面的键,我的测试就不会运行。另外,我知道 tsconfig.jest.json 文件中指定的更改是修复我的测试所必需的更改,因为如果我在主 tsconfig.json 文件中进行相同的更改,我的测试运行良好。
我还尝试将所需的 tsconfig 编译器选项直接放入 jest.config.ts 文件中,但这似乎也不起作用:
module.exports = {
setupFilesAfterEnv: [`./jest.setup.ts`],
testEnvironment: `jsdom`,
roots: [
`<rootDir>/test`
],
testMatch: [
`**/__tests__/**/*.+(ts|tsx|js)`,
`**/?(*.)+(spec|test).+(ts|tsx|js)`
],
transform: {
"^.+\\.(ts|tsx)$": `ts-jest`
},
globals: {
"ts-jest": {
tsConfig: {
jsx: `react`
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Dus*_*etz 39
jest@29(2022 年 8 月发布)和 ts-jest@29(2022 年 9 月发布)的更新答案
将 Jest-and-friends 升级到版本 29 后,我所有与 React 组件相关的测试都被破坏了。我还收到了现已弃用的*.tsx错误消息。jestConfig.globals['ts-jest']
tsconfig而不是驼峰式tsConfig。jestConfig.transform['regex_match_files'].我的项目配置的相关部分如下。(警告:请注意,我为配置文件使用自定义位置,即./config/项目根目录中的专用目录,但指定配置文件的相对路径的原则保持不变)。
// ./package.json
// (trimmed to just the relevant parts)
"scripts": {
"test": "jest --config=./config/.jestrc.js --rootDir=./src/"
},
"devDependencies": {
"@types/jest": "^29.0.1",
"jest": "^29.0.3",
"jest-environment-jsdom": "^29.0.3",
"ts-jest": "^29.0.0",
"typescript": "^4.8.3"
}
Run Code Online (Sandbox Code Playgroud)
// ./config/.jestrc.js
// (trimmed to just the relevant parts)
transform: {
'^.+\\.tsx?$': [
'ts-jest',
// required due to custom location of tsconfig.json configuration file
// https://kulshekhar.github.io/ts-jest/docs/getting-started/options/tsconfig
{tsconfig: './config/tsconfig.json'},
],
},
Run Code Online (Sandbox Code Playgroud)
the*_*bit 16
正确的键tsconfig不是tsConfig。
| 归档时间: |
|
| 查看次数: |
22377 次 |
| 最近记录: |