How can I serve an instrumented folder to run my Cypress tests against?

Viewed 941

Viewing the instrumenting code section in the Cypress documentation: https://docs.cypress.io/guides/tooling/code-coverage.html they state that you can serve the instrumented folder. 'We can see the counters if we serve the instrumented folder instead of src and open the application.'

What is the best approach for serving this? My understanding is that instrumented code only contains files where instrumentation can be added such as .js, and many files such as .html will not included in the folder, how can I serve the application with instrumentation so that I can test against it using Cypress?

1 Answers

Instrumenting the code is dependent on the way you serve the app, but for plain javascript (not bundled) see this blog Code Coverage for End-to-end Tests

The cp command answers your question about how to handle non-js file. Essentially everything ends up (duplicated and instrumented) in build/src.

package.json

{
  "scripts": {
    "build": "npm run instrument && npm run cp",
    "preinstrument": "npm run clean",
    "instrument": "nyc instrument --compact false src build/src",
    "cp": "cp src/*.css build/src && cp src/*.png build/src && cp index.html build",
    "clean": "rm -rf build .nyc_output || true",
    "report:coverage": "nyc report --reporter=html"
  }
}
Related