Relative imports across root directories in Jest

Viewed 110

The rootDirs element of a tsconfig.json file merges multiple physical directories into a single virtual directory.

This works for relative imports, so with the following file / folder structure

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

and the following tsconfig.json

{
    "compilerOptions": {
        "rootDirs": [
            "src/main",
            "src/test"
        ],
        "moduleResolution": "node"
    }
}

and with the following import in file.test.ts

import { something } from "./file"

the import will find file.ts.

If I run file.test.ts in Jest, however, it fails with the following error

Cannot find module './file' from 'test/file.test.ts'

Does Jest / ts-jest support something similar to the Typescript rootDirs element ? I tried the roots element, but it did not fix the issue.

0 Answers
Related