Mutation Testing Metrics Utilities

3.7.3 · active · verified Sun Apr 19

The `mutation-testing-metrics` package provides a set of utility functions designed to calculate and aggregate mutation testing metrics from mutation test reports. Currently at version 3.7.3, it is an actively maintained component within the Stryker Mutator ecosystem, with a release cadence that appears to be every few months, addressing bug fixes and minor feature enhancements. Its key differentiators include the ability to programmatically process `MutationTestResult` objects, perform detailed metric calculations, and aggregate multiple module-level reports into a single, consolidated view. This enables developers and CI/CD pipelines to deeply analyze the effectiveness of their test suites, going beyond simple code coverage by evaluating how well tests detect intentionally introduced faults (mutants). It is built with TypeScript, providing strong type guarantees for its API.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `calculateMutationTestMetrics` to process a `MutationTestResult` object and output key metrics like total mutants, killed, survived, and the overall mutation score.

import { MetricsResult, calculateMutationTestMetrics } from 'mutation-testing-metrics';
import type { MutationTestResult } from 'mutation-testing-report-schema';

// In a real scenario, mutationTestReport would be loaded from a file or generated
const mutationTestReport: MutationTestResult = {
  $schema: 'https://raw.githubusercontent.com/stryker-mutator/mutation-testing-elements/master/packages/report-schema/src/mutation-testing-report-schema.json',
  schemaVersion: '1.4',
  thresholds: {
    high: 80,
    low: 60
  },
  files: {
    'src/calculator.js': {
      language: 'javascript',
      mutants: [
        {
          id: '1',
          mutatorName: 'BinaryExpression',
          replacement: 'a - b',
          location: { start: { line: 1, column: 15 }, end: { line: 1, column: 20 } },
          status: 'Survived'
        },
        {
          id: '2',
          mutatorName: 'BinaryExpression',
          replacement: 'a / b',
          location: { start: { line: 1, column: 15 }, end: { line: 1, column: 20 } },
          status: 'Killed'
        }
      ],
      source: 'const add = (a, b) => a + b;'
    },
    'src/subtract.js': {
      language: 'javascript',
      mutants: [
        {
          id: '3',
          mutatorName: 'BinaryExpression',
          replacement: 'a + b',
          location: { start: { line: 1, column: 18 }, end: { line: 1, column: 23 } },
          status: 'NoCoverage'
        }
      ],
      source: 'const subtract = (a, b) => a - b;'
    }
  }
};

const result: MetricsResult = calculateMutationTestMetrics(mutationTestReport);

console.log('--- Mutation Test Metrics ---');
console.log('Total mutants:', result.metrics.totalMutants);
console.log('Killed mutants:', result.metrics.killed);
console.log('Survived mutants:', result.metrics.survived);
console.log('No Coverage mutants:', result.metrics.noCoverage);
console.log('Mutation score:', result.metrics.mutationScore.toFixed(2) + '%');

view raw JSON →