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]: ESM syntax is not allowed in a CommonJS #4249

Open
Sahasrara opened this issue Dec 29, 2023 · 1 comment
Open

[Bug]: ESM syntax is not allowed in a CommonJS #4249

Sahasrara opened this issue Dec 29, 2023 · 1 comment

Comments

@Sahasrara
Copy link

Version

^29.1.1

Steps to reproduce

Install:

  • ts-jest
  • ts-node
  • jest
  • @types/jest
  • @jest/globals

Setup your configuration file:

/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
  preset: 'ts-jest/presets/js-with-ts-esm',
  extensionsToTreatAsEsm: ['.ts'],
  testEnvironment: 'node',
  clearMocks: true,
};

Try to run a test that uses ESM modules and you'll get this error:

ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.

Expected behavior

I would expect the modules to load correctly.

Actual behavior

The modules do not load at all.

Debug log

Too long

Additional context

No response

Environment

System:
    OS: Windows 11 10.0.22631
    CPU: (24) x64 AMD Ryzen 9 5900X 12-Core Processor
  Binaries:
    Node: 21.2.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.21 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 10.2.4 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.13.1 - ~\AppData\Local\pnpm\pnpm.EXE
  npmPackages:
    jest: ^29.7.0 => 29.7.0
@randre70
Copy link

And further set the package.json "type": "module". This makes "verbatimModuleSyntax":true a requirement. Otherwise node would complain on "import { some } from './'" statements without a trailing ".js" extension.


Node.js v21.6.2
✖ tsc-output/test-import.test.js (73.202709ms)
  'test failed'

node:internal/modules/esm/resolve:263
    throw new ERR_MODULE_NOT_FOUND(
          ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '<filepath-without-js-extension>' imported from tsc-output/test-import.test.js
    at finalizeResolution (node:internal/modules/esm/resolve:263:11)
    at moduleResolve (node:internal/modules/esm/resolve:908:10)
    at defaultResolve (node:internal/modules/esm/resolve:1131:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:390:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:359:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:234:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:85:39)
    at link (node:internal/modules/esm/module_job:84:36) {
  code: 'ERR_MODULE_NOT_FOUND',
  url: 'file:///<filepath-without-js-extension>'
}

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

2 participants