jest: [Bug]: jest ts-node ignores provided tsconfig.json
Version
28.0.5
Steps to reproduce
Create valid jest.config.ts file wit the next content:
import { InitialOptionsTsJest } from 'ts-jest'
import path from 'path'
console.log(import.meta)
const jestOptions: InitialOptionsTsJest = {
preset: 'ts-jest/presets/default-esm',
rootDir: './some_root',
testMatch: [ '**/*.test.ts' ],
extensionsToTreatAsEsm: ['.ts'],
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1'
},
globals: {
'ts-jest': {
useESM: true,
tsconfig: path.join(process.cwd(), 'tsconfig.json')
}
}
}
export default jestOptions
Note console.log(import.meta)
Create valid tsconfig.json file:
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"allowJs": true,
"esModuleInterop": true,
"strict": true,
"skipLibCheck": true,
"declaration": true,
"allowSyntheticDefaultImports": true,
"isolatedModules": true
}
}
run npx jest
Expected behavior
No TS errors
Actual behavior
Error: Jest: Failed to parse the TypeScript config file ./some_proj/jest.config.ts
TSError: ⨯ Unable to compile TypeScript:
jest.config.ts:55:13 - error TS1343: The 'import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'es2022', 'esnext', 'system', 'node12', or 'nodenext'.
console.log(import.meta)
~~~~~~~~~~~
at readConfigFileAndSetRootDir (/some_proj/node_modules/jest-config/build/readConfigFileAndSetRootDir.js:136:13)
at async readConfig (/some_proj/node_modules/jest-config/build/index.js:216:18)
at async readConfigs (/some_proj/node_modules/jest-config/build/index.js:404:26)
at async runCLI (/some_proj/node_modules/@jest/core/build/cli/index.js:140:59)
at async Object.run (/some_proj/node_modules/jest-cli/build/cli/index.js:155:37)
Additional context
No response
Environment
System:
OS: Linux 5.17.12-100.fc34.x86_64
CPU: (8) arm64
Binaries:
Node: 16.15.0
npm: 7.20.0
npmPackages:
"jest": "28.1.0",
"ts-jest": "^28.0.5"
Stack overflow
There is a question on stackoverflow been posted 9 months ago still not answered…
TS Jest
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 11
- Comments: 38 (5 by maintainers)
So is there any workaround for this? This is the only blocker I have for migrating my code to ESM from CJS.
I don’t use it specifically in the
jest.config.ts
file, but my project does use it in theglobalSetup.ts
file which has the same issue.Hey peeps,
My teammate @macmv came up with a work around for this using jest transformers.
Feel free to use this code:
The other nifty thing about this is you could extend it to install some kind of mock object instead of
({} as any)
for example{ taco: "tuesday", pizza: "friday" }
.Enjoy!
srsly wtf
I think there’s some confusion because the
tsconfig
used to compilejest.config.ts
, before launching the test runner, is not necessarily the sametsconfig
used by the ts-jest transformer to compile the TS code under test.I don’t write my Jest config in TS – it works fine in JS with type annotation comment directives – but for those who do, is there really a use-case for consuming
import.meta
while configuring Jest (i.e., specifically injest.config.ts
)?As far as consuming
import.meta
in the code under test, I believe that should fall into the purview ofts-jest
– they punted over here specifically because you’re trying to refer to it in the config file, beforets-jest
actually does anything.ETA: actually I don’t think there is a problem on the
ts-jest
side. I just found https://github.com/kulshekhar/ts-jest/issues/3888 which links to this handy example, where the user is successfully running a test that usesimport.meta
, usingts-jest/presets/default-esm
. So, bottom line, this issue is only aboutimport.meta
injest.config.ts
, specifically, and if you want to useimport.meta
in your code and/or tests, that already works. (Again, if you are trying to useimport.meta
injest.config.ts
, I would be really curious to know why.)Yeah, this isn’t currently possible - see https://github.com/facebook/jest/issues/11453#issuecomment-1040424443. In #12397 we enforce CJS, might be we should tweak this. It’s unfortunate this requires using a loader, tho… Would love to figure out a good way for Jest to just ask
ts-node
to load the config for us without caring about what flavour of module system it’s written inIssue still present
Issue still present