istanbuljs / nyc

the Istanbul command line interface
https://istanbul.js.org/
ISC License
5.54k stars 353 forks source link

How to use nyc with TypeScript and ESM? #1537

Closed bennycode closed 8 months ago

bennycode commented 8 months ago

Hi there, I moved my TypeScript codebase from CommonJS to ESM (using "type": "module" in my package.json file). My nyc.config.json looks like this:

{
  "all": false,
  "check-coverage": false,
  "exclude": ["**/*.d.ts", "**/*.test*.ts", "**/index.ts", "**/demo/**/*"],
  "extension": [".ts"],
  "include": ["src/**/*.ts"],
  "per-file": false,
  "reporter": ["text-summary"],
  "require": ["ts-node/esm"]
}

According to the docs of ts-node, I have to switch from --require ts-node/register to --loader ts-node/esm.

I tried to change "require": ["ts-node/esm"] to "loader": ["ts-node/esm"] in my nyc.config.json but I am getting this error:

Must use import to load ES Module

Can you help me with a code recipe to solve that? Currently I am starting my tests with Jasmine without nyc: ts-node-esm ./node_modules/.bin/jasmine --config=jasmine.json.

Best, Benny

bennycode commented 8 months ago

Today I learnt that c8 supports .nycrc.json config files, so I could easily switch to c8 and make it working with TypeScript, ESM & Jasmine:

{
  "scripts": {
    "test": "c8 --config=.nycrc.json ts-node-esm ./node_modules/.bin/jasmine --config=jasmine.json"
  }
}