jest icon indicating copy to clipboard operation
jest copied to clipboard

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

Open CyberCookie opened this issue 2 years ago • 42 comments

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 avatar Jun 20 '22 14:06 CyberCookie

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[bot] avatar Jul 20 '22 14:07 github-actions[bot]

Issue still present

Torsitano avatar Jul 20 '22 14:07 Torsitano

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[bot] avatar Aug 19 '22 14:08 github-actions[bot]

Issue still present

Torsitano avatar Aug 19 '22 15:08 Torsitano

Yeah, this isn't currently possible - see https://github.com/facebook/jest/issues/11453#issuecomment-1040424443. 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

SimenB avatar Aug 22 '22 11:08 SimenB

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

Abhi347 avatar Aug 30 '22 14:08 Abhi347

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

lvqq avatar Sep 05 '22 01:09 lvqq

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[bot] avatar Oct 05 '22 02:10 github-actions[bot]

Bump

Lord-Valen avatar Oct 05 '22 15:10 Lord-Valen

Well this puts us in a bind...

tim-g-provectusalgae avatar Oct 11 '22 02:10 tim-g-provectusalgae

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

ologbonowiwi avatar Oct 25 '22 14:10 ologbonowiwi

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[bot] avatar Nov 24 '22 14:11 github-actions[bot]

Issue still present

Toliak avatar Dec 11 '22 22:12 Toliak

Any update on this ?

ashfaqnisar avatar Dec 20 '22 06:12 ashfaqnisar

Still present.

ologbonowiwi avatar Jan 11 '23 14:01 ologbonowiwi

I think there's some confusion because the tsconfig used to compile jest.config.ts, before launching the test runner, is not necessarily the same tsconfig used by the ts-jest transformer to compile the TS code under test.

I don't write my Jest config in TS -- it works fine in JS with type annotation comment directives -- but for those who do, is there really a use-case for consuming import.meta while configuring Jest (i.e., specifically in jest.config.ts)?

As far as consuming import.meta in the code under test, I believe that should fall into the purview of ts-jest -- they punted over here specifically because you're trying to refer to it in the config file, before ts-jest actually does anything.

ETA: actually I don't think there is a problem on the ts-jest side. I just found https://github.com/kulshekhar/ts-jest/issues/3888 which links to this handy example, where the user is successfully running a test that uses import.meta, using ts-jest/presets/default-esm. So, bottom line, this issue is only about import.meta in jest.config.ts, specifically, and if you want to use import.meta in your code and/or tests, that already works. (Again, if you are trying to use import.meta in jest.config.ts, I would be really curious to know why.)

thw0rted avatar Feb 02 '23 18:02 thw0rted

@SimenB if my logic in the previous comment is correct, then this should be closed now as a duplicate of https://github.com/facebook/jest/issues/13118 .

thw0rted avatar Feb 03 '23 14:02 thw0rted

I don't use it specifically in the jest.config.ts file, but my project does use it in the globalSetup.ts file which has the same issue.

MaverickH avatar Feb 23 '23 19:02 MaverickH

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[bot] avatar Mar 25 '23 19:03 github-actions[bot]

Issue still present

Torsitano avatar Mar 25 '23 22:03 Torsitano

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

AlfieJones avatar Mar 27 '23 21:03 AlfieJones

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[bot] avatar Apr 26 '23 22:04 github-actions[bot]

still happens

pocesar avatar Apr 28 '23 02:04 pocesar

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[bot] avatar May 28 '23 02:05 github-actions[bot]

still happens

DeepUmbrella avatar Jun 07 '23 07:06 DeepUmbrella

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[bot] avatar Jul 07 '23 08:07 github-actions[bot]

Still relevant...

khitrenovich avatar Jul 07 '23 12:07 khitrenovich

Issue persists...

vdo9 avatar Jul 18 '23 19:07 vdo9

still happens

gregorvand avatar Jul 27 '23 08:07 gregorvand

still happens

tranphuquy19 avatar Aug 09 '23 18:08 tranphuquy19