Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Maxim-Mazurok opened this issue Oct 1, 2022 · 39 comments
Open

Comments

@Maxim-Mazurok
Copy link
Contributor

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 sindresorhus/got#2051 (comment)

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
@sindrisig
Copy link

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

@Maxim-Mazurok
Copy link
Contributor Author

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

@github-actions
Copy link

github-actions bot commented Nov 3, 2022

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 github-actions bot added the Stale label Nov 3, 2022
@Maxim-Mazurok
Copy link
Contributor Author

Occurring an activity

@github-actions github-actions bot removed the Stale label Nov 4, 2022
@dsellarsnr
Copy link

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

@github-actions
Copy link

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 github-actions bot added the Stale label Dec 20, 2022
@Maxim-Mazurok
Copy link
Contributor Author

unstaling

@github-actions github-actions bot removed the Stale label Dec 20, 2022
@Vithanco
Copy link

same problem here. Any workarounds?

@Maxim-Mazurok
Copy link
Contributor Author

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

@Vithanco
Copy link

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

@github-actions
Copy link

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 github-actions bot added the Stale label Jan 29, 2023
@Maxim-Mazurok
Copy link
Contributor Author

No worries! Also, unstaling the issue

@github-actions github-actions bot removed the Stale label Feb 12, 2023
@github-actions
Copy link

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 github-actions bot added the Stale label Mar 14, 2023
@Maxim-Mazurok
Copy link
Contributor Author

Unstale

@github-actions
Copy link

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 github-actions bot added the Stale label Apr 13, 2023
@Maxim-Mazurok
Copy link
Contributor Author

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

@github-actions github-actions bot removed the Stale label Apr 13, 2023
@github-actions
Copy link

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.

@ddeltree
Copy link

still the same

ddeltree added a commit to ddeltree/leavify that referenced this issue Jul 23, 2023
jestjs/jest#13350;

also, file renaming and package metadata
@yifanwww
Copy link

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.

@datner
Copy link

datner commented Aug 22, 2023

Why is this being ignored?

@github-actions
Copy link

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 github-actions bot added the Stale label Sep 21, 2023
@noahnu
Copy link

noahnu commented Sep 27, 2023

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:

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

@github-actions
Copy link

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 github-actions bot added the Stale label Oct 27, 2023
@noahnu
Copy link

noahnu commented Oct 27, 2023

Still relevant.

@github-actions github-actions bot removed the Stale label Oct 27, 2023
@yc-w-cn
Copy link

yc-w-cn commented Nov 22, 2023

Still relevant.

@JamesKyburz
Copy link

Just ran into this, wondering how jest is used with modern typescript projects using moduleResolution node16?

Overriding module resolution for tests would mean you get errors from the base tsconfig.json in your editor, or when validating types using for example tsc --noEmit.

Copy link

github-actions bot commented Jan 5, 2024

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 github-actions bot added the Stale label Jan 5, 2024
@noahnu
Copy link

noahnu commented Jan 5, 2024

Still relevant

@github-actions github-actions bot removed the Stale label Jan 5, 2024
@JamesKyburz
Copy link

For ESM support in Node.js to work without using extra bundlers or compilers module resolution has to be node16. Otherwise the output generated by tsc --build isn't compatible with Node, importing files without extensions just doesn't work without a non standard loader.

If we for example want https://swc.rs/ to generate ESM code we have to use node16 resolution, which basically means swc + jest won't work.

@phun-ky
Copy link

phun-ky commented Feb 1, 2024

Still an issue, and testing with @noahnu 's solution did not work for me..

@datner

This comment was marked as spam.

@SimenB SimenB added the Pinned label Feb 6, 2024
@akwodkiewicz
Copy link
Contributor

akwodkiewicz commented May 2, 2024

I did not know about this issue and I have submitted a duplicate here half a year ago: #14740

I was able to propose a solution to this issue back then, but the PR is still pending (need help with a test suite or make the decision to ship it without one): #14739

Some help would be appreciated (with the suite, or even letting me know the fix works for you).

@akwodkiewicz
Copy link
Contributor

My PR is about the TS config file with modern moduleResolution, but it does not solve the issue with .js imports inside the config.

However, I wanted to suggest a workaround that requires you to add this to the tsconfig.json in your project:

 "ts-node": {
    "experimentalResolver": true
  }

@SimenB
Copy link
Member

SimenB commented May 30, 2024

partly addressed in https://github.com/jestjs/jest/releases/tag/v30.0.0-alpha.5 (via #14739)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests