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

[Native ESM] [ts-jest@next] ReferenceError: exports is not defined #2399

Closed
qnighy opened this issue Feb 23, 2021 · 6 comments
Closed

[Native ESM] [ts-jest@next] ReferenceError: exports is not defined #2399

qnighy opened this issue Feb 23, 2021 · 6 comments
Labels
Not An Issue Not ts-jest issue

Comments

@qnighy
Copy link

qnighy commented Feb 23, 2021

🐛 Bug Report

Under a certain condition with native ESM support, the test fails with ReferenceError: exports is not defined.

To Reproduce

package.json:

{
  "private": true,
  "type": "module",
  "devDependencies": {
    "@babel/core": "^7.13.1",
    "jest": "^27.0.0-next.3",
    "ts-jest": "^27.0.0-next.6",
    "typescript": "^4.1.5"
  },
  "scripts": {
    "test": "node --experimental-vm-modules node_modules/.bin/jest --no-cache"
  },
  "jest": {
    "transform": {
      "\\.[jt]sx?$": "ts-jest"
    },
    "extensionsToTreatAsEsm": [
      ".ts"
    ]
  }
}

lib.ts:

export const X = 42;

lib.test.ts:

import { X } from "./lib";

test("X", () => {
  expect(X).toBe(42);
});

tsconfig.json:

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "ESNext",
    "lib": ["ES2015", "DOM"],
    "allowJs": true,
    "checkJs": true,
    "isolatedModules": true,
    "strict": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
  }
}
$ node --version
v14.15.5
$ yarn test
yarn run v1.22.5
$ node --experimental-vm-modules node_modules/.bin/jest --no-cache
(node:23494) 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)
 FAIL  ./lib.test.ts
  ● Test suite failed to run

    ReferenceError: exports is not defined

      1 | import { X } from "./lib";
    > 2 |
        | ^
      3 | test("X", () => {
      4 |   expect(X).toBe(42);
      5 | });

      at lib.test.ts:2:23

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        2.343 s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

And without --no-cache, the following behaviors are observed:

  • Change lib.ts a bit (e.g. adding / removing empty lines) and rerun the test: the test starts to fail.
  • Change lib.test.ts a bit (e.g. adding / removing empty lines) and rerun the test: the test starts to pass.

Expected behavior

The test is expected to always pass.

Link to repo (highly encouraged)

https://github.com/qnighy/ts-jest-esm-failure-example

The example above was extracted from https://github.com/qnighy/ts-type-module-example.

Debug log:

https://gist.github.com/qnighy/9ba2c6b2023c4a8dad38c53faf4ed592

envinfo

System:
    OS: Ubuntu 20.04 on WSL2
    Node version: v14.15.5

Npm packages:
    jest: 27.0.0-next.3
    ts-jest: 27.0.0-next.6
    typescript: 4.1.5
    babel(optional): 7.13.1
@qnighy qnighy added Bug Report Needs Repo Need a minimium repository to reproduce the problem Needs Triage labels Feb 23, 2021
@ahnpnl
Copy link
Collaborator

ahnpnl commented Feb 23, 2021

Hi, would you please check with Jest repo. IIRC export is not supported yet. We only transpile ts to js and Jest runs the transpires codes so I don’t think this is ts-jest issue.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Feb 23, 2021

See more at jestjs/jest#9430

@ahnpnl ahnpnl closed this as completed Feb 23, 2021
@ahnpnl ahnpnl added Not An Issue Not ts-jest issue and removed Bug Report Needs Repo Need a minimium repository to reproduce the problem Needs Triage labels Feb 23, 2021
@qnighy
Copy link
Author

qnighy commented Feb 23, 2021

See more at jestjs/jest#9430

@ahnpnl Yes, I was experimenting with the native ESM support mentioned in jestjs/jest#9430, and by your comment there I thought it's being implemented in ts-jest@next. Is my understanding wrong, or shouldn't have I filed ts-jest@next's issue here?

@qnighy qnighy changed the title [Native ESM] ReferenceError: exports is not defined [Native ESM] [ts-jest@next] ReferenceError: exports is not defined Feb 23, 2021
@qnighy
Copy link
Author

qnighy commented Feb 23, 2021

A similar configuration works with babel-jest@next so I thought it's ts-jest@next's issue

@ahnpnl
Copy link
Collaborator

ahnpnl commented Feb 23, 2021

I see you missed one important thing, enable support esm config option for ts-jest. If you go to online doc and choose next version, you will find the guide :)

@qnighy
Copy link
Author

qnighy commented Feb 23, 2021

Aaah, this one! https://kulshekhar.github.io/ts-jest/docs/next/guides/esm-support

Thanks! It turned out to be my mistake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Not An Issue Not ts-jest issue
Projects
None yet
Development

No branches or pull requests

2 participants