Comprehensive Guide to Mochawesome for Efficient Test Reporting and Debugging

Introduction to Mochawesome

Mochawesome is a powerful reporter for Mocha tests, designed to generate visually appealing and comprehensive HTML/CSS reports. With an array of useful APIs, Mochawesome allows developers to improve their testing workflow by providing detailed insights into their test cases.

API Overview

Mochawesome comes with several APIs that can be used to customize and enhance the test reports. Below are examples of some key APIs:

Basic Usage

const Mocha = require('mocha');
const mochawesome = require('mochawesome');

const mocha = new Mocha({
  reporter: 'mochawesome',
  reporterOptions: {
    reportFilename: 'report'
  }
});

mocha.addFile('./test/example.spec.js');
mocha.run();

Setting Custom Report Path

const mocha = new Mocha({
  reporter: 'mochawesome',
  reporterOptions: {
    reportDir: './custom-report',
    reportFilename: 'custom-report-file'
  }
});

Embedding Screenshots

const mocha = new Mocha({
  reporter: 'mochawesome',
  reporterOptions: {
    inlineAssets: true
  }
});

// Add screenshots to report
// Add the screenshot path to the appropriate test using below approach:
describe('My Test Suite', () => {
  it('should take a screenshot', function() {
    this.test.context = {
      title: this.test.title,
      value: [
        `screenshot`
      ]
    };
  });
});

Generating JSON Reports

const mocha = new Mocha({
  reporter: 'mochawesome',
  reporterOptions: {
    reportDir: './report',
    reportFilename: 'report',
    json: true
  }
});

Including Test Meta Data

const mocha = new Mocha({
  reporter: 'mochawesome',
  reporterOptions: {
    reportDir: './report',
    reportFilename: 'report',
    quiet: true
  }
});

describe('Meta Data Test', () => {
  it('should include metadata', function() {
    this.test.context = { metadata: 'This is a meta data for the test'};
  });
});

Ignoring Certain Tests in Reports

const mocha = new Mocha({
  reporter: 'mochawesome',
  reporterOptions: {
    ignoreSkipped: true
  }
});

Enabling Custom Styles

const mocha = new Mocha({
  reporter: 'mochawesome',
  reporterOptions: {
        reportDir: './report',
        reportTitle: 'My Custom Report',
        inlineAssets: true,
        enableCharts: true
  }
});

App Example Using Introduced APIs

Here is an example to create an application utilizing various Mochawesome APIs:

// app.js
const { exec } = require('child_process');

// Run the test suite and generate report
exec('mocha --reporter mochawesome --reporter-options reportDir=custom-report,json=true,inlineAssets=true', (err, stdout, stderr) => {
  if (err) {
    console.error(`Error occurred: ${err.message}`);
    return;
  }
  console.log('Test suite completed, check the custom-report directory for results.');
});

// example.spec.js
describe('Example Test Suite', () => {
  it('Test Case 1', function() {
    this.test.context = { metadata: 'Metadata for Test Case 1' };
    this.test.context = {
      title: 'Test Case 1',
      value: [
        `screenshot`
      ]
    };
    // assertions
  });

  it('Test Case 2', function() {
    this.test.context = { metadata: 'Metadata for Test Case 2' };
    // assertions
  });
});

Run the application with:

node app.js

Check the custom-report directory for the HTML and JSON test reports.

For more information, visit the Mochawesome GitHub page.

Hash: 2258526ed38d3229f6f6c96799813797bfc3b76a15e6ff869a4cd3fc46462027

Leave a Reply

Your email address will not be published. Required fields are marked *