Lau
Lau

Reputation: 580

Jest with TS and ES2022 & NodeNext Fails

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

Upvotes: 12

Views: 3399

Answers (4)

McFrank
McFrank

Reputation: 351

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.

Upvotes: 0

Lau
Lau

Reputation: 580

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.

Upvotes: 0

David Guerin
David Guerin

Reputation: 9

I decided to switch to bun personally

Upvotes: 0

Andrew
Andrew

Reputation: 133

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.

Upvotes: 5

Related Questions