Skip to content

Commit

Permalink
feat(jest-runner): switch mutants using env (#2416)
Browse files Browse the repository at this point in the history
Pass the active mutant id through the env vars rather than config. This allows the jest transpiler files to be cached.

Co-authored-by: Gareth Parker <gareth.parker@ros.gov.uk>
Co-authored-by: Nico Jansen <jansennico@gmail.com>
  • Loading branch information
3 people committed Aug 28, 2020
1 parent 4d3ae97 commit cad01ba
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 88 deletions.
27 changes: 6 additions & 21 deletions packages/jest-runner/src/JestTestRunner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StrykerOptions, INSTRUMENTER_CONSTANTS, Mutant } from '@stryker-mutator/api/core';
import { StrykerOptions, INSTRUMENTER_CONSTANTS } from '@stryker-mutator/api/core';
import { Logger } from '@stryker-mutator/api/logging';
import { commonTokens, Injector, PluginContext, tokens } from '@stryker-mutator/api/plugin';
import {
Expand Down Expand Up @@ -37,8 +37,6 @@ jestTestRunnerFactory.inject = tokens(commonTokens.injector);

export default class JestTestRunner implements TestRunner2 {
private readonly jestConfig: jest.Config.InitialOptions;
private mutantRunJestConfigCache: jest.Config.InitialOptions | undefined;

private readonly enableFindRelatedTests: boolean;

public static inject = tokens(commonTokens.logger, commonTokens.options, processEnvToken, jestTestAdapterToken, configLoaderToken);
Expand Down Expand Up @@ -73,31 +71,18 @@ export default class JestTestRunner implements TestRunner2 {
}

public dryRun(): Promise<DryRunResult> {
return this.run(this.jestConfig);
return this.run();
}
public async mutantRun({ activeMutant, sandboxFileName }: MutantRunOptions): Promise<MutantRunResult> {
const fileUnderTest = this.enableFindRelatedTests ? sandboxFileName : undefined;
const dryRunResult = await this.run(this.getMutantRunOptions(activeMutant), fileUnderTest);
process.env[INSTRUMENTER_CONSTANTS.ACTIVE_MUTANT_ENV_VARIABLE] = activeMutant.id.toString();
const dryRunResult = await this.run(fileUnderTest);
return toMutantRunResult(dryRunResult);
}

private getMutantRunOptions(activeMutant: Mutant): jest.Config.InitialOptions {
if (!this.mutantRunJestConfigCache) {
this.mutantRunJestConfigCache = {
...this.jestConfig,
globals: {
...this.jestConfig.globals,
},
};
}
this.mutantRunJestConfigCache.globals![INSTRUMENTER_CONSTANTS.ACTIVE_MUTANT] = activeMutant.id;
return this.mutantRunJestConfigCache;
}

private async run(config: jest.Config.InitialOptions, fileUnderTest: string | undefined = undefined): Promise<DryRunResult> {
private async run(fileUnderTest: string | undefined = undefined): Promise<DryRunResult> {
this.setEnv();
const all = await this.jestTestAdapter.run(config, process.cwd(), fileUnderTest);

const all = await this.jestTestAdapter.run(this.jestConfig, process.cwd(), fileUnderTest);
return this.collectRunResult(all.results);
}

Expand Down
21 changes: 20 additions & 1 deletion packages/jest-runner/test/integration/JestTestRunner.it.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ describe(`${JestTestRunner.name} integration test`, () => {
'Add should be able to add one to a number',
'Add should be able negate a number',
'Add should be able to recognize a negative number',
'Add should be able to recognize that 0 is not a negative number',
'Circle should have a circumference of 2PI when the radius is 1',
];

Expand Down Expand Up @@ -126,6 +125,26 @@ describe(`${JestTestRunner.name} integration test`, () => {

assertions.expectSurvived(runResult);
});

it('should be able to let a mutant survive after killing mutant 1', async () => {
// Arrange
const exampleProjectRoot = getProjectRoot('exampleProject');
process.chdir(getProjectRoot('exampleProject'));
const jestTestRunner = createSut();
const mutantRunOptions = factory.mutantRunOptions({
sandboxFileName: require.resolve(path.resolve(exampleProjectRoot, 'src', 'Add.js')),
});
mutantRunOptions.activeMutant.id = 1;

// Act
const firstResult = await jestTestRunner.mutantRun(mutantRunOptions);
mutantRunOptions.activeMutant.id = 5;
const secondResult = await jestTestRunner.mutantRun(mutantRunOptions);

// Assert
assertions.expectKilled(firstResult);
assertions.expectSurvived(secondResult);
});
});
});

Expand Down
11 changes: 3 additions & 8 deletions packages/jest-runner/test/unit/JestTestRunner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,10 @@ describe(JestTestRunner.name, () => {
expect(jestTestAdapterMock.run).calledWithExactly(sinon.match.object, sinon.match.string, undefined);
});

it('should set the active mutant in globals', async () => {
it('should set the active mutant in environment variables', async () => {
const sut = createSut();
await sut.mutantRun(factory.mutantRunOptions({ activeMutant: factory.mutant({ id: 25 }) }));
expect(jestTestAdapterMock.run).calledWithMatch(
sinon.match({
globals: { [INSTRUMENTER_CONSTANTS.ACTIVE_MUTANT]: 25 },
}),
sinon.match.string
);
expect(process.env[INSTRUMENTER_CONSTANTS.ACTIVE_MUTANT_ENV_VARIABLE]).to.equal('25');
});

it('should allow for other globals', async () => {
Expand All @@ -245,7 +240,7 @@ describe(JestTestRunner.name, () => {
await sut.mutantRun(factory.mutantRunOptions({ activeMutant: factory.mutant({ id: 25 }) }));
expect(jestTestAdapterMock.run).calledWithMatch(
sinon.match({
globals: { foo: 'bar', [INSTRUMENTER_CONSTANTS.ACTIVE_MUTANT]: 25 },
globals: { foo: 'bar' },
}),
sinon.match.string
);
Expand Down
25 changes: 8 additions & 17 deletions packages/jest-runner/testResources/exampleProject/src/Add.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
// @ts-nocheck
var __global_69fa48 = function (g) {
g.__mutantCoverage__ = g.__mutantCoverage__ || {
static: {},
perTest: {}
};

var __global_69fa48 = (function(g){
if (g.__activeMutant__ === undefined && g.process && g.process.env && g.process.env.__STRYKER_ACTIVE_MUTANT__) {
g.__activeMutant__ = Number(g.process.env.__STRYKER_ACTIVE_MUTANT__);
}
g.__mutantCoverage__ = g.__mutantCoverage__ || { static: {}, perTest: {} };
g.__coverMutant__ = g.__coverMutant__ || function () {
var c = g.__mutantCoverage__.static;

if (g.__currentTestId__) {
c = g.__mutantCoverage__.perTest[g.__currentTestId__] = g.__mutantCoverage__.perTest[g.__currentTestId__] || {};
c = g.__mutantCoverage__.perTest[g.__currentTestId__] = g.__mutantCoverage__.perTest[g.__currentTestId__] || {};
}

var a = arguments;

for (var i = 0; i < a.length; i++) {
for(var i=0; i < a.length; i++){
c[a[i]] = (c[a[i]] || 0) + 1;
}
};

if(g.process && g.process.env && g.process.env.__ACTIVE_MUTANT__) {
g.__activeMutant__ = Number(g.process.env.__ACTIVE_MUTANT__);
}

return g;
}(new Function("return this")());
})(new Function("return this")())

exports.add = function (num1, num2) {
switch (__global_69fa48.__activeMutant__) {
Expand Down
25 changes: 8 additions & 17 deletions packages/jest-runner/testResources/exampleProject/src/Circle.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
// @ts-nocheck
var __global_69fa48 = function (g) {
g.__mutantCoverage__ = g.__mutantCoverage__ || {
static: {},
perTest: {}
};

var __global_69fa48 = (function(g){
if (g.__activeMutant__ === undefined && g.process && g.process.env && g.process.env.__STRYKER_ACTIVE_MUTANT__) {
g.__activeMutant__ = Number(g.process.env.__STRYKER_ACTIVE_MUTANT__);
}
g.__mutantCoverage__ = g.__mutantCoverage__ || { static: {}, perTest: {} };
g.__coverMutant__ = g.__coverMutant__ || function () {
var c = g.__mutantCoverage__.static;

if (g.__currentTestId__) {
c = g.__mutantCoverage__.perTest[g.__currentTestId__] = g.__mutantCoverage__.perTest[g.__currentTestId__] || {};
c = g.__mutantCoverage__.perTest[g.__currentTestId__] = g.__mutantCoverage__.perTest[g.__currentTestId__] || {};
}

var a = arguments;

for (var i = 0; i < a.length; i++) {
for(var i=0; i < a.length; i++){
c[a[i]] = (c[a[i]] || 0) + 1;
}
};

if(g.process && g.process.env && g.process.env.__ACTIVE_MUTANT__) {
g.__activeMutant__ = Number(g.process.env.__ACTIVE_MUTANT__);
}

return g;
}(new Function("return this")());
})(new Function("return this")())

exports.getCircumference = function (radius) {
switch (__global_69fa48.__activeMutant__) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,4 @@ describe('Add', function() {
expect(isNegative).toBe(true);
});

it('should be able to recognize that 0 is not a negative number', function() {
var number = 0;

var isNegative = isNegativeNumber(number);

expect(isNegative).toBe(false);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,4 @@ describe('Add', function() {
expect(isNegative).toBe(true);
});

it('should be able to recognize that 0 is not a negative number', function() {
var number = 0;

var isNegative = isNegativeNumber(number);

expect(isNegative).toBe(false);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,4 @@ describe('Add', function() {
expect(isNegative).toBe(true);
});

it('should be able to recognize that 0 is not a negative number', function() {
var number = 0;

var isNegative = isNegativeNumber(number);

expect(isNegative).toBe(false);
});
});
});

0 comments on commit cad01ba

Please sign in to comment.