尝试将 Jest 与 Typescript 结合使用时,tsconfig.json 文件出现问题

pau*_*krg 5 typescript jestjs

我收到的错误消息是:

File 'd:/Project/server/jest.config.ts' is not under 'rootDir' 
'd:/Project/server/src'. 'rootDir' is expected to contain all source files.
The file is in the program because:
Matched by include pattern '**/*' in 'd:/Project/server/tsconfig.json'
Run Code Online (Sandbox Code Playgroud)

我的 tsconfig.json 文件是:

{
 "compilerOptions": {
    "target": "es2020",
    "module": "commonjs",

    "rootDir": "./src",
    "outDir": "./dist",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  }
}
Run Code Online (Sandbox Code Playgroud)

我的 jest.config.ts 文件是:

export default {
    clearMocks: true,
    collectCoverage: true,
    coverageDirectory: "coverage",
    coverageProvider: "v8",
    transform: {
        "^.+\\.(t|j)sx?$": ["@swc/jest"],
    },
};
Run Code Online (Sandbox Code Playgroud)

我试图完全遵循每个文档的指示。

我在这里做错了什么?

小智 1

您必须将jest.config.ts文件移动到目录中src/才能工作。我想说这个错误是不言自明的。