Skip to content

Instantly share code, notes, and snippets.

@cwmanning
Last active April 21, 2022 21:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwmanning/60cc252d391cf781bd94960fd1c6f5c8 to your computer and use it in GitHub Desktop.
Save cwmanning/60cc252d391cf781bd94960fd1c6f5c8 to your computer and use it in GitHub Desktop.
Test Suite Performance with Cypress Code Coverage

Cypress Code Coverage Performance

The problem

A Cypress.io test suite with Istanbul-instrumented code took 30 minutes to run instead of 10. For context, we were using https://github.com/cypress-io/code-coverage.

Debugging

Some overhead is expected with each spec. Collecting coverage will increase the overall run time, surely. But in debug runs (setting DEBUG=code-coverage), we saw an unexpected 31 second overhead running just one spec:

code-coverage NYC file /app/e2e/.nyc_output/out.json has 739 key(s) +13s
code-coverage 1 key /app/frontend/src/config.js file path /app/frontend/src/config.js +1ms
code-coverage 2 key /app/frontend/src/util/logging.js file path /app/frontend/src/util/logging.js +0ms
code-coverage 3 key /app/frontend/src/domains/test-sites/constants.js file path /app/frontend/src/domains/test-sites/constants.js +0ms
code-coverage in file /app/e2e/.nyc_output/out.json all files are not found? false +32ms
code-coverage NYC file /app/e2e/.nyc_output/out.json has 739 key(s) +32ms
code-coverage calling NYC reporter with options { 'report-dir': '/app/e2e/coverage', reporter: [ 'lcov', 'clover', 'json', 'json-summary' ], extension: [ '.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx' ], excludeAfterRemap: false, 'temp-dir': '/app/e2e/.nyc_output', tempDir: '/app/e2e/.nyc_output', reportDir: '/app/e2e/coverage' } +0ms
code-coverage current working directory is /app/e2e +0ms
code-coverage after reporting, returning the report folder name /app/e2e/coverage +31s

Resolution?

What was it doing, exactly?

After some digging into the plugin code, we found a task in the code coverage plugin calling nyc.report() after each spec file. But we don't need to generate a report until after all specs are done.

Can it be improved with minimal effort?

The code-coverage plugin code is a project dependency in node_modules, so we couldn't easily edit it. Luckily, though, we saw that there's a custom coverage report hook with npm scripts that we can override, bypassing the report code entirely:

"scripts": {
  "coverage:report": "echo 'skipped'"
},
"nyc": {
  "excludeAfterRemap": false
}

After the workaround, the test suite runs in 15 minutes instead of 30. Progress!

Note: we generated the coverage report manually after the Cypress run via npx nyc report --reporter=html --reporter=text-summary. It took about 10 seconds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment