-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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]: jest ts-node ignores provided tsconfig.json #12952
Comments
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
Issue still present |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
Issue still present |
Yeah, this isn't currently possible - see #11453 (comment). In #12397 we enforce CJS, might be we should tweak this. It's unfortunate this requires using a loader, tho... Would love to figure out a good way for Jest to just ask |
So is there any workaround for this? This is the only blocker I have for migrating my code to ESM from CJS. |
Any updates about this issue? Same error while using |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
Bump |
Well this puts us in a bind... |
Any possible solution? If there's any, I can help with. |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
Issue still present |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
still happens |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
still happens |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
Still relevant... |
Issue persists... |
still happens |
1 similar comment
still happens |
srsly wtf |
still persist |
Hey peeps, My teammate @macmv came up with a work around for this using jest transformers. Feel free to use this code: const tsJest = require("ts-jest").default;
module.exports = {
createTransformer(config) {
const transformer = tsJest.createTransformer(config);
const tsProcess = transformer.process;
transformer.process = (sourceText, ...rest) => {
sourceText = sourceText.replaceAll("import.meta.env", "({} as any)");
return tsProcess(sourceText, ...rest);
};
return transformer;
},
}; The other nifty thing about this is you could extend it to install some kind of mock object instead of Enjoy! |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
Bump. Not stale. Ffs. |
For Yarn PnP ESM projects, I managed to workaround by using my own ts-node register instead. Loaders is working well for Node 18.6+ If your project is on Node 18.6+, you can see my diff --git a/build/readConfigFileAndSetRootDir.js b/build/readConfigFileAndSetRootDir.js
index e989961ba71bbea43eb293ebdc7739f00f6e85e7..44429f504f568cb5cef2998f527e0a8837d47338 100644
--- a/build/readConfigFileAndSetRootDir.js
+++ b/build/readConfigFileAndSetRootDir.js
@@ -11,6 +11,13 @@ function path() {
};
return data;
}
+function url() {
+ const data = _interopRequireWildcard(require('url'));
+ url = function () {
+ return data;
+ };
+ return data;
+}
function fs() {
const data = _interopRequireWildcard(require('graceful-fs'));
fs = function () {
@@ -101,7 +108,11 @@ async function readConfigFileAndSetRootDir(configPath) {
let configObject;
try {
if (isTS) {
- configObject = await loadTSConfigFile(configPath);
+ const configModule = await import(path().isAbsolute(configPath)
+ ? url().pathToFileURL(configPath).href
+ : configPath
+ );
+ configObject = configModule.default;
} else if (isJSON) {
const fileContent = fs().readFileSync(configPath, 'utf8');
configObject = (0, _parseJson().default)(
Then using yarn node --loader ts-node/esm $(yarn bin jest) # or $(yarn bin test-storybook) # `file://` for resolving windows file paths This should be able to read your project's Update: I migrated the |
This seems to be rather a limitation in jest. I'm trying to port my mocha-based project to jest as I had thought it might have fewer issues with TypeScript and modules; but apparently not. Sometimes I wonder if anyone uses typescript and ECMAScript modules for anything as nothing seems to work; I've been on a multi-hour Google/SO/Github issue journey and nobody seems to have solved this |
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
this is the best solution I found |
#15190 allows you to disable typechecking and overriding all other options (i.e., you can add Unfortunately, native ESM doesn't work with the transpilation, so you'll get an error about the
I don't think programmatic registration of |
Version
28.0.5
Steps to reproduce
Create valid jest.config.ts file wit the next content:
Note console.log(import.meta)
Create valid tsconfig.json file:
run npx jest
Expected behavior
No TS errors
Actual behavior
Additional context
No response
Environment
Stack overflow
There is a question on stackoverflow been posted 9 months ago still not answered...
TS Jest
issue on git
The text was updated successfully, but these errors were encountered: