I’m trying to use Jest in my Express API with TypeScript. However, every time I try to execute npm run test
, Jest complains about the ESM.
I’ve tried many configurations, but none of then solved the problem. Anyone knows how can I solve it?
I installed jest
and ts-jest
. My tests are in src/__tests__/
. Here is my jest.config.js
(created using npx ts-jest config:init
):
/** @type {import('ts-jest').JestConfigWithTsJest} **/
export default {
testEnvironment: 'node',
transform: {
'^.+\.(ts|tsx)$': ['ts-jest', {useESM: true}],
},
testMatch: ['**/**/*.test.ts'],
};
When I try to execute Jest, it throws:
Import assertions are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
It happens because there is an assertion like this in app.ts
, a file that is imported in a test:
import swaggerFile from '../swagger-output.json' assert { type: 'json' };