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]: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'. #4187

Closed FreePhoenix888 closed 2 weeks ago

FreePhoenix888 commented 1 year ago

Version

29.1.1 (latest)

Steps to reproduce

  1. git clone https://github.com/deep-foundation/npm-automation.git
  2. cd npm-automation
  3. git checkout 89153a08e34165a7b4d27730c295cdfd38c0c097
  4. npm clean-intall
  5. npm run test --templateNamePattern "sync"
  6. See this error:
    
    freephoenix888@freephoenix888:~/Programming/deep/npm-automation$ npm run test --templateNamePattern "sync"

@deep-foundation/npm-automation@3.2.1 test jest sync

FAIL tests/sync-dependencies.ts ● Test suite failed to run

src/sync-dependencies.ts:31:82 - error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'.

31   const {default: deepJson}: {default: Package} = await import(deepJsonFilePath, {assert: {type: 'json'}}) ;
                                                                                    ~~~~~~~~~~~~~~~~~~~~~~~~
src/sync-dependencies.ts:33:97 - error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'.

33   const {default: packageJson}: {default: Partial<PackageJson>} = await import(packageJsonPath, {assert: {type: 'json'}});
                                                                                                   ~~~~~~~~~~~~~~~~~~~~~~~~

Test Suites: 1 failed, 1 total Tests: 0 total Snapshots: 0 total Time: 2.14 s Ran all test suites matching /sync/i.


### Expected behavior

I expect tests to run without this error

### Actual behavior

Tests run with this error

### Debug log

freephoenix888@freephoenix888:~/Programming/deep/npm-automation$ export TS_JEST_LOG=ts-jest.log && npm run test --templateNamePattern "sync"

@deep-foundation/npm-automation@3.2.1 test jest sync

FAIL tests/sync-dependencies.ts ● Test suite failed to run

src/sync-dependencies.ts:31:82 - error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'.

31   const {default: deepJson}: {default: Package} = await import(deepJsonFilePath, {assert: {type: 'json'}}) ;
                                                                                    ~~~~~~~~~~~~~~~~~~~~~~~~
src/sync-dependencies.ts:33:97 - error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'.

33   const {default: packageJson}: {default: Partial<PackageJson>} = await import(packageJsonPath, {assert: {type: 'json'}});
                                                                                                   ~~~~~~~~~~~~~~~~~~~~~~~~

Test Suites: 1 failed, 1 total Tests: 0 total Snapshots: 0 total Time: 2.187 s Ran all test suites matching /sync/i.


### Additional context

_No response_

### Environment

```shell
System:
    OS: Linux 5.19 Ubuntu 22.04.2 LTS 22.04.2 LTS (Jammy Jellyfish)
    CPU: (12) x64 AMD Ryzen 5 2600 Six-Core Processor
  Binaries:
    Node: 18.14.0 - ~/.nvm/versions/node/v18.14.0/bin/node
    npm: 9.3.1 - ~/.nvm/versions/node/v18.14.0/bin/npm
  npmPackages:
    jest: ^29.6.1 => 29.6.1
tlaziuk commented 1 year ago

I think I have encountered a related issue:

> ###@1.9.0 test
> node --no-warnings=ExperimentalWarning --experimental-import-meta-resolve --loader ts-node/esm/transpile-only --max-old-space-size=8192 ./node_modules/jest/bin/jest.js formsection

 FAIL   ###  packages/guidebook-widget/src/components/GuidebookModal/FormView/FormSection/FormSection.spec.tsx
  ● Test suite failed to run

    packages/guidebook-widget/src/components/GuidebookModal/FormView/FormSection/FormSection.tsx:29:68 - error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.

    29 import translations from '../../../../config/translations/de.json' assert { type: 'json' }
                                                                          ~~~~~~~~~~~~~~~~~~~~~~~

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        8.535 s

(I've used # to replace confidential data)

ESM is used all across the project (the package type is module).

  System:
    OS: Linux 5.14 Ubuntu 22.04.2 LTS 22.04.2 LTS (Jammy Jellyfish)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
  Binaries:
    Node: 20.5.0 - /usr/bin/node
    Yarn: 1.22.15 - /usr/bin/yarn
    npm: 9.8.0 - /usr/bin/npm
  npmPackages:
    jest: ^29.0.0 => 29.6.2 

I'm also attaching the Jest config and resolver and the tsconfig.

ahnpnl commented 2 months ago

Jest can only process this under ESM mode. You can check documentation how to configure it https://kulshekhar.github.io/ts-jest/docs/guides/esm-support

ahnpnl commented 2 weeks ago

Added e2e test included this case to prove the latest version works with this, see https://github.com/kulshekhar/ts-jest/blob/main/e2e/esm-features/__tests__/esm-features.spec.ts

Please note that only ESNext would work. Node16/NodeNext is not supported yet