我找到了解决办法。
我最初的项目结构是:
.
|--src
| |--service.ts
|--tests
| |--service.test.ts
|--dist
|--tsconfig.json
|--package.json
package.json 中的 Jest 配置:
"jest": {
"globals": {
"ts-jest": {
"tsconfig": "tsconfig.json"
}
},
"moduleFileExtensions": ["ts","js"],
"transform": {
"^.+\\.(ts|tsx)$": "./node_modules/ts-jest/preprocessor.js"
},
"testMatch": [
"**/tests/**/*.test.(ts|js)"
],
"testEnvironment": "node"
}
使用此配置ts-jest 在内存中执行 .ts 文件的转译。
我在compilerOptions 的tsconfig.json 中有outDir 选项,但在outDir 中没有写任何内容,因为我不能使用转译的jest-config.js
然后我在 src 中移动测试文件夹并更改 Jest 配置。
项目的新结构是:
.
|--src
| |--service.ts
| |--tests
| |--service.test.ts
| |--jest-config.ts
|--dist
|--tsconfig.json
|--package.json
新的 Jest 配置是:
"jest": {
"globalSetup": "./dist/tests/jest-config.js",
"moduleFileExtensions": ["ts", "js", "json"],
"testMatch": [
"**/dist/**/*.test.js"
],
"testEnvironment": "node"
}
现在我可以在 Jest 中使用 jest-config.js 进行全局设置。
加法