/// <reference types="cypress" />
import { existsSync, readFileSync } from 'fs';
import path from 'path';
describe('Mochawesome Reporter Setup Test', () => {
const reportDir = 'cypress/reports/mochawesome';
const jsonReport = path.join(reportDir, 'mochawesome.json');
const htmlReport = path.join(reportDir, 'mochawesome.html');
before(() => {
// Clean reports folder before tests
if (existsSync(jsonReport)) {
// Delete files if needed - simplified here
}
});
it('runs a sample test', () => {
cy.visit('https://example.cypress.io');
cy.contains('type').click();
cy.url().should('include', '/commands/actions');
cy.get('.action-email').type('test@example.com').should('have.value', 'test@example.com');
});
it('verifies mochawesome report files exist and contain test info', () => {
cy.wait(1000); // wait for report generation
cy.task('fileExists', jsonReport).should('be.true');
cy.task('fileExists', htmlReport).should('be.true');
cy.task('readFile', htmlReport).then((content) => {
expect(content).to.include('Mochawesome Report');
expect(content).to.include('Mochawesome Reporter Setup Test');
expect(content).to.match(/<span class="pass">1</span>/);
});
});
});
// In cypress.config.js
// import { defineConfig } from 'cypress';
// export default defineConfig({
// reporter: 'mochawesome',
// reporterOptions: {
// reportDir: 'cypress/reports/mochawesome',
// overwrite: false,
// html: true,
// json: true
// },
// e2e: {
// setupNodeEvents(on, config) {
// const fs = require('fs');
// on('task', {
// fileExists(filePath) {
// return fs.existsSync(filePath);
// },
// readFile(filePath) {
// return fs.readFileSync(filePath, 'utf8');
// }
// });
// }
// }
// });
// npm scripts example in package.json
// "scripts": {
// "test": "cypress run"
// }This test suite does two things:
- Runs a simple Cypress test visiting a page and typing text.
- Checks that the mochawesome report files (JSON and HTML) are created after the test run.
The cypress.config.js is configured to use mochawesome as the reporter with JSON and HTML output enabled in a specific folder.
We use Cypress tasks to check if the report files exist and to read the HTML report content. This allows us to assert that the report contains the test suite name and shows the test pass count.
This setup ensures the mochawesome reporter is correctly generating reports and that the reports reflect the test results.