13

This is a demo code below used to demonstrate failure i am getting in my current project.

It tanspiles and runs, just fails to compile with jest with following error

Error: error TS1343: The 'import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'es2022', 'esnext', 'system', 'node16', or 'nodenext'.

Files: src/calc.ts

import { fileURLToPath } from 'url'; import path from 'path'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); export function add(x: number, y: number): number { return x + y; } export function mul(x: number, y: number): number { return x * y; } 

jest.config.cjs:

 module.exports = { preset: 'ts-jest', testEnvironment: 'node', }; 

tsconfig.json:

 { "include": [ "./src/**/*" ], "exclude": [ "node_modules" ], "compilerOptions": { "target": "ES2022", "module": "NodeNext", "outDir": "./out", "rootDir": "./src", "strict": true, "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true } } 

package.json

 "devDependencies": { "@types/jest": "^26.0.24", "jest": "^26.6.3", "ts-jest": "^26.5.6", "typescript": "^4.7.4" } 

Not sure if i am doing anything incorrect here

3
  • 4
    I have the same case as yours. Did you find a solution? Commented Oct 17, 2022 at 7:13
  • I'm wondering the same Commented Jul 7, 2023 at 19:54
  • --module is not a jest CLI option? Commented Jan 24, 2024 at 22:55

4 Answers 4

5

Assuming we're talking about running the tests with test, I found the reason for the error, but I have no idea how to get around it.

Your "module": "NodeNext" option is ignored, because jest registers ts-node with "module": "CommonJS"

/// packages/jest-config/src/readConfigFileAndSetRootDir.ts // Register TypeScript compiler instance const tsNode = await import('ts-node'); return tsNode.register({ compilerOptions: { module: 'CommonJS', }, moduleTypes: { '**': 'cjs', }, }); 

See jest source code

This is why the error mentions nodenext being a valid choice even though you actually used it in your tsconfig.json.

Sign up to request clarification or add additional context in comments.

2 Comments

I assume there is a cli option to ovverride ts-config registration not sure on it though..
Not sure about that. What I eventually did was switch to swc as the transpiler for jest (kept tsc for the rest of the project, for now). Worked out of the box
0

I decided to switch to bun personally

2 Comments

and this problem didn't occur when you switched?
Sorry my answer wasn't clear. Yes it fixed the issue since the test could run perfectly without any useless headache
0

This may not be the answer that solves the above problem, but instead of jest runner i used Node inbuilt test runner LTS 20 onward (not everyone will have this luxury) which closed this for me, its a cleaner way to run tests and using tsc to transpile code and test both.

Comments

0

I'm using ts-jest to run my test.

I had to update the jest.config.js to jest.config.ts

I had to update the new jest.config.ts file to be an ESM module like so

import type { JestConfigWithTsJest } from 'ts-jest' const jestConfig : JestConfigWithTsJest = { bail: true, verbose: true, collectCoverage: true, testTimeout: 10000, collectCoverageFrom: ['src/**/*.ts'], coveragePathIgnorePatterns: ['\\.d\\.ts$'], modulePathIgnorePatterns: [ 'node_modules/', 'dist/' ], preset: 'ts-jest/presets/default-esm', testEnvironment: 'node', } export default jestConfig 

I also had to update the preset property from

preset: 'ts-node' 

to

preset: 'ts-jest/presets/default-esm' 

please look at the preset to see whatever fits the configuration of what you are trying to do https://kulshekhar.github.io/ts-jest/docs/getting-started/presets

good luck.

1 Comment

I would like to use this solution, as it looks the cleanest to me, but my compiler is struggling with this message: Failed to parse the TypeScript config file jest.config.ts SyntaxError: Unexpected token 'export' - how do you call the tests? npx jest ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.