This repo try to solve Node.js in ESM and TypeScript with test coverage.
# 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.
{
"compilerOptions": {
// ...
"module": "es2022",
"esModuleInterop": true
// ...
},
"ts-node": {
"esm": true // If not provided, use ts-node-esm instead
}
}
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"
}
}
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)