jaredpalmer / tsdx

Zero-config CLI for TypeScript package development
https://tsdx.io
MIT License
11.21k stars 505 forks source link

tsconfig.json ESM options not respected when using `yarn test --coverage` #1011

Open karlhorky opened 3 years ago

karlhorky commented 3 years ago

Current Behavior

$ git clone https://github.com/upleveled/preflight.git
$ cd preflight
$ yarn
$ yarn test --coverage

...

Running coverage on untested files...Failed to collect coverage from /Users/k/p/preflight/src/preflightBinPath.ts
ERROR: src/preflightBinPath.ts:5:38 - error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.

5 const { stdout: preflightBinPath } = await execa.command(`yarn bin`, {
                                       ~~~~~
src/preflightBinPath.ts:6:30 - error TS1343: The 'import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'esnext', or 'system'.

6   cwd: dirname(fileURLToPath(import.meta.url)),
                               ~~~~~~~~~~~
STACK: 
Failed to collect coverage from /Users/k/p/preflight/src/checks/eslintConfigIsLatestVersion.ts
ERROR: src/checks/eslintConfigIsLatestVersion.ts:7:7 - error TS2441: Duplicate identifier 'require'. Compiler reserves name 'require' in top level scope of a module.

7 const require = createRequire(`${process.cwd()}/`);
        ~~~~~~~
STACK: 
Failed to collect coverage from /Users/k/p/preflight/src/checks/preflightIsLatestVersion.ts
ERROR: src/checks/preflightIsLatestVersion.ts:15:50 - error TS1343: The 'import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'esnext', or 'system'.

15     await fs.readFile(new URL('../package.json', import.meta.url), 'utf-8'),
                                                    ~~~~~~~~~~~
STACK: 
Failed to collect coverage from /Users/k/p/preflight/src/checks/prettier.ts

These errors do not show up in the normal yarn test, since the tsconfig.json is respected.

Expected behavior

The tsconfig.json file should also be respected for the Coverage testing mode.

Suggested solution(s)

I tried to look into it for a few minutes, but didn't get too far - not sure which dependency is having troubles loading the tsconfig.json.

At first I thought it was ts-jest, which doesn't yet support ESM, but this is looking more and more like a red herring...

Additional context

I tried also adding NODE_OPTIONS=--experimental-vm-modules as suggested by the Jest ESM docs, but this also didn't help.

Your environment

  System:
    OS: macOS 11.2.3
    CPU: (8) arm64 Apple M1
    Memory: 134.92 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 15.10.0 - /opt/homebrew/bin/node
    Yarn: 1.22.10 - /opt/homebrew/bin/yarn
    npm: 7.5.3 - /opt/homebrew/bin/npm
  Browsers:
    Chrome: 89.0.4389.128
    Chrome Canary: 92.0.4476.0
    Firefox: 87.0
    Safari: 14.0.3
  npmPackages:
    tsdx: 0.14.1 => 0.14.1 
    typescript: 4.2.3 => 4.2.3