Skip to content

Commit

Permalink
fix(IsolatedTestRunnerAdapter): Improve error handling when test runn…
Browse files Browse the repository at this point in the history
…er worker process crashes (#285)

* fix(IsolatedTestRunnerAdapter): Log why a child process crashed
* chore(vscode): Refactor vscode config for new build
* fix(runner-adapter): Reject promise when child process crashes
* fix(integration-test): Split out the integration tests
* Split out the integration tests and make sure they work with relative paths
  • Loading branch information
nicojs authored Apr 21, 2017
1 parent cfa09b3 commit 2b4bda7
Show file tree
Hide file tree
Showing 21 changed files with 162 additions and 57 deletions.
3 changes: 1 addition & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
],
"showOutput": "always",
"suppressTaskName": true,
"isBackground": true,
"problemMatcher": "$tsc-watch"
"isBackground": true
}
3 changes: 2 additions & 1 deletion integrationTest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"stryker": "file:../packages/stryker",
"stryker-api": "file:../packages/stryker-api",
"stryker-jasmine": "file:../packages/stryker-jasmine",
"stryker-karma-runner": "file:../packages/stryker-karma-runner"
"stryker-karma-runner": "file:../packages/stryker-karma-runner",
"typescript": "^2.2.2"
},
"scripts": {
"postinstall": "link-parent-bin -c test"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "test-module",
"version": "0.0.0",
"private": true,
"description": "A module to perform an integration test",
"main": "index.js",
"scripts": {
Expand Down
24 changes: 24 additions & 0 deletions integrationTest/test/karma-jasmine/src/Add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var add = function(num1, num2) {
return num1 + num2;
};

var addOne = function(number) {
number++;
return number;
};

var negate = function(number) {
return -number;
};

var notCovered = function(number) {
return number > 10;
};

var isNegativeNumber = function(number) {
var isNegative = false;
if(number < 0){
isNegative = true;
}
return isNegative;
};
8 changes: 8 additions & 0 deletions integrationTest/test/karma-jasmine/src/Circle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var getCircumference = function(radius) {
//Function to test multiple math mutations in a single function.
return 2 * Math.PI * radius;
};

var untestedFunction = function() {
var i = 5 / 2 * 3;
};
8 changes: 8 additions & 0 deletions integrationTest/test/karma-jasmine/stryker.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = function(config){
config.set({
files: ['src/*.js', 'test/*.js'],
mutate: ['src/*.js'],
testFramework: 'jasmine',
testRunner: 'karma'
});
};
45 changes: 45 additions & 0 deletions integrationTest/test/karma-jasmine/test/AddSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
describe('Add', function() {
it('should be able to add two numbers', function() {
var num1 = 2;
var num2 = 5;
var expected = num1 + num2;

var actual = add(num1, num2);

expect(actual).toBe(expected);
});

it('should be able 1 to a number', function() {
var number = 2;
var expected = 3;

var actual = addOne(number);

expect(actual).toBe(expected);
});

it('should be able negate a number', function() {
var number = 2;
var expected = -2;

var actual = negate(number);

expect(actual).toBe(expected);
});

it('should be able to recognize a negative number', function() {
var number = -2;

var isNegative = isNegativeNumber(number);

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);
});
});
10 changes: 10 additions & 0 deletions integrationTest/test/karma-jasmine/test/CircleSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
describe('Circle', function() {
it('should have a circumference of 2PI when the radius is 1', function() {
var radius = 1;
var expectedCircumference = 2 * Math.PI;

var circumference = getCircumference(radius);

expect(circumference).toBe(expectedCircumference);
});
});
9 changes: 0 additions & 9 deletions integrationTest/test/module/stryker.conf.js

This file was deleted.

3 changes: 0 additions & 3 deletions integrationTest/test/module/usingStryker.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "use-stryker-programmatically",
"version": "0.0.0",
"private": true,
"scripts": {
"pretest": "tsc -p .",
"test": "node usingStryker.js"
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var stryker_1 = require("stryker");
new stryker_1.default({ mutateFiles: [], allFiles: [], coverageAnalysis: 'off' }).runMutationTest().then(function () { return console.log('done'); });
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Stryker from 'stryker';

new Stryker({ mutateFiles: [], allFiles: [], coverageAnalysis: 'off'} ).runMutationTest().then(() => console.log('done'));
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"test": "lerna run test",
"posttest": "mocha integrationTest/*.js",
"clean-integration-test": "node tasks/cleanIntegrationTest.js",
"start": "concurrently \"tsc -w -p integrationTest\" \"tsc -w -p packages/stryker\" \"tsc -w -p packages/stryker-api\"",
"start": "concurrently \"tsc -w -p integrationTest\" \"tsc -w -p packages/stryker\" \"tsc -w -p packages/stryker-api\" \"tsc -w -p packages/stryker-jasmine\" \"tsc -w -p packages/stryker-karma-runner\" \"tsc -w -p packages/stryker-mocha-runner\"",
"test:stryker": "lerna run test --scope stryker",
"test:stryker-api": "lerna run test --scope stryker"
}
Expand Down
48 changes: 10 additions & 38 deletions packages/stryker/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,20 @@
"version": "0.2.0",
"configurations": [
{
"name": "Run unit tests",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/grunt-cli/bin/grunt",
// "preLaunchTask": "build",
"stopOnEntry": false,
"name": "Unit tests",
"program": "${workspaceRoot}/../../node_modules/mocha/bin/_mocha",
"args": [
"mochaTest:unit"
],
"cwd": "${workspaceRoot}/.",
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
"-u",
"tdd",
"--timeout",
"999999",
"--colors",
"${workspaceRoot}/test/helpers/**/*.js",
"${workspaceRoot}/test/unit/**/*.js"
],
"env": {
"NODE_ENV": "development"
},
"console": "internalConsole",
"sourceMaps": true,
"outDir": "${workspaceRoot}"
},
{
"name": "Run integration tests",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/grunt-cli/bin/grunt",
// "preLa4unchTask": "ts",
"stopOnEntry": false,
"args": [
"mochaTest:integration"
],
"cwd": "${workspaceRoot}/.",
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "development"
},
"console": "internalConsole",
"sourceMaps": true,
"outDir": "${workspaceRoot}"
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": "Run stryker example",
Expand Down
2 changes: 1 addition & 1 deletion packages/stryker/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.tsdk": "../../node_modules/typescript/lib",
"files": {
"exclude": {
".git": "",
Expand Down
11 changes: 11 additions & 0 deletions packages/stryker/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "npm",
"isShellCommand": true,
"args": ["start"],
"showOutput": "silent",
"isBackground": true,
"problemMatcher": "$tsc-watch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default class TestRunnerChildProcessAdapter extends EventEmitter implemen

private workerProcess: ChildProcess;
private currentTask: WorkerTask;
private lastMessagesQueue: string[] = [];

constructor(private realTestRunnerName: string, private options: IsolatedRunnerOptions) {
super();
Expand All @@ -51,11 +52,19 @@ export default class TestRunnerChildProcessAdapter extends EventEmitter implemen
if (this.workerProcess.stdout) {
let traceEnabled = log.isTraceEnabled();
this.workerProcess.stdout.on('data', (data: Buffer) => {
const msg = data.toString();

this.lastMessagesQueue.push(msg);
if (this.lastMessagesQueue.length > 10) {
this.lastMessagesQueue.shift();
}

if (traceEnabled) {
log.trace(data.toString());
log.trace(msg);
}
});
}

if (this.workerProcess.stderr) {
this.workerProcess.stderr.on('data', (data: any) => {
log.error(data.toString());
Expand Down Expand Up @@ -90,6 +99,15 @@ export default class TestRunnerChildProcessAdapter extends EventEmitter implemen
break;
}
});

this.workerProcess.on('exit', (code: number | null, signal: string) => {
if (code !== 0 && code !== null) {
log.error(`Child process exited with non-zero exit code ${code}. Last 10 message from the child process were: \r\n${this.lastMessagesQueue.map(msg => `\t${msg}`).join('\r\n')}`);
if (this.currentTask) {
this.currentTask.reject(`Test runner child process exited with non-zero exit code ${code}`);
}
}
});
}

private logReceivedUnexpectedMessageWarning(message: WorkerMessage) {
Expand Down
6 changes: 5 additions & 1 deletion packages/stryker/src/stryker-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ if (strykerConfig) {
}

const commands: { [cmd: string]: () => void } = {
run: () => new Stryker(program).runMutationTest().catch(err => log.error(`an error occurred`, err))
run: () => new Stryker(program).runMutationTest().catch(err => {
log.error(`an error occurred`, err);
process.exitCode = 1;
process.kill(process.pid, 'SIGINT');
})
};

if (Object.keys(commands).indexOf(command) >= 0) {
Expand Down

0 comments on commit 2b4bda7

Please sign in to comment.