kulshekhar / ts-jest

A Jest transformer with source map support that lets you use Jest to test projects written in TypeScript.

Home Page:https://kulshekhar.github.io/ts-jest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug]: ReferenceError: exports is not defined when using --experimental-vm-modules

NathanVss opened this issue · comments

Version

29.0.3

Steps to reproduce

I followed the documentation ( https://kulshekhar.github.io/ts-jest/docs/next/guides/esm-support/ ) to enable the support for Typescript and ESM on Jest.

jest.config.ts

import { JestConfigWithTsJest } from "ts-jest";

const jestConfig: JestConfigWithTsJest = {
  preset: "ts-jest",
  testEnvironment: "node",
  extensionsToTreatAsEsm: [".ts"],
  moduleNameMapper: {
    "#ansi-styles": "ansi-styles/index.js",
    "#supports-color": "supports-color/index.js",
    "^(\\.{1,2}/.*)\\.js$": "$1",
  },
  transform: {
    // '^.+\\.[tj]sx?$' to process js/ts with `ts-jest`
    // '^.+\\.m?[tj]sx?$' to process js/ts/mjs/mts with `ts-jest`
    "^.+\\.m?[tj]sx?$": [
      "ts-jest",
      {
        tsconfig: "./tsconfig.json",
        useESM: true,
        isolatedModules: true,
      },
    ],
  },
  transformIgnorePatterns: ["/node_modules/(?!(chalk))"],
};

export default jestConfig;

tsconfig.json

{
  "extends": "@openfun/typescript-configs/node.json",
  "include": [
    "./**/*"
  ],
  "compilerOptions": {
    "baseUrl": ".",
    "outDir": "../../dist",
    "module": "Node16",
    "moduleResolution": "Node16",
    "sourceMap": true,
    "target": "es6",
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "lib": [
      "dom",
      "dom.iterable",
      "es6",
      "scripthost",
      "es2015",
      "es2016",
      "es2017",
      "es2021.string",
      "esnext.intl"
    ],
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "strict": true
  }
}

src/bin/tests/Cunningham.spec.ts

describe("Cunningham Bin", () => {
  test("Run", async () => {
    expect(1).toEqual(1);
  });
});

package.json

{
  ...
  "type": "module",
  ...
}

Expected behavior

Adding --experimental-vm-modules should not execute my tests as CJS.

Actual behavior

When I run node_modules/.bin/jest --config ./src/bin/jest.config.ts --no-cache everything works fine:

 PASS  src/bin/tests/Cunningham.spec.ts
  Cunningham Bin
    ✓ Run (1 ms)

But when I add the --experimental-vm-modules flag here is what I get:

NODE_OPTIONS=--experimental-vm-modules node_modules/.bin/jest --config ./src/bin/jest.config.ts --no-cache

 FAIL  src/bin/tests/Cunningham.spec.ts
  ● Test suite failed to run

    ReferenceError: exports is not defined

This error leads me to think that enabling NODE_OPTIONS=--experimental-vm-modules makes the test to be executed as CommonJS .. and not ESM.

NB: "You could juste not set the NODE_OPTIONS option if it works without" -> Nope, I can't because. Here I simplified this example, but in my real tests I need it You need to run with a version of node that supports ES Modules in the VM API. See https://jestjs.io/docs/ecmascript-modules )

I spent the afternoon on this problem and I can find a solution .. thank you in advance

Debug log

 FAIL  src/bin/tests/Cunningham.spec.ts
  ● Test suite failed to run

    ReferenceError: exports is not defined



      at tests/Cunningham.spec.ts:11:23

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.328 s
Ran all test suites.
(node:32070) 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)

Additional context

No response

Environment

System:
    OS: macOS 13.0.1
    CPU: (10) arm64 Apple M1 Pro
  Binaries:
    Node: 16.15.1 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 8.11.0 - /usr/local/bin/npm
commented

This is related to configuration. You can check example app https://github.com/kulshekhar/ts-jest/tree/main/examples to find the correct config. I hope it helps.