jest icon indicating copy to clipboard operation
jest copied to clipboard

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

Open Maxim-Mazurok opened this issue 2 years ago • 36 comments

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

Maxim-Mazurok avatar Oct 01 '22 05:10 Maxim-Mazurok

@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",

sindrisig avatar Oct 04 '22 17:10 sindrisig

@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!

Maxim-Mazurok avatar Oct 04 '22 23:10 Maxim-Mazurok

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Nov 03 '22 23:11 github-actions[bot]

Occurring an activity

Maxim-Mazurok avatar Nov 04 '22 00:11 Maxim-Mazurok

Same problem for me. tsc and esbuild work fine, jest can't resolve the import.

dsellarsnr avatar Nov 20 '22 15:11 dsellarsnr

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Dec 20 '22 15:12 github-actions[bot]

unstaling

Maxim-Mazurok avatar Dec 20 '22 21:12 Maxim-Mazurok

same problem here. Any workarounds?

Vithanco avatar Dec 29 '22 16:12 Vithanco

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

Maxim-Mazurok avatar Dec 29 '22 21:12 Maxim-Mazurok

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

Vithanco avatar Dec 30 '22 09:12 Vithanco

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Jan 29 '23 09:01 github-actions[bot]

No worries! Also, unstaling the issue

Maxim-Mazurok avatar Feb 12 '23 00:02 Maxim-Mazurok

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Mar 14 '23 00:03 github-actions[bot]

Unstale

Maxim-Mazurok avatar Mar 14 '23 01:03 Maxim-Mazurok

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Apr 13 '23 01:04 github-actions[bot]

I've migrated to vitest and also using module resolution "bundle" so this issue is no longer relevant to me.

Maxim-Mazurok avatar Apr 13 '23 05:04 Maxim-Mazurok

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar May 13 '23 06:05 github-actions[bot]

As I mention it's no relevant to me, so I'll unstale it and unsubscribe, perhaps someone else can take it over, cheers

Maxim-Mazurok avatar May 14 '23 05:05 Maxim-Mazurok

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Jun 13 '23 06:06 github-actions[bot]

unstale

yifanwww avatar Jun 13 '23 15:06 yifanwww

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Jul 13 '23 16:07 github-actions[bot]

unstale

yifanwww avatar Jul 13 '23 17:07 yifanwww

still the same

ddeltree avatar Jul 23 '23 21:07 ddeltree

I looked at this issue again and found that this was not the case I was having.

For this issue, I think you probably need to compile TS jest config (jest.config.ts and jest.integration.config.ts) to JS jest config (jest.config.js and jest.integration.config.js) and then pass the js file to --config in the command.

I don't think jest does any transformations on the config files.

yifanwww avatar Aug 20 '23 07:08 yifanwww

Why is this being ignored?

datner avatar Aug 22 '23 11:08 datner

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Sep 21 '23 12:09 github-actions[bot]

This is still an issue. @SimenB Is there a label that can be added so this doesn't get marked as stale? (it's adding unnecessary "bump" comments that's making it hard to follow along for updates)

It looks like compilerOptions.module is hardcoded here: https://github.com/jestjs/jest/blob/f9398b12dfc235695e9a055d27a8f77936d07b01/packages/jest-config/src/readConfigFileAndSetRootDir.ts#L118

a quick fix would be to update jest-config to also set moduleResolution. Alternatively, respecting any existing tsconfig for the project would be nice.


As a workaround, you can create a second tsconfig file for test, e.g. tsconfig.test.json:

{
    "extends": "./tsconfig.json",
    "compilerOptions": {
        "moduleResolution": "Node10",
        "module": "CommonJS",
    }
}

and then when running jest (I'm using yarn):

 TS_NODE_PROJECT=./tsconfig.test.json yarn jest --config jest.config.ts

noahnu avatar Sep 27 '23 15:09 noahnu

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Oct 27 '23 16:10 github-actions[bot]

Still relevant.

noahnu avatar Oct 27 '23 16:10 noahnu

Still relevant.

yc-w-cn avatar Nov 22 '23 16:11 yc-w-cn