Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Electron code coverage setup #2215

Merged
merged 5 commits into from
Feb 15, 2019
Merged

chore: Electron code coverage setup #2215

merged 5 commits into from
Feb 15, 2019

Conversation

bennycode
Copy link
Contributor

@bennycode bennycode commented Feb 15, 2019

Let me describe the code coverage workflow which makes it possible to get the coverage metrics for code that runs inside Electron's main process:

  1. TypeScript source code gets compiled with tsc
  2. Compiled code gets instrumented with istanbul
  3. Instrumented code will be tested using electron-mocha within Electron's main process
  4. A script bound to mocha's global "after" hook will write the coverage info into a coverage.json file
  5. remap-istanbul will create a nice HTML report based on coverage.json and remap the compiled code to it's TypeScript sources within the report
  6. Bonus: Code coverage thresholds will be checked using istanbul check-coverage

Although the workflow sounds straight forward, there are some caveats:

  1. Within the tests we need to import the instrumented code and not the compiled code → electron-mocha and istanbul/isparta jprichardson/electron-mocha#19 (comment)
  2. To get proper coverage reports of the full code base, we need to require every code that is being used which is done in BeforeTest.js using mocha's before hook → How to include all source files for code coverage reporting? jprichardson/electron-mocha#135

Special adjustments to make it fit with our codebase:

  1. Tests will be run from the ./electron subdirectory because we need the node modules from there plus the references to ./electron/locale
  2. To prevent tests in Electron's main process from crashing when loading code that needs to be run in Electron's renderer process, I moved all renderer-related code into ./electron/src/renderer and excluded these fragments in BeforeTest.js
  3. I moved the configuration for electron-mocha into mocha.opts, which is the default file to store configurations for mocha

"coverage:clean": "rimraf coverage instrumented-code",
"coverage:instrument": "istanbul instrument dist --no-compact --complete-copy --output instrumented-code",
"coverage:report": "remap-istanbul --input instrumented-code/coverage.json --output coverage -t html",
"coverage:test": "electron-mocha",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this script can be renamed just test. It is not directly linked to coverage, am I wrong?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, has been renamed: 6f4f2e6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants