How to generate typescript code coverage with mocha.js?

Member

by darion , in category: Javascript , 3 months ago

How to generate typescript code coverage with mocha.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by mac , 3 months ago

@darion 

To generate code coverage reports with mocha.js and Typescript, you can follow these steps:

  1. Install necessary dependencies: npm install --save-dev ts-node typescript @babel/preset-typescript istanbul-lib-instrument nyc
  2. Configure TypeScript to compile to ES6 modules in your tsconfig.json file: { "compilerOptions": { "module": "es6", "target": "es6", "isolatedModules": true } }
  3. Create a script in your package.json file to run tests with code coverage: { "scripts": { "test": "nyc --require ts-node/register --extension .ts mocha --require @babel/register --require @babel/preset-typescript test/**/*.spec.ts" } }
  4. Run your tests with the npm test command: npm test
  5. A coverage report will be generated in the ./coverage directory. You can view the HTML report by opening the index.html file in a browser.


By following these steps, you can generate code coverage reports for your Typescript code using mocha.js and nyc.