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]: error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'. #4202

Open
zaininfo opened this issue Sep 1, 2023 · 4 comments

Comments

@zaininfo
Copy link

zaininfo commented Sep 1, 2023

Version

29.1.1

Steps to reproduce

With the following TS config:

{
    "compilerOptions": {
        "module": "esnext",
        "resolveJsonModule": true,
    }
}

And, the following Jest config:

{
    "preset": "ts-jest"
}

A file index.ts which imports a JSON file with a type assertion e.g.

import * as config from './config.json' assert {type:'json'}

when imported in a Jest test file e.g.

import index from './index'

describe('test', () => {
    test('should pass', () => {
		expect(index).toBeDefined()
    })
})

gives an error.

Expected behavior

Test passes.

Actual behavior

Test fails with a TS error:

    index.ts:1:41 - error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.

    1 import * as config from './config.json' assert {type:'json'}
                                              ~~~~~~~~~~~~~~~~~~~~

Debug log

{"context":{"call":null,"logLevel":10,"namespace":"ts:serviceHost","package":"ts-jest","version":"29.1.1"},"message":"readFile","sequence":412,"time":"2023-09-01T20:04:41.079Z"}
{"context":{"call":null,"logLevel":20,"namespace":"hoist-jest","package":"ts-jest","version":"29.1.1"},"message":"visitSourceFileNode(): hoist jest","sequence":413,"time":"2023-09-01T20:04:41.196Z"}
{"context":{"fileName":"C:\\project\\index.ts","logLevel":20,"namespace":"ts-compiler","package":"ts-jest","version":"29.1.1"},"message":"_doTypeChecking(): computing diagnostics using language service","sequence":414,"time":"2023-09-01T20:04:41.208Z"}
{"context":{"diagnosticCodes":[2821],"diagnosticText":"\u001b[96mindex.ts\u001b[0m:\u001b[93m1\u001b[0m:\u001b[93m41\u001b[0m - \u001b[91merror\u001b[0m\u001b[90m TS2821: \u001b[0mImport assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.\n\n\u001b[7m1\u001b[0m import * as config from './config.json' assert {type:'json'}\n\u001b[7m \u001b[0m \u001b[91m                                        ~~~~~~~~~~~~~~~~~~~~\u001b[0m\n","logLevel":20,"namespace":"TSError","package":"ts-jest","version":"29.1.1"},"message":"created new TSError","sequence":415,"time":"2023-09-01T20:04:41.209Z"}
{"context":{"logLevel":20,"namespace":"ts-jest-transformer","package":"ts-jest","version":"29.1.1"},"message":"created new transformer","sequence":416,"time":"2023-09-01T20:04:41.216Z"}

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 of module is changed from esnext.
So, the value of module 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:

error TS6046: Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015', 'es2020', 'es2022', 'esnext', 'node16', 'nodenext'.

So, it looks like TS Jest is overriding the esnext value.

Environment

Binaries:
    Node: 18.17.1 - C:\Program Files\nodejs\node.EXE
    npm: 9.6.7 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    jest: ^29.6.4 => 29.6.4
@mike-lischke
Copy link

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" };

@priley86
Copy link

any update on this issue? Stumbling on it today 🙃

@sezanzeb
Copy link

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 #3525 and https://jestjs.io/docs/ecmascript-modules

@sezanzeb
Copy link

sezanzeb commented Apr 19, 2024

(Or use vitest instead of jest https://vitest.dev/guide/migration.html#migrating-from-jest, which works for me without configuring anything at all)

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

4 participants