Jest 中跨根目录的相对导入

olo*_*tus 6 typescript jestjs ts-jest

rootDirs文件的元素将tsconfig.json多个物理目录合并到一个虚拟目录中。

这适用于相对导入,因此具有以下文件/文件夹结构

  • src/
    • main/
      • file.ts
    • test/
      • file.test.ts

以及以下内容tsconfig.json

{
    "compilerOptions": {
        "rootDirs": [
            "src/main",
            "src/test"
        ],
        "moduleResolution": "node"
    }
}
Run Code Online (Sandbox Code Playgroud)

并进行以下导入file.test.ts

import { something } from "./file"
Run Code Online (Sandbox Code Playgroud)

导入会发现file.ts

但是,如果我file.test.ts在 Jest 中运行,它会失败并出现以下错误

Cannot find module './file' from 'test/file.test.ts'
Run Code Online (Sandbox Code Playgroud)

Jest / ts-jest 是否支持类似于 TypescriptrootDirs元素的东西?我尝试了该roots元素,但它没有解决问题。