kulshekhar / ts-jest

A Jest transformer with source map support that lets you use Jest to test projects written in TypeScript.
https://kulshekhar.github.io/ts-jest
MIT License
6.92k stars 452 forks source link

[Bug]: ESM + Typescript doesn't work while transpiling testEnvironment #4195

Open luiz290788 opened 1 year ago

luiz290788 commented 1 year ago

Version

29.1.1

Steps to reproduce

  1. Clone my repo https://github.com/luiz290788/jest-esm-typescript
  2. run NODE_OPTIONS="--experimental-vm-modules" npx jest
  3. test will fail with message:

    FAIL  ./my-test.test.ts
    ● Test suite failed to run
    
    my-environment.ts:4:13 - error TS1343: The 'import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'es2022', 'esnext', 'system', 'node16', or 'nodenext'.
    
    4 console.log(import.meta.url)

Expected behavior

Expect the testEnvironment to be transpiled as expected using ESM.

Actual behavior

testEnvironment is transpiled as commonjs

Debug log

I tried to post the logs but github complained saying it was too long.

Additional context

It looks like the transpilation happens normally for the tests but the useESM is not affecting the testEnvironment.

Environment

System:
    OS: macOS 13.4.1
    CPU: (8) arm64 Apple M1
  Binaries:
    Node: 18.16.1 - ~/.nvm/versions/node/v18.16.1/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 9.8.1 - ~/.nvm/versions/node/v18.16.1/bin/npm
  npmPackages:
    jest: ^29.6.2 => 29.6.2
ckrauterlovescoffee commented 4 months ago

Any updates on this?

ahnpnl commented 3 weeks ago

@luiz290788 @ckrauterlovescoffee this is a bug with Jest. After debugging I found out that Jest never asks ts-jest to process custom testEnvironment file in ESM mode.

A workaround is that the test environment file has to be written with .js extension instead