ยท testing
Setup TypeScript code coverage for Electron applications
This tutorial teaches you how to include all source files for code coverage reporting in an Electron app. It covers steps such as using `babel` with `electron-mocha`, registering plugins in `babel` to instrument code, and running `nyc` to create a coverage report. The article also provides code snippets and configuration files to help you implement these steps.
Learn how to include all source files for code coverage reporting in an Electron app. In the following tutorial we will create TypeScript code coverage from Electron's main and renderer process.
Contents
Environment
Tested with:
- Node.js v10.9.0
- Electron v4.0.5
To receive code coverage based on TypeScript source code within an Electron environment, we need to do the following:
- Tell
electron-mocha
to usebabel
- Register
@babel/preset-typescript
inbabel
to compile our code on-the-fly - Register
istanbul
plugin inbabel
to instrument our compiled code - Register a
after
hook inmocha
to write out our coverage information (provided byistanbul
) - Run
nyc
to create a HTML report from our coverage information stored in.nyc_output
Takeaways
nyc
can combine multiple coverage files (likecoverage.json
) into one reporteletron-mocha
can run tests in an Electron renderer process and in an Electron main process- To get full code coverage, there needs to be a test run with test files for the main process (
*.test.main.ts
) and a second run for tests from the renderer process (*.test.renderer.ts
) - Electron provides a
process.type
(can bebrowser
orrenderer
) to indicate in which process the code runs - To have a universal
after
hook for both test runs, theprocess.type
can be used in the coverage output file name istanbul
needs to exclude the test files, otherwise it will report code coverage for the test files too