What is @stryker-mutator/core?
@stryker-mutator/core is a mutation testing framework for JavaScript and TypeScript projects. It helps developers improve their test suites by introducing small changes (mutations) to the code and checking if the existing tests can detect these changes. If a test fails due to a mutation, it means the test suite is effective in catching potential bugs.
What are @stryker-mutator/core's main functionalities?
Mutation Testing
This code demonstrates how to set up and run mutation testing using Stryker. It configures Stryker to mutate JavaScript files in the 'src' directory, use Jest as the test runner, and generate reports in HTML and clear-text formats.
const { Stryker } = require('@stryker-mutator/core');
const stryker = new Stryker({
mutate: ['src/**/*.js'],
testRunner: 'jest',
reporters: ['html', 'clear-text'],
coverageAnalysis: 'off'
});
stryker.runMutationTest().then(() => console.log('Mutation testing completed.'));
Configuration
This is a sample Stryker configuration file. It specifies which files to mutate, the test runner to use, the reporters for output, and other settings. This configuration is essential for customizing how Stryker runs mutation tests.
module.exports = {
mutate: ['src/**/*.js'],
mutator: 'javascript',
packageManager: 'npm',
reporters: ['progress', 'clear-text', 'html'],
testRunner: 'jest',
transpilers: [],
testFramework: 'jest',
coverageAnalysis: 'off'
};
Other packages similar to @stryker-mutator/core
humbug
Humbug is a mutation testing tool for PHP. It operates similarly to Stryker by mutating code and checking the effectiveness of the test suite. Humbug is focused on PHP, providing a specialized solution for PHP developers, unlike Stryker, which targets JavaScript and TypeScript.
StrykerJS
Professor X: For someone who hates mutants... you certainly keep some strange company.
William Stryker: Oh, they serve their purpose... as long as they can be controlled.
Introduction
For an introduction to mutation testing and StrykerJS features, see stryker-mutator.io.
Getting started
Please follow the quickstart on the website.
For small js projects, you can try the following command:
npm install --save-dev @stryker-mutator/core
# Only for small projects:
npx stryker run
It will run stryker with default values:
- Uses
npm test
as your test command - Searches for files to mutate in the
lib
and src
directories
Usage
$ npx stryker <command> [options] [configFile]
See usage on stryker-mutator.io
Supported mutators
See our website for the list of currently supported mutators.
Configuration
See configuration on stryker-mutator.io.
Programmatic use
Stryker can also be used programmatically from nodejs. It exports 2 classes for you to use: Stryker
and StrykerCli
.
import { Stryker, StrykerCli } from '@stryker-mutator/core';
Both classes can be used to run Stryker. The main difference is that Stryker
is a slightly more low-level approach, while StrykerCli
is the straight up CLI api.
In this example you can see how to use both.
async function main() {
new StrykerCli(process.argv ).run();
const stryker = new Stryker({ concurrency: 4 } );
const mutantResults = await stryker.runMutationTest();
}
Stryker is written in TypeScript, so it is recommended to use Typescript as well to get the best developer experience.