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]: Version 29.2.1 breaks default imports #4468

Closed
flovouin opened this issue Jul 23, 2024 · 2 comments · Fixed by #4487
Closed

[Bug]: Version 29.2.1 breaks default imports #4468

flovouin opened this issue Jul 23, 2024 · 2 comments · Fixed by #4487
Labels
🐛 Bug Confirmed Bug is confirmed

Comments

@flovouin
Copy link

Version

29.2.1

Steps to reproduce

  1. Clone https://github.com/flovouin/ts-jest-default-imports-bug/.
  2. Run npm i && npm run build. TypeScript compilation should succeed.
  3. Run npm test.

Expected behavior

npm test should pass with no error.
You can install ts-jest version 29.2.0 instead and see it succeed.

Actual behavior

$ npm test

> test@0.1.0 test
> NODE_OPTIONS="$NODE_OPTIONS --experimental-vm-modules --no-warnings=ExperimentalWarning" jest

 FAIL  src/index.spec.ts
  ● Test suite failed to run

    src/index.ts:2:28 - error TS2351: This expression is not constructable.
      Type 'typeof import("[..]/node_modules/i18next-fs-backend/cjs/index")' has no construct signatures.

    2 export const backend = new Backend();
                                 ~~~~~~~

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.38 s
Ran all test suites.

For what it's worth, even though I wasn't expecting to see the CJS version imported, node_modules/i18next-fs-backend/cjs/index does exist and export a default class.

Debug log

ts-jest.log

Additional context

No response

Environment

System:
    OS: macOS 14.5
    CPU: (10) arm64 Apple M1 Max
  Binaries:
    Node: 20.12.1 - ~/.asdf/installs/nodejs/20.12.1/bin/node
    npm: 10.8.2 - ~/.asdf/plugins/nodejs/shims/npm
  npmPackages:
    jest: ^29.7.0 => 29.7.0
@ahnpnl
Copy link
Collaborator

ahnpnl commented Jul 23, 2024

I could understand why it worked at 29.2.0 because we always used target: ESNext for ESM mode and Node16/NodeNext wasn't taken into account.

For some reasons, ts-jest starts throwing type error related when taking into account of Node16/NodeNext in your case.

As a workaround, you can use target: ESNext which should fix the issue

@flovouin
Copy link
Author

flovouin commented Jul 23, 2024

Thanks for the reply! Indeed, updating the Jest configuration does the trick:

  transform: {
    "^.+\\.ts$": ["ts-jest", { useESM: true, tsconfig: { module: "ESNext" } }],
  },

I'll let you keep the issue around or close it depending on how you want to address this. In my case it's probably an acceptable compromise, even though it may create new problems down the line.

ahnpnl added a commit that referenced this issue Jul 31, 2024
- For CJS mode, `module` is always `CommonJS`
- For ESM mode, `module` with value `Node16/NodeNext` will use `ESNext`, otherwise use the value provided by test `tsconfig`
- `moduleResolution` is always `Node10`

Fixes #4468
Fixes #4473
ahnpnl added a commit that referenced this issue Jul 31, 2024
- For CJS mode, `module` is always `CommonJS`
- For ESM mode, `module` with value `Node16/NodeNext` will use `ESNext`, otherwise use the value provided by test `tsconfig`
- `moduleResolution` is always `Node10`

Fixes #4468
Fixes #4473
ahnpnl added a commit that referenced this issue Aug 1, 2024
- For CJS mode, `module` is always `CommonJS`
- For ESM mode, `module` with value `Node16/NodeNext` will use `ESNext`, otherwise use the value provided by test `tsconfig`
- `moduleResolution` is always `Node10`

Fixes #4468
Fixes #4473
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 Bug Confirmed Bug is confirmed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants