Skip to content
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

Open
CyberCookie opened this issue Jun 20, 2022 · 39 comments
Open

[Bug]: jest ts-node ignores provided tsconfig.json #12952

CyberCookie opened this issue Jun 20, 2022 · 39 comments

Comments

@CyberCookie
Copy link

CyberCookie commented Jun 20, 2022

Version

28.0.5

Steps to reproduce

Create valid jest.config.ts file wit the next content:

import { InitialOptionsTsJest } from 'ts-jest'
import path from 'path'

console.log(import.meta)

const jestOptions: InitialOptionsTsJest = {
    preset: 'ts-jest/presets/default-esm',
    rootDir: './some_root',
    testMatch: [ '**/*.test.ts' ],
    extensionsToTreatAsEsm: ['.ts'],
    moduleNameMapper: {
        '^(\\.{1,2}/.*)\\.js$': '$1'
    },
    globals: {
        'ts-jest': {
            useESM: true,
            tsconfig: path.join(process.cwd(), 'tsconfig.json')
        }
    }
}

export default jestOptions

Note console.log(import.meta)

Create valid tsconfig.json file:

{
    "compilerOptions": {
	"target": "esnext",
        "module": "esnext",
	"moduleResolution": "node",
        "allowJs": true,
	"esModuleInterop": true,
        "strict": true,
	"skipLibCheck": true,
        "declaration": true,
        "allowSyntheticDefaultImports": true,
        "isolatedModules": true
    }
}

run npx jest

Expected behavior

No TS errors

Actual behavior

Error: Jest: Failed to parse the TypeScript config file ./some_proj/jest.config.ts
  TSError: ⨯ Unable to compile TypeScript:
jest.config.ts:55:13 - error TS1343: The 'import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'es2022', 'esnext', 'system', 'node12', or 'nodenext'.

console.log(import.meta)
               ~~~~~~~~~~~

    at readConfigFileAndSetRootDir (/some_proj/node_modules/jest-config/build/readConfigFileAndSetRootDir.js:136:13)
    at async readConfig (/some_proj/node_modules/jest-config/build/index.js:216:18)
    at async readConfigs (/some_proj/node_modules/jest-config/build/index.js:404:26)
    at async runCLI (/some_proj/node_modules/@jest/core/build/cli/index.js:140:59)
    at async Object.run (/some_proj/node_modules/jest-cli/build/cli/index.js:155:37)

Additional context

No response

Environment

System:
  OS: Linux 5.17.12-100.fc34.x86_64
  CPU: (8) arm64

Binaries:
  Node: 16.15.0
  npm: 7.20.0

npmPackages:
  "jest": "28.1.0",
  "ts-jest": "^28.0.5"

Stack overflow

There is a question on stackoverflow been posted 9 months ago still not answered...

TS Jest

issue on git

@CyberCookie CyberCookie changed the title [Bug]: [Bug]: ts-jest ignores provided tsconfig.json Jun 20, 2022
@CyberCookie CyberCookie changed the title [Bug]: ts-jest ignores provided tsconfig.json [Bug]: jest ts-node ignores provided tsconfig.json Jun 20, 2022
@github-actions
Copy link

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.

@github-actions github-actions bot added the Stale label Jul 20, 2022
@Torsitano
Copy link

Issue still present

@github-actions github-actions bot removed the Stale label Jul 20, 2022
@github-actions
Copy link

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.

@github-actions github-actions bot added the Stale label Aug 19, 2022
@Torsitano
Copy link

Issue still present

@github-actions github-actions bot removed the Stale label Aug 19, 2022
@SimenB
Copy link
Member

SimenB commented Aug 22, 2022

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 ts-node to load the config for us without caring about what flavour of module system it's written in

@Abhi347
Copy link
Contributor

Abhi347 commented Aug 30, 2022

So is there any workaround for this? This is the only blocker I have for migrating my code to ESM from CJS.

@lvqq
Copy link
Contributor

lvqq commented Sep 5, 2022

Any updates about this issue? Same error while using ts-jest and it blocks even if I use ts-node to import jest.config.ts

@github-actions
Copy link

github-actions bot commented Oct 5, 2022

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.

@github-actions github-actions bot added the Stale label Oct 5, 2022
@Lord-Valen
Copy link

Bump

@github-actions github-actions bot removed the Stale label Oct 5, 2022
@tim-g-provectusalgae
Copy link

Well this puts us in a bind...

@ologbonowiwi
Copy link

Any possible solution? If there's any, I can help with.

@github-actions
Copy link

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.

@github-actions github-actions bot added Stale and removed Stale labels Nov 24, 2022
@Toliak
Copy link

Toliak commented Dec 11, 2022

Issue still present

@Torsitano
Copy link

Issue still present

@github-actions github-actions bot removed the Stale label Mar 25, 2023
@AlfieJones
Copy link

@thw0rted The use case for me is in the globalSetup which has the same issue

@github-actions
Copy link

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.

@github-actions github-actions bot added the Stale label Apr 26, 2023
@pocesar
Copy link

pocesar commented Apr 28, 2023

still happens

@github-actions github-actions bot removed the Stale label Apr 28, 2023
@github-actions
Copy link

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.

@github-actions github-actions bot added the Stale label May 28, 2023
@DeepUmbrella
Copy link

still happens

@github-actions github-actions bot removed the Stale label Jun 7, 2023
@github-actions
Copy link

github-actions bot commented Jul 7, 2023

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.

@github-actions github-actions bot added the Stale label Jul 7, 2023
@khitrenovich
Copy link

Still relevant...

@github-actions github-actions bot removed the Stale label Jul 7, 2023
@vdo9
Copy link

vdo9 commented Jul 18, 2023

Issue persists...

@gregorvand
Copy link

still happens

1 similar comment
@tranphuquy19
Copy link

still happens

@tiborux
Copy link

tiborux commented Aug 29, 2023

any news here?
I have the same problem
image

@adam-rocska
Copy link

srsly wtf

@roniceyemeli
Copy link

still persist

@cleve-fauna
Copy link

cleve-fauna commented Oct 27, 2023

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 ({} as any) for example { taco: "tuesday", pizza: "friday" }.

Enjoy!

Copy link

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.

@github-actions github-actions bot added the Stale label Nov 26, 2023
@adam-rocska
Copy link

Bump. Not stale. Ffs.

@github-actions github-actions bot removed the Stale label Nov 26, 2023
@SimenB SimenB added the Pinned label Nov 26, 2023
@psychobolt
Copy link

psychobolt commented Nov 26, 2023

For Yarn PnP ESM projects, I managed to workaround by opting out from ts-node commonJS register. I think multiple loaders is working for Node 18.6+ If your project is on Node 18.6+, you can see my yarn patch for lines to modify.

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 ts-node-esm loader to launch Jest CLI:

yarn node --loader ts-node/esm --loader file://$PROJECT_CWD/.pnp.loader.mjs $(yarn bin jest) # or $(yarn bin test-storybook) # `file://` for resolving windows file paths

This should be able to read your project's tsconfig.json.

@mattgodbolt
Copy link

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

@justin-masse
Copy link

The year is 2056 and jest still doesn't support the future.

I don't have a solution, just wanted to add a snarky comment because this has wasted 2-3 hours of my time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests