cypress-mochawesome-reporter-v2
v2.0.5
Published
Zero config Mochawesome reporter for Cypress with screenshots
Readme
cypress-mochawesome-reporter-v2
Warning
This is a fork of cypress-mochawesome-reporter the only difference in this version is the current fix to support not thowing an error when running Cypress tests in parallel for non json reports found when generating the report On this fork I've also add a new config option called addScreenshotToHTML which we can pass to reporterOptions as false value to not add the screenshots when we are already adding in some different way
Zero config Mochawesome reporter for Cypress with screenshots attached to tests.
Cypress compatibility
| reporter version | cypress version | reporter branch |
| ---------------- | ----------------------------------------------------------------------------- | ----------------------------------------------------------------------- |
| v4 | node >= 22>= 6.7.0 >= 6.2.0 with experimentalRunEvents: true | main |
| v3 | node >= 14>= 6.7.0 >= 6.2.0 with experimentalRunEvents: true | v3 |
| v2 | >= 6.7.0 >= 6.2.0 with experimentalRunEvents: true | v2 |
| v1 | >= 4.0.0 | v1 |
Setup
This setup tutorial works with Cypress >= v10, looking for older version setup? here
install cypress-mochawesome-reporter
npm i --save-dev cypress-mochawesome-reporteror
yarn add -D cypress-mochawesome-reporterChange cypress reporter & setup hooks
Edit config file (
cypress.config.jsby default)const { defineConfig } = require('cypress'); module.exports = defineConfig({ reporter: 'cypress-mochawesome-reporter', e2e: { setupNodeEvents(on, config) { require('cypress-mochawesome-reporter/plugin')(on); }, }, });If you are override
before:runorafter:runhooks, use this:const { defineConfig } = require('cypress'); const { beforeRunHook, afterRunHook } = require('cypress-mochawesome-reporter/lib'); module.exports = defineConfig({ reporter: 'cypress-mochawesome-reporter', e2e: { setupNodeEvents(on, config) { on('before:run', async (details) => { console.log('override before:run'); await beforeRunHook(details); }); on('after:run', async () => { console.log('override after:run'); await afterRunHook(); }); }, }, });Add to
cypress/support/e2e.jsimport 'cypress-mochawesome-reporter/register';(optional, if your are using
cypress-cucumber-preprocessor) Add tocypress/support/step_definitions/index.jsimport 'cypress-mochawesome-reporter/cucumberSupport';⚠️
cypress-cucumber-preprocessoruses the same hooks ascypress-mochawesome-reporter, you also need to install cypress-on-fix. Full example of usingcypress-mochawesome-reporterwithcypress-cucumber-preprocessorcan be found here.run cypress
Custom options
If you want to customize your HTML report with mochawesome-report-generator flags just add the flags you want to reporterOptions
const { defineConfig } = require('cypress');
module.exports = defineConfig({
reporter: 'cypress-mochawesome-reporter',
reporterOptions: {
charts: true,
reportPageTitle: 'custom-title',
embeddedScreenshots: true,
inlineAssets: true,
saveAllAttempts: false,
},
e2e: {
setupNodeEvents(on, config) {
require('cypress-mochawesome-reporter/plugin')(on);
},
},
});Additional reporter options:
| name | type | default | description |
| --------------------- | --------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| embeddedScreenshots | boolean | false | Embedded external screenshots into HTML using base64, use with inlineAssets option to produce a single HTML file |
| ignoreVideos | boolean | false | Will not copy videos recorded by Cypress nor show them in the mochawesome report. Requires that Cypress config option video is set to true for the option to have any effectBecause mochawesome doesn't support context per spec file, each test will have the whole spec file video. More info can be found here |
| videoOnFailOnly | boolean | false | If Videos are recorded and added to the report, setting this to true will add the videos only to tests with failures.Do note that this will NOT cause video's to only record failed tests, just they not be added to passed tests in the mochawesome report |
| quiet | boolean | false | Silence console messages |
| saveAllAttempts | boolean | true | Save screenshots of all test attempts, set to false to save only the last attempt |
| debug | boolean | false | Creates log file with debug data |
| saveJson | boolean | false | Keeps the json file used to create html report |
Add extra information to report
Add extra information to the report manually by using cy.addTestContext() as seen in the simple-typescript example test 2
Examples
- Simple use of
cypress-mochawesome-reporter - Using
cypress-multi-reporters - With
mochawesome-report-generatorflags - Change default screenshots folder in
cypress.json - Using
cypress-mochawesome-reporterwith typescript - Using
cypress-mochawesome-reporterwithcypress-parallel - Using
cypress-mochawesome-reporterwithcypress-cucumber-preprocessor
Run npm i in root directory then:
cd examples/<example-project>
npm i
npm testPublishing
Automated publishing (via GitHub Actions)
This project uses Release Please to automate releases. The workflow is:
- Push commits to
mainusing Conventional Commits format (e.g.,fix: description,feat: description) - Release Please opens a release PR with version bump and changelog
- Merge the release PR
- Release Please creates a GitHub Release, which triggers npm publish automatically
Publishing manually from local
Log in to npm:
npm loginVerify you are logged in with the correct account:
npm whoamiPublish:
npm publish --access public --tag latest
Merging without triggering a release
Release Please only creates release PRs for commits that use fix: or feat: prefixes. To merge changes (like README updates) without triggering a new release, use other conventional commit types:
docs: update README— documentation changeschore: update dependencies— maintenance tasksci: update workflow— CI/CD changesrefactor: reorganize code— code restructuringtest: add unit tests— test changes
These commits will be merged to main without Release Please creating a release PR or bumping the version.
Troubleshooting npm publish
E404 Not Found on publish
npm error 404 Not Found - PUT https://registry.npmjs.org/cypress-mochawesome-reporter-v2 - Not foundThis usually means your npm token or account does not have publish permissions to the package.
Check who owns the package:
npm owner ls cypress-mochawesome-reporter-v2Check which account you are logged in as:
npm whoamiIf
npm whoamireturnsE401 Unauthorizedeven afternpm login, check for a stale token in~/.npmrc:cat ~/.npmrcReplace the old token:
npm config set //registry.npmjs.org/:_authToken YOUR_NEW_TOKENVerify your account is listed as an owner or has read-write collaborator access to the package.
E401 Unauthorized after npm login
If npm login succeeds in the browser but npm whoami returns E401, an old token in ~/.npmrc is overriding the new session. Replace it as shown above.
Cannot publish over previously published version
npm error You cannot publish over the previously published versions: x.x.xThe version in package.json has already been published. Bump the version before publishing again.
GitHub Actions publish fails
If the automated publish fails after a release is created:
- Fix the issue (e.g., update the
NPM_TOKENsecret in the repo settings) - Go to the Actions tab in GitHub
- Find the failed workflow run
- Click "Re-run failed jobs" to retry with the same release outputs
