Open Maxim-Mazurok opened 1 year ago
@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 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!
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.
Occurring an activity
Same problem for me. tsc and esbuild work fine, jest can't resolve the import.
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.
unstaling
same problem here. Any workarounds?
Maybe try switching to vitest, same API, even same plugins usually work, but TS and ESM stuff generally works much better with it.
Excellent tip. I was using vite but didn’t know about vitest! Now “fixed”. And resolve works like a charm. Problem fixed as well. 😜
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.
No worries! Also, unstaling the issue
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.
Unstale
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.
I've migrated to vitest and also using module resolution "bundle" so this issue is no longer relevant to me.
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.
As I mention it's no relevant to me, so I'll unstale it and unsubscribe, perhaps someone else can take it over, cheers
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.
unstale
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.
unstale
still the same
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.
Why is this being ignored?
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.
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
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.
Still relevant.
Still relevant.
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
.
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.
Still relevant
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.
Still an issue, and testing with @noahnu 's solution did not work for me..
I managed to get a consistent fix
change in your package.json "jest": "npm:vitest@latest"
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): https://github.com/jestjs/jest/pull/14739
Some help would be appreciated (with the suite, or even letting me know the fix works for you).
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
}
partly addressed in https://github.com/jestjs/jest/releases/tag/v30.0.0-alpha.5 (via #14739)
Version
29.1.2
Steps to reproduce
git clone https://github.com/Maxim-Mazurok/jest-config-bug-repro
cd jest-config-bug-repro
nvm i
npm ci
tsc --noEmit
- worksnpm run test:unit
- worksnpm run test:integration
- doesn't workExpected behavior
Parse config just fine and exit with
No tests found, exiting with code 0
Actual behavior
gives error:
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