This is the bug repro for cypress-io/code-coverage#472.
It is a minimal example with the following requirements:
- Is an ESM module
- i.e.
"type": "module"is explicitly marked in thepackage.json - appropriate Cypress files need to be marked as
.cjsin order for the tests to run. Incypress/plugins/index.cjsand fields incypress.json.
- i.e.
- Uses component testing (i.e.
@cypress/reactand usingopen-ctandrun-cttest runners). - Uses code coverage (i.e.
@cypress/code-coverageandbabel-plugin-istanbulbased on the official docs for instrumentation).
The requirements above are needed in the actual bug faced in the project, so the repo is capturing the minimal configuration of reproducing the same bug.
npm i
npm test
The test runner runs fine, and tests the code correctly.
Code coverage to output something meaningful in .nyc_output/out.json
.nyc_output/out.json is an empty object. The test runner complains that the code is not instrumented.
To give context on the actual project where I first encountered the bug:
- I'm building a component library (does not actually use React).
- The code is authored as an ESM module (hence the repro repo being explicit about it in
package.jsonand also having to use.cjsfor some Cypress related setup to get the test runner working). - I'm using
@cypress/reactand the component test runner for its delightful testing experience. That has been superb, it's just the last stretch of figuring out coverage that I'm hitting a wall. As a result, while React is not a dependency of the library I'm writing, it is a dev dependency for my tests. I definitely do not usecreate-react-app, and therefore have a custom.webpack.config.cjsand.babelrcin thecypress/folder.
I don't fully understand why code coverage is not working, but some suspicions I have if they are helpful for the debugger:
- Maybe it's related to my package being ESM-only? (i.e.
"type": "module"required inpackage.json). - Maybe
@cypress/reactdoes not interoperate well with@cypress/code-coveragewith ESM (as mentioned in the earlier point)? - Maybe it's related to peer dependency versions that I have installed (I simply installed the latest).
- Maybe I'm completely missing something despite spending days iterating on variations from the official docs (which has a very simple setup!).