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]: can't use ESM with external node modules that export commonjs and moduel files #3800

Closed
rbnayax opened this issue Sep 9, 2022 · 14 comments

Comments

@rbnayax
Copy link

rbnayax commented Sep 9, 2022

Version

29.0.0

Steps to reproduce

  1. clone my repo - https://github.com/rbnayax/ts-jest-example
  2. run yarn
  3. run yarn test

Expected behavior

test should pass

Actual behavior

 FAIL  lib/fn/fn.spec.ts
  ● Test suite failed to run

    lib/fn/fn.ts:1:21 - error TS7016: Could not find a declaration file for module 'colorette'. '/home/regev/WebstormProjects/ts-jest-example/node_modules/colorette/index.cjs' implicitly has an 'any' type.
      Try `npm i --save-dev @types/colorette` if it exists or add a new declaration (.d.ts) file containing `declare module 'colorette';`

    1 import {green} from "colorette";
                          ~~~~~~~~~~~

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.761 s
Ran all test suites.
(node:415750) ExperimentalWarning: VM Modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)

Debug log

link

Additional context

when removing the reference to 3rd party node module, everything works as expected

Environment

System:
    OS: Linux 5.14 Ubuntu 20.04.5 LTS (Focal Fossa)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-11370H @ 3.30GHz
  Binaries:
    Node: 16.11.1 - ~/.nvm/versions/node/v16.11.1/bin/node
    Yarn: 3.2.1 - ~/.nvm/versions/node/v16.11.1/bin/yarn
    npm: 8.0.0 - ~/.nvm/versions/node/v16.11.1/bin/npm
  npmPackages:
    jest: ^29.0.2 => 29.0.2
@unional
Copy link

unional commented Sep 10, 2022

Updating to ts-jest 29 breaks import (and all CI goes into infinite loop)

Getting ReferenceError: exports is not defined while it was working fine with ts-jest@28 with jest@28|29.

I've tried moving the config from global to transform.

const config = {
  preset: 'ts-jest/presets/default-esm',
  extensionsToTreatAsEsm: ['.ts'],
  moduleNameMapper: {
    '^(\\.{1,2}/.*)\\.js$': '$1',
    '#(.*)': '$1'
  },
  roots: [
    '<rootDir>/ts'
  ],
  testEnvironment: 'node',
  testMatch: [
    '**/ts/?(*.)+(spec|test|integrate|accept|system|unit).[jt]s?(x)'
  ],
  transform: {
    '^.+.tsx?$': ['ts-jest', { isolatedModules: true, useESM: true }]
  },
  transformIgnorePatterns: [
    // Need to MANUALLY identify each ESM package, one by one
    'node_modules/(?!(assertron|chalk|#ansi-styles)/)'
  ]
}

module.exports = config

The infinite loop in CI is a concern. (e.g. https://github.com/unional/clibuilder/runs/8280815715?check_suite_focus=true)
They are triggered by tools such as renovate.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Sep 10, 2022

  • does this happen to next tag?
  • does this happen when using global?
  • In v29, we don’t have anything change specifically related to this, how about the combination of Jest 28 and ts-jest 29?

@rbnayax
Copy link
Author

rbnayax commented Sep 10, 2022

next tag is the same as 29 no?
global doesn't help
I tried it using jest 28 and ts-jest 28 and it doesn't work as well...
jest 28 with ts-jest 29 fails the same way...
I tried using a different node module that has typing in it (but it is not of type module) and it works so the issue must be related to packages that export module and commonjs at the same time (like colorette)
It is important to mention that typesciprt compiles without errors so it must be something to do with the transformation during the Jest run.

@rbnayax rbnayax changed the title [Bug]: can't use ESM with external node modules [Bug]: can't use ESM with external node modules that export commonjs and moduel files Sep 10, 2022
@ahnpnl
Copy link
Collaborator

ahnpnl commented Sep 11, 2022

@rbnayax please use isolatedModules: true as a workaround. Indeed there is an issue with type checking which is provided by ts-jest

@okitan
Copy link

okitan commented Sep 13, 2022

@rbnayax

Have you tried removing preset from your config?
I think setting both preset and transform at once will cause two transformations.

@rbnayax
Copy link
Author

rbnayax commented Sep 14, 2022

@rbnayax

Have you tried removing preset from your config? I think setting both preset and transform at once will cause two transformations.

I did it does not help

@goums
Copy link

goums commented Sep 15, 2022

I had the same issue with jest: 29 / ts-jest: 29
downgrading to ts-jest: 28 fixed it

@ahnpnl
Copy link
Collaborator

ahnpnl commented Sep 15, 2022

@goums this issue is not related to v28 or v29. This issue already happened to v28 because it's related to TypeScript APIs itself which ts-jest didn't change in that area.

@goums
Copy link

goums commented Sep 15, 2022

@ahnpnl I'm working on a private repo, so I'm not able to share it unfortunately, but it looks like there are some esm incompatibility issues on ts-jest 29. Maybe you updated some dependency versions or configuration that introduce an incompatibility?

just sharing some command line results, without touching anything in the code:

  1. testing with ts-jest@next ( 29.0.0-next.1 ) is crashing
yarn add ts-jest@next
yarn run test 
...
  ● Test suite failed to run

    ReferenceError: exports is not defined

      1 | import { spawn } from "node:child_process";
    > 2 |
        | ^
  1. downgrading to ts-jest@28 fix the issue
yarn add ts-jest@28
yarn run test 
...
Test Suites: 2 passed, 2 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        9.693 s

Other versions I'm using:

"typescript": "4.7.4",
"ts-node": "10.9.1",
"jest": "29.0.3",

@ahnpnl
Copy link
Collaborator

ahnpnl commented Sep 15, 2022

We only compile ts to js so the error you see is about Jest not giving the file to ts-jest to compile

@goums
Copy link

goums commented Sep 15, 2022

Have you tried removing preset from your config? I think setting both preset and transform at once will cause two transformations.

removing the preset works for me when using ts-jest@29, thanks @okitan 👍

so maybe need to update the docs here:
https://kulshekhar.github.io/ts-jest/docs/guides/esm-support#use-esm-presets
as the example config is working fine on v28 but not in v29

@ahnpnl
Copy link
Collaborator

ahnpnl commented Sep 15, 2022

Feel free to open a PR to adjust the doc:)

@lbesson
Copy link

lbesson commented Sep 16, 2022

One (maybe dumb) question, but couldn't / shouldn't the preset include the useESM: true option?

ahnpnl added a commit to ahnpnl/colorette that referenced this issue Sep 25, 2022
`types` corresponds to `main` only. `main/types` will be completely ignored by Node/TypeScript if `exports` is defined.

Related to kulshekhar/ts-jest#3800
@ahnpnl ahnpnl added Upstream Bug and removed 🐛 Bug Confirmed Bug is confirmed labels Sep 25, 2022
@ahnpnl
Copy link
Collaborator

ahnpnl commented Sep 25, 2022

@rbnayax this issue will be fixed in the next release of colorette. You can check the PR which is linked.

@ahnpnl ahnpnl closed this as completed Sep 25, 2022
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

6 participants