Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ignore static): prevent leak of hybrid mutants #3443

Merged
merged 8 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,076 changes: 1,069 additions & 1,007 deletions e2e/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"grunt": "~1.4.1",
"jasmine": "~4.0.2",
"jasmine-core": "~4.0.0",
"jest": "~27.4.3",
"jest": "~27.5.1",
"jest-environment-jsdom-sixteen": "~1.0.3",
"karma": "~6.3.9",
"karma-chai": "~0.1.0",
Expand Down
9 changes: 5 additions & 4 deletions e2e/test/coverage-analysis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
"description": "A e2e test for --coverageAnalysis",
"type": "commonjs",
"scripts": {
"test:unit:cucumber": "cucumber-js",
"test:unit:jasmine": "jasmine",
"test:unit:mocha": "mocha",
"test:unit:karma": "karma start",
"test:cucumber": "cucumber-js",
"test:jasmine": "jasmine --config=jasmine.json",
"test:mocha": "mocha",
"test:jest": "jest",
"test:karma": "karma start",
"test": "mocha --no-config --no-package --timeout 0 verify/verify.js"
}
}
4 changes: 4 additions & 0 deletions e2e/test/ignore-static/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"require": ["spec/chai-setup.js"],
"spec": ["spec/*.spec.js"]
}
14 changes: 14 additions & 0 deletions e2e/test/ignore-static/cucumber-features/math.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Feature: Math

Scenario: add
Given input 40
When add with 2
Then the result should be 42

Scenario: myMath.pi
Given input myMath.pi
Then the result should be 3.14

Scenario: MyMath.pi
Given input MyMath.pi
Then the result should be '🥧'
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { Given, When, Then } = require('@cucumber/cucumber');
const { expect } = require('chai');
const { MyMath } = require('../../src/math');

const myMath = new MyMath();

Given('input myMath.pi', function() {
this.value = myMath.pi;
});

Given('input MyMath.pi', function() {
this.value = MyMath.pi;
});

Given('input {int}', function (input) {
this.value = input;
});

When('add with {int}', function (other) {
this.value = myMath.add(this.value, other);
});

Then('the result should be {string}', function (expected) {
expect(this.value).eq(expected);
});
Then('the result should be {float}', function (expected) {
expect(this.value).eq(expected);
});
4 changes: 4 additions & 0 deletions e2e/test/ignore-static/cucumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
default: '--publish-quiet cucumber-features/*.feature',
stryker: '--publish-quiet'
}
5 changes: 5 additions & 0 deletions e2e/test/ignore-static/jasmine.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"spec_files": [
"spec/*.spec.js"
]
}
4 changes: 4 additions & 0 deletions e2e/test/ignore-static/jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"testEnvironment": "node",
"testMatch": ["<rootDir>/spec/*.spec.js"]
}
21 changes: 21 additions & 0 deletions e2e/test/ignore-static/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Karma configuration
// Generated on Tue Nov 30 2021 09:57:14 GMT+0100 (Central European Standard Time)

module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['chai', 'jasmine'],
files: [
'src/**/*.js',
'spec/**/*.js'
],
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['ChromeHeadless'],
singleRun: true,
concurrency: Infinity
})
}
14 changes: 14 additions & 0 deletions e2e/test/ignore-static/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "ignore-static",
"description": "e2e test for testing --ignoreStatic",
"version": "0.0.0",
"private": true,
"scripts": {
"test:cucumber": "cucumber-js",
"test:jasmine": "jasmine --config=jasmine.json",
"test:mocha": "mocha",
"test:jest": "jest",
"test:karma": "karma start",
"test": "mocha --no-config --no-package --timeout 0 verify/verify.js"
}
}
13 changes: 13 additions & 0 deletions e2e/test/ignore-static/spec/chai-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
const chai = globalThis.chai ?? require('chai');

globalThis.expect = chai.expect;
chai.util.addMethod(chai.Assertion.prototype, 'toEqual', function (expected) {
var obj = chai.util.flag(this, 'object');
new chai.Assertion(obj).to.deep.equal(expected);
});
chai.util.addMethod(chai.Assertion.prototype, 'toBe', function (expected) {
var obj = chai.util.flag(this, 'object');
new chai.Assertion(obj).to.equal(expected);
});
}
50 changes: 50 additions & 0 deletions e2e/test/ignore-static/spec/math.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
const MyMath = globalThis.MyMath ?? require('../src/math').MyMath;

let crashNextRun = false;

describe('MyMath', () => {
// Bad practice, but on purpose. This makes for a hybrid mutant
const math = new MyMath();

// Testing the hybrid mutant
it('should provide value 3.14 for pi', () => {
expect(math.pi).toEqual(3.14);
});

// Testing the static mutant
it('should be able to provide pi = "🥧"', () => {
expect(MyMath.pi).toEqual('🥧');
});

describe('add', () => {
let math2;
beforeEach(() => {
math2 = new MyMath();
});

// Testing the non-static mutant
it('should be able to add 40 and 2', () => {
expect(math2.add(40, 2)).toEqual(42);
});

// Testing that the hybrid didn't "leak" to, see https://github.com/stryker-mutator/stryker-js/issues/3442
it('should be able to add pi 2 times', () => {
expect(math2.add(math.pi, math.pi)).toEqual(6.28);
});
});
});

(typeof beforeAll === 'function' ? beforeAll : before)(() => {
if (crashNextRun && typeof process === 'object') {
process.exit(1);
}
});

(typeof afterAll === 'function' ? afterAll : after)(() => {
// Exit after dry run, so we get a fresh start.
if (!globalThis.__stryker__?.activeMutant) {
crashNextRun = true;
}
});
}
24 changes: 24 additions & 0 deletions e2e/test/ignore-static/src/math.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

// Use `var` so it is declared on the `globalThis` in the browser
var MyMath = class MyMath {

constructor() {
// Hybrid mutant "0"
this.pi = 3.14;
}

add(a, b) {
// Non-static mutants: "1" and "2"
return a + b;
}

// Static mutant "3"
static pi = '🥧';
}



// Stryker disable all: Not useful for coverage analysis test
if (typeof module === 'object') {
module.exports = { MyMath: MyMath };
}
14 changes: 14 additions & 0 deletions e2e/test/ignore-static/stryker.conf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "../../node_modules/@stryker-mutator/core/schema/stryker-schema.json",
"concurrency": 1,
"concurrency_comment": "Concurrency 1 so we can test the reuse of the test runner for subsequent runs",
"reporters": ["json"],
"ignoreStatic": true,
"jasmineConfigFile": "jasmine.json",
"karma": {
"configFile": "karma.conf.js"
},
"jest": {
"configFile": "jest.config.json"
}
}
3 changes: 3 additions & 0 deletions e2e/test/ignore-static/verify/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
37 changes: 37 additions & 0 deletions e2e/test/ignore-static/verify/verify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import fs from 'fs';

import { expect } from 'chai';

import { execStryker, expectMetricsJsonToMatchSnapshot } from '../../../helpers.js';

describe('Ignore static e2e test', () => {
beforeEach(async () => {
await fs.promises.rm('reports', { recursive: true, force: true });
});

it('should work for mocha', async () => {
const { exitCode } = execStryker('stryker run --testRunner mocha');
expect(exitCode).eq(0);
await expectMetricsJsonToMatchSnapshot();
});
it('should work for jasmine', async () => {
const { exitCode } = execStryker('stryker run --testRunner jasmine');
expect(exitCode).eq(0);
await expectMetricsJsonToMatchSnapshot();
});
it('should work for karma', async () => {
const { exitCode } = execStryker('stryker run --testRunner karma');
expect(exitCode).eq(0);
await expectMetricsJsonToMatchSnapshot();
});
it('should work for cucumber', async () => {
const { exitCode } = execStryker('stryker run --testRunner cucumber');
expect(exitCode).eq(0);
await expectMetricsJsonToMatchSnapshot();
});
it('should work for jest', async () => {
const { exitCode } = execStryker('stryker run --testRunner jest --tempDirName stryker-tmp');
expect(exitCode).eq(0);
await expectMetricsJsonToMatchSnapshot();
});
});
101 changes: 101 additions & 0 deletions e2e/test/ignore-static/verify/verify.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Ignore static e2e test should work for cucumber 1`] = `
Object {
"compileErrors": 0,
"ignored": 8,
"killed": 2,
"mutationScore": 100,
"mutationScoreBasedOnCoveredCode": 100,
"noCoverage": 0,
"runtimeErrors": 0,
"survived": 0,
"timeout": 0,
"totalCovered": 2,
"totalDetected": 2,
"totalInvalid": 0,
"totalMutants": 10,
"totalUndetected": 0,
"totalValid": 2,
}
`;

exports[`Ignore static e2e test should work for jasmine 1`] = `
Object {
"compileErrors": 0,
"ignored": 7,
"killed": 2,
"mutationScore": 66.66666666666666,
"mutationScoreBasedOnCoveredCode": 66.66666666666666,
"noCoverage": 0,
"runtimeErrors": 0,
"survived": 1,
"timeout": 0,
"totalCovered": 3,
"totalDetected": 2,
"totalInvalid": 0,
"totalMutants": 10,
"totalUndetected": 1,
"totalValid": 3,
}
`;

exports[`Ignore static e2e test should work for jest 1`] = `
Object {
"compileErrors": 0,
"ignored": 7,
"killed": 3,
"mutationScore": 100,
"mutationScoreBasedOnCoveredCode": 100,
"noCoverage": 0,
"runtimeErrors": 0,
"survived": 0,
"timeout": 0,
"totalCovered": 3,
"totalDetected": 3,
"totalInvalid": 0,
"totalMutants": 10,
"totalUndetected": 0,
"totalValid": 3,
}
`;

exports[`Ignore static e2e test should work for karma 1`] = `
Object {
"compileErrors": 0,
"ignored": 7,
"killed": 3,
"mutationScore": 100,
"mutationScoreBasedOnCoveredCode": 100,
"noCoverage": 0,
"runtimeErrors": 0,
"survived": 0,
"timeout": 0,
"totalCovered": 3,
"totalDetected": 3,
"totalInvalid": 0,
"totalMutants": 10,
"totalUndetected": 0,
"totalValid": 3,
}
`;

exports[`Ignore static e2e test should work for mocha 1`] = `
Object {
"compileErrors": 0,
"ignored": 7,
"killed": 2,
"mutationScore": 66.66666666666666,
"mutationScoreBasedOnCoveredCode": 66.66666666666666,
"noCoverage": 0,
"runtimeErrors": 0,
"survived": 1,
"timeout": 0,
"totalCovered": 3,
"totalDetected": 2,
"totalInvalid": 0,
"totalMutants": 10,
"totalUndetected": 1,
"totalValid": 3,
}
`;
16 changes: 3 additions & 13 deletions e2e/test/jest-react-ts/stryker.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,10 @@
"packageManager": "npm",
"testRunner": "jest",
"tempDirName": "stryker-tmp",
"mutate": [
"src/App/TodoList/Item/index.tsx",
"src/NotFound.tsx"
],
"mutate": ["src/App/TodoList/Item/index.tsx", "src/NotFound.tsx"],
"timeoutMS": 40000,
"concurrency": 2,
"coverageAnalysis": "perTest",
"reporters": [
"json",
"progress",
"clear-text",
"html"
],
"jest": {
"projectType": "create-react-app"
}
"reporters": ["json", "progress", "clear-text", "html"],
"jest": { "projectType": "create-react-app" }
}
Loading