Cypress mochawesome report only shows the last spec
You ran cypress run across twelve spec files. The mochawesome report
opens fine — and contains exactly one of them. Run it again and it is a
different one.
Nothing is lost by the tests. The reports are overwriting each other.
Why it happens
Cypress does not run your suite as one Mocha run. It runs each spec file as its own run, in a fresh browser context, and starts the reporter from scratch every time.
mochawesome writes mochawesome.json and mochawesome.html with
overwrite: true by default. Spec 1 writes the file, spec 2 replaces
it, and the last spec to finish is the report you see. In a parallel or
randomised order, "last" changes from run to run.
Fix 1: one JSON per spec, merge, render
Tell mochawesome to keep every spec's output, as JSON only:
// cypress.config.js
module.exports = defineConfig({
reporter: "mochawesome",
reporterOptions: {
reportDir: "cypress/results",
overwrite: false,
html: false,
json: true,
},
});
With overwrite: false the files come out as mochawesome.json,
mochawesome_001.json, mochawesome_002.json and so on. Then merge
them and render once:
rm -rf cypress/results
npx cypress run || true
npx mochawesome-merge "cypress/results/*.json" -o merged.json
npx marge merged.json --reportDir mochawesome-report --inline
marge is the CLI of mochawesome-report-generator. --inline puts the
CSS and JS into the HTML instead of an assets/ folder beside it, which
matters as soon as the file leaves the runner.
Write merged.json outside the results folder. Put it inside and the
next run's glob merges the previous merged file back in, doubling every
test.
Fix 2: cypress-mochawesome-reporter
If you would rather not script the merge, cypress-mochawesome-reporter
does it inside Cypress:
// cypress.config.js
module.exports = defineConfig({
reporter: "cypress-mochawesome-reporter",
reporterOptions: { embeddedScreenshots: true, inlineAssets: true },
e2e: {
setupNodeEvents(on) {
require("cypress-mochawesome-reporter/plugin")(on);
},
},
});
// cypress/support/e2e.js
import "cypress-mochawesome-reporter/register";
It merges the per-spec results when cypress run finishes and, with
embeddedScreenshots, puts the failure screenshot next to the failing
test — which the plain mochawesome setup does not do on its own. Miss
either the plugin line or the support import and you are back to one
spec, or to no report at all.
The other ways the merge goes wrong
| Symptom | Cause | Fix |
|---|---|---|
| Tests from yesterday in today's report | Old JSON files still in the results folder | rm -rf cypress/results before cypress run |
| Every test appears twice | merged.json written inside the globbed dir |
Write the merged file elsewhere |
| No report when a test fails in CI | cypress run exits 1, later steps skipped |
if: always() / when: always on merge + render |
| Report has no styling once downloaded | assets/ folder left behind |
marge --inline, or upload the whole folder |
No files found from mochawesome-merge |
Unquoted glob expanded by the shell, or html: true only |
Quote the glob; set json: true |
| Parallel CI shards each have a fragment | Each machine merged only its own specs | Collect every shard's JSON, merge once at the end |
In CI
- name: Cypress
run: npx cypress run
- name: Build mochawesome report
if: always()
run: |
npx mochawesome-merge "cypress/results/*.json" -o merged.json
npx marge merged.json --reportDir mochawesome-report --inline
Then get it read
A merged report sitting in a zipped build artifact is still one download and an unzip away from anyone looking at it, and it expires under retention.
Publish mochawesome-report/ instead and the report is a link that opens
in the browser. Publish to the same report on every run and each build
becomes a revision, so the run where a flaky test
failed is still there next week. Reviewers comment on the failing test
itself instead of screenshotting it into chat (comment on
HTML). Scripts run in a sandboxed iframe, so the
mochawesome filters and charts keep working.
Limits: entry HTML 5 MB; assets 25 MB per file, 250 MB and 500 files per report. A report with embedded base64 screenshots grows fast — keep an eye on the 5 MB entry limit, or let screenshots upload as separate files.
Try it
Comma is free — unlimited reports, unlimited commenters, unlimited revision history.
Related
- Share a mochawesome report · Cypress reports
- pytest-html report has no CSS — the same
assets/problem - Flaky test · Test quarantine
- The CI artifact link expired
- Publish from CI