The extendable JavaScript mutation testing framework
npx @tessl/cli install tessl/npm-stryker-mutator--core@9.1.0The extendable JavaScript mutation testing framework core package. StrykerJS systematically introduces small code changes (mutations) to test files to verify the effectiveness of your test suite. It provides a robust plugin architecture supporting multiple test runners and comprehensive reporting.
npm install @stryker-mutator/coreimport { Stryker, StrykerCli } from "@stryker-mutator/core";Default import for backward compatibility:
import Stryker from "@stryker-mutator/core";Mixed import:
import Stryker, { StrykerCli } from "@stryker-mutator/core";import { Stryker } from "@stryker-mutator/core";
import { PartialStrykerOptions, MutantResult } from "@stryker-mutator/api/core";
// Configure Stryker with options
const options: PartialStrykerOptions = {
testRunner: 'jest',
mutate: ['src/**/*.js', '!src/index.js'],
reporters: ['html', 'clear-text'],
coverageAnalysis: 'perTest'
};
// Create and run mutation testing
const stryker = new Stryker(options);
const results: MutantResult[] = await stryker.runMutationTest();
console.log(`Found ${results.length} mutants`);import { StrykerCli } from "@stryker-mutator/core";
// Create CLI instance with command line arguments
const cli = new StrykerCli(process.argv);
cli.run();Stryker Core is built around several key concepts:
The package contains extensive internal functionality including process executors, reporters, configuration system, and DI container, but these are deliberately not exposed as public APIs.
Main mutation testing orchestrator class that executes the complete mutation testing process.
class Stryker {
/**
* Creates a new Stryker instance
* @param cliOptions - Configuration options for mutation testing
* @param injectorFactory - Dependency injection factory (for testing purposes)
*/
constructor(
cliOptions: PartialStrykerOptions,
injectorFactory?: Function
);
/**
* Executes the complete mutation testing process
* @returns Promise resolving to array of mutant test results
*/
runMutationTest(): Promise<MutantResult[]>;
}Usage Example:
import { Stryker } from "@stryker-mutator/core";
const stryker = new Stryker({
testRunner: 'mocha',
mutate: ['src/**/*.ts'],
reporters: ['progress', 'html'],
thresholds: { high: 80, low: 60, break: 50 }
});
const results = await stryker.runMutationTest();
console.log(`Mutation testing completed with ${results.length} mutants`);Command-line interface implementation providing CLI access to Stryker functionality.
import { Command } from 'commander';
class StrykerCli {
/**
* Creates a new StrykerCli instance
* @param argv - Command line arguments array
* @param program - Commander program instance (optional)
* @param runMutationTest - Mutation test runner function (optional)
* @param runMutationTestingServer - Mutation server runner function (optional)
*/
constructor(
argv: string[],
program?: Command,
runMutationTest?: (options: PartialStrykerOptions) => Promise<MutantResult[]>,
runMutationTestingServer?: (options: PartialStrykerOptions) => Promise<void>
);
/**
* Parses command line arguments and executes the appropriate command
* Supports commands: run, init, runServer
*/
run(): void;
}Usage Example:
import { StrykerCli } from "@stryker-mutator/core";
// Create CLI with process arguments
const cli = new StrykerCli(process.argv);
// Run the CLI - will parse arguments and execute appropriate command
cli.run();Supported CLI Commands:
run - Execute mutation testinginit - Initialize Stryker configuration for projectrunServer - Start mutation testing server (implements mutation-server-protocol)The package relies on types from @stryker-mutator/api/core:
// Key configuration interface - comprehensive configuration options
interface PartialStrykerOptions {
// Core testing configuration
testRunner?: string;
mutate?: string[];
reporters?: string[];
coverageAnalysis?: 'off' | 'all' | 'perTest';
// File and pattern configuration
files?: string[];
ignorePatterns?: string[];
ignoreStatic?: boolean;
// Test execution configuration
buildCommand?: string;
dryRunOnly?: boolean;
allowEmpty?: boolean;
checkers?: string[];
checkerNodeArgs?: string[];
testRunnerNodeArgs?: string[];
// Performance and concurrency
concurrency?: number;
maxConcurrentTestRunners?: number;
maxTestRunnerReuse?: number;
disableBail?: boolean;
// Timeout configuration
timeoutMS?: number;
timeoutFactor?: number;
dryRunTimeoutMinutes?: number;
// Incremental mutation testing
incremental?: boolean;
incrementalFile?: string;
force?: boolean;
// Thresholds and reporting
thresholds?: {
high?: number;
low?: number;
break?: number;
};
dashboard?: {
project?: string;
version?: string;
module?: string;
baseUrl?: string;
reportType?: 'full' | 'mutationScore';
};
// Directory and file management
inPlace?: boolean;
tempDirName?: string;
cleanTempDir?: boolean | 'always';
// Logging configuration
logLevel?: 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace' | 'off';
fileLogLevel?: 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace' | 'off';
allowConsoleColors?: boolean;
// Plugin configuration
plugins?: string[];
appendPlugins?: string[];
// Configuration file
configFile?: string;
// ... many other advanced configuration properties
}
// Result of mutation testing - key properties shown
interface MutantResult {
id: string;
mutatorName: string;
replacement: string;
fileName: string;
status: MutantStatus;
statusReason?: string;
// ... additional result properties from mutation-testing-report-schema
}
// Mutation status enumeration
type MutantStatus = 'Killed' | 'Survived' | 'NoCoverage' | 'TimedOut' | 'CompileError' | 'RuntimeError';The package provides the stryker CLI binary as the primary interface for mutation testing:
# Run mutation testing (most common usage)
npx stryker run
# Initialize Stryker configuration
npx stryker init
# Start mutation testing server
npx stryker runServer
# Direct binary execution
./node_modules/.bin/stryker run
# Via npm script (add to package.json)
npm run stryker
# Global installation
npm install -g @stryker-mutator/core
stryker runCommon CLI Options:
# Specify files to mutate
stryker run --mutate "src/**/*.js" --testRunner jest
# Set coverage analysis strategy
stryker run --coverageAnalysis perTest
# Configure reporters
stryker run --reporters html,clear-text,progress
# Set concurrency
stryker run --concurrency 4
# Incremental mutation testing
stryker run --incrementalThe package handles errors through several mechanisms:
runMutationTest() returns rejected promises for mutation testing failuresimport { Stryker } from "@stryker-mutator/core";
try {
const stryker = new Stryker({
testRunner: 'jest',
mutate: ['src/**/*.js']
});
const results = await stryker.runMutationTest();
console.log(`Mutation testing completed: ${results.length} mutants tested`);
} catch (error) {
if (error.name === 'ConfigError') {
console.error('Configuration error:', error.message);
} else if (error.message.includes('Node.js version')) {
console.error('Node.js version incompatibility:', error.message);
} else {
console.error('Mutation testing failed:', error.message);
}
process.exit(1);
}