jest: [Bug]: jest-config can't import TS file with moduleResolution: node16

Version

29.1.2

Steps to reproduce

  1. git clone https://github.com/Maxim-Mazurok/jest-config-bug-repro
  2. cd jest-config-bug-repro
  3. (optional) nvm i
  4. npm ci
  5. tsc --noEmit - works
  6. npm run test:unit - works
  7. npm run test:integration - doesn’t work

Expected behavior

Parse config just fine and exit with No tests found, exiting with code 0

Actual behavior

gives error:

Error: Jest: Failed to parse the TypeScript config file /home/maxim/jest-config-bug-repro/jest.integration.config.ts
  Error: Cannot find module './jest.config.js'
Require stack:
- /home/maxim/jest-config-bug-repro/jest.integration.config.ts
- /home/maxim/jest-config-bug-repro/node_modules/jest-config/build/readConfigFileAndSetRootDir.js
- /home/maxim/jest-config-bug-repro/node_modules/jest-config/build/index.js
- /home/maxim/jest-config-bug-repro/node_modules/jest-cli/build/init/index.js
- /home/maxim/jest-config-bug-repro/node_modules/jest-cli/build/cli/index.js
- /home/maxim/jest-config-bug-repro/node_modules/jest-cli/build/index.js
- /home/maxim/jest-config-bug-repro/node_modules/jest-cli/bin/jest.js
- /home/maxim/jest-config-bug-repro/node_modules/jest/bin/jest.js
    at readConfigFileAndSetRootDir (/home/maxim/jest-config-bug-repro/node_modules/jest-config/build/readConfigFileAndSetRootDir.js:136:13)
    at async readConfig (/home/maxim/jest-config-bug-repro/node_modules/jest-config/build/index.js:208:18)
    at async readConfigs (/home/maxim/jest-config-bug-repro/node_modules/jest-config/build/index.js:404:26)
    at async runCLI (/home/maxim/jest-config-bug-repro/node_modules/@jest/core/build/cli/index.js:182:59)
    at async Object.run (/home/maxim/jest-config-bug-repro/node_modules/jest-cli/build/cli/index.js:155:37)

Additional context

Had to make the switch in https://github.com/Maxim-Mazurok/google-api-typings-generator because https://github.com/sindresorhus/got/issues/2051#issuecomment-1259401339

Environment

System:
    OS: Linux 5.10 Ubuntu 22.04.1 LTS 22.04.1 LTS (Jammy Jellyfish)
    CPU: (20) x64 12th Gen Intel(R) Core(TM) i9-12900H
  Binaries:
    Node: 18.10.0 - ~/.nvm/versions/node/v18.10.0/bin/node
    npm: 8.19.2 - ~/.nvm/versions/node/v18.10.0/bin/npm
  npmPackages:
    jest: ^29.1.2 => 29.1.2

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 7
  • Comments: 36 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Excellent tip. I was using vite but didn’t know about vitest! Now “fixed”. And resolve works like a charm. Problem fixed as well. 😜

Maybe try switching to vitest, same API, even same plugins usually work, but TS and ESM stuff generally works much better with it.

Unstale

same problem here. Any workarounds?

@Maxim-Mazurok Your script in package.json is incorrect

Current

 "test:integration": "npm run jest -- --config jest.integration.config.ts",

Should be

"test:integration": "npm run jest --config jest.integration.config.ts",

No, using -- allows to pass cli arguments to npm script as per this answer, I’ve been using it for years in many projects.

You can even see this from log:

> npm run jest -- --config jest.integration.config.ts


> jest
> node --experimental-vm-modules node_modules/jest/bin/jest.js --passWithNoTests "--config" "jest.integration.config.ts"

Error: Jest: Failed to parse the TypeScript config file /home/maxim/jest-config-bug-repro/jest.integration.config.ts

Notice how it has jest.js --passWithNoTests "--config" "jest.integration.config.ts" part where config argument is getting passed.

And without -- it’s not being passed:

> npm run jest --config jest.integration.config.ts


> jest
> node --experimental-vm-modules node_modules/jest/bin/jest.js --passWithNoTests "jest.integration.config.ts"

You see jest.js --passWithNoTests "jest.integration.config.ts" - the --config disappeared.

This is an unrelated matter, perhaps you just didn’t notice that I’m not doing jest --config bla but npm run jest --config bla because I have added the jest script. In any case, let’s not focus on this as this is not the issue at hand.

One can get the same reproduction by changing jest.config.ts to be:

import { JestConfigWithTsJest } from "ts-jest";
import integrationConfig from "./jest.integration.config.js";

const config: JestConfigWithTsJest = { ...integrationConfig };

export default config;

And then run npm run jest with no arguments and get the same error:

Error: Jest: Failed to parse the TypeScript config file /home/maxim/jest-config-bug-repro/jest.config.ts
  Error: Cannot find module './jest.integration.config.js'

Hope that clears it up, cheers!

Still relevant