-
Notifications
You must be signed in to change notification settings - Fork 459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: Using TS + ESM in globalSetup does not pick up correct tsconfig.json module #3328
Comments
I think I'm experiencing the issue but from another angle. I'm using project references within my project, and the actual tests are able import modules with "absolute" paths (not So yeah, it seems like the compiler options are being ignored when running the global setup. My setup is as follows. I have two "projects" -- // src/tsconfig.json
{
"extends": "../tsconfig-base.json",
"compilerOptions": {
"composite": true
}
} // test/jsconfig.json
{
"extends": "../tsconfig-base.json",
"references": [
{ "path": "../src" }
]
} Base config: // tsconfig-base.json
{
"compilerOptions": {
"outDir": "out/compiled",
"sourceMap": true,
"esModuleInterop": true,
"lib": [
"es2020",
"dom"
],
"module": "commonjs",
"target": "es2020",
"strict": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"rootDir": ".",
"baseUrl": ".",
"declaration": true,
"declarationMap": true,
"paths": {
"src/*": ["src/*"],
"test/*": ["test/*"]
}
},
"exclude": [
"node_modules"
]
} And root config: // tsconfig.json
{
"files": [],
"include": [],
"references": [
{ "path": "./src" },
{ "path": "./test" }
]
} Then, I have multiple testing modules (unit, integration, etc) with // test/unit/jest.config.js
const base = require('../../jest.config.js');
module.exports = {
...base,
testMatch: [
'<rootDir>/test/unit/**/*.test.ts'
],
}; While the base // fix resolution of config/*.json in tests run from IntelliJ
process.env.NODE_CONFIG_DIR = `${__dirname}/config`;
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
rootDir: __dirname,
testMatch: [
"<rootDir>/test/**/*.test.ts"
],
modulePathIgnorePatterns: ["<rootDir>/out/"],
moduleNameMapper: {
"^src/(.*)$": "<rootDir>/src/$1",
"^test/(.*)$": "<rootDir>/test/$1"
},
globals: {
"ts-jest": {
tsconfig: "<rootDir>/test/tsconfig.json"
}
},
globalSetup: "<rootDir>/test/utils/global-setup.ts"
}; |
I spent some time during the weekend to look into this issue, and I think I have finally come up with an explanation and a possible workaround (at least for my use case): ExplanationThe correct However, before performing the transformation, the Therefore, I think we can conclude that this is an issue with WorkaroundBy using With the following config setup,
|
I believe I also encountered this issue. I also found that some compiler options seems randomly affects the result.
|
Now this bug is solved in Jest 28 without the need of babel. |
Version
27.1.3
Steps to reproduce
Clone my repo at https://github.com/santi/jest-ts-esm-issue
Tested with NodeJS v.17+
Expected behavior
When setting
{"compilerOptions": { "module": "es2022" }}
intsconfig.json
, I expect that the tests and globalSetup function will run with the givenmodule
option while parsing the TS files.Actual behavior
It seems like the correct
module
option is used when running tests, as theimport.meta.url
reference is resolved correctly in the sample test file. However, when registering theglobalSetup.ts
hook injest.config.ts
, an error is thrown, claiming that TypeScript is running with anothermodule
setting (I assume the default ES5, but have not been able to confirm this):See Debug log below for full stack trace.
Debug log
Additional context
The same error occurs with
module: es2020|esnext|nodenext|etc...
. I am a bit unsure if this is an error withts-jest
orjest
in general. I've tested with both latest stable Jest (27.5.1) and latest alpha Jest (28.0.0-alpha.7). If this is an issue with Jest, feel free to tell me, and I'll open an issue there instead.Environment
The text was updated successfully, but these errors were encountered: