Closed zaininfo closed 1 week ago
Would love to get a real solution. For now I suppress the import assertion error using:
// @ts-ignore, because when setting node module resolution to Node16, tsc raises an error for the import assertion.
import configSchema from "./config-schema.json" assert { type: "json" };
any update on this issue? Stumbling on it today 🙃
Try putting
"extensionsToTreatAsEsm": [".ts"],
"globals": {
"ts-jest": {
"useESM": true
}
}
into your jest.config, and set the NODE_OPTIONS=--experimental-vm-modules
environment variable, or use node --experimental-vm-modules
.
Also see https://github.com/kulshekhar/ts-jest/issues/3525 and https://jestjs.io/docs/ecmascript-modules
(Or use vitest instead of jest https://vitest.dev/guide/migration.html#migrating-from-jest, which works for me without configuring anything at all)
@sezanzeb is right. This feature only exists when using Jest in ESM mode. You can check documentation how to do it at https://kulshekhar.github.io/ts-jest/docs/guides/esm-support
Version
29.1.1
Steps to reproduce
With the following TS config:
And, the following Jest config:
A file
index.ts
which imports a JSON file with a type assertion e.g.when imported in a Jest test file e.g.
gives an error.
Expected behavior
Test passes.
Actual behavior
Test fails with a TS error:
Debug log
Additional context
This error happens regardless of whether the Jest config is provided as JSON, JS or TS file.
Also, TS doesn't throw this error when transpiling
index.ts
file. In fact, TS throws this error if the value ofmodule
is changed fromesnext
. So, the value ofmodule
is indeed correct, but somehow TS Jest doesn't get it.On a related note, changing the value of
module
to some invalid value triggers a different error about the value being invalid:So, it looks like TS Jest is overriding the
esnext
value.Environment