evan361425 / playground-ts-esm

Solve Node.js in ESM and TypeScript with test coverage.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ESM-TS-Coverage

This repo try to solve Node.js in ESM and TypeScript with test coverage.

packages:

# Test framework, you can use mocha instead
npm i -D jasmine @types/jasmine
# Coverage, important! nyc not support well in ESM. See details in Misc
npm i -D c8
npm i -D ts-node typescript
Jasmine additional config

Create jasmine.ts to compatible with TypeScript.


tsconfig.json:

{
  "compilerOptions": {
    // ...
    "module": "es2022",
    "esModuleInterop": true
    // ...
  },
  "ts-node": {
    "esm": true // If not provided, use ts-node-esm instead
  }
}

Start command

Test:

npx ts-node jasmine.ts

Or add to package.json

{
  "scripts": {
    "test": "ts-node jasmine.ts"
  }
}

Coverage:

npx c8 ts-node jasmine.ts

Or add to package.json

{
  "scripts": {
    "test": "c8 npm test"
  }
}

Misc

Why use c8? according to nyc officially:

Until the node.js feature is stabilized breakage should not be unexpected. For more stable options to test coverage you can:

  • Use c8
  • Pre-instrument your code (run nyc instrument then test the output)

About

Solve Node.js in ESM and TypeScript with test coverage.


Languages

Language:TypeScript 64.1%Language:JavaScript 35.9%