Skip to content

Commit

Permalink
feat(unincluded-files): Support unincluded files
Browse files Browse the repository at this point in the history
Add support for unincluded files. Files with `included: false` will be cleared from the cache but will *not* be in the initial load by the mocha test framework.
  • Loading branch information
nicojs authored and simondel committed Apr 21, 2017
1 parent 0c41fbf commit 80297bc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/stryker-mocha-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"log4js": "^0.6.37"
},
"peerDependencies": {
"stryker-api": "^0.1.0",
"stryker-api": "^0.1.2",
"mocha": "^2.3.3"
},
"devDependencies": {
Expand All @@ -52,7 +52,7 @@
"mocha-sinon": "^1.1.4",
"sinon": "^1.17.2",
"sinon-chai": "^2.8.0",
"stryker-api": "0.1.0",
"stryker-api": "0.1.2",
"typescript": "^1.8.10",
"typings": "^1.3.1"
}
Expand Down
11 changes: 6 additions & 5 deletions packages/stryker-mocha-runner/src/MochaTestRunner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {TestRunner, TestResult, RunResult, RunnerOptions, CoverageCollection} from 'stryker-api/test_runner';
import {InputFile} from 'stryker-api/core';
import * as path from 'path';
import * as log4js from 'log4js';
// import * as Mocha from 'mocha';
Expand All @@ -7,22 +8,22 @@ import StrykerMochaReporter from './StrykerMochaReporter';

const log = log4js.getLogger('MochaTestRunner');
export default class MochaTestRunner implements TestRunner {
private files: string[];
private files: InputFile[];

constructor(runnerOptions: RunnerOptions) {
this.files = runnerOptions.files.map(f => path.resolve(f.path));
this.files = runnerOptions.files;
}

private purgeFiles() {
this.files.forEach(f => delete require.cache[f]);
this.files.forEach(f => delete require.cache[f.path]);
}

run(): Promise<RunResult> {
return new Promise<RunResult>((resolve, fail) => {
try {
this.purgeFiles();
let mocha = new Mocha({ reporter: StrykerMochaReporter });
this.files.forEach(f => mocha.addFile(f));
this.files.filter(file => file.included).forEach(f => mocha.addFile(f.path));
try {
let runner: any = mocha.run((failures: number) => {
let result: RunResult = runner.runResult;
Expand All @@ -36,7 +37,7 @@ export default class MochaTestRunner implements TestRunner {
}
} catch (error) {
log.error(error);
fail();
fail(error);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import * as chai from 'chai';
import MochaTestRunner from '../../src/MochaTestRunner';
import {TestResult, RunnerOptions, RunResult} from 'stryker-api/test_runner';
import * as chaiAsPromised from 'chai-as-promised';
import * as path from 'path';
chai.use(chaiAsPromised);
let expect = chai.expect;

describe.only('MochaTestRunner', function() {
describe('MochaTestRunner', function () {

var sut: MochaTestRunner;
this.timeout(10000);
Expand All @@ -15,7 +16,9 @@ describe.only('MochaTestRunner', function() {

before(() => {
testRunnerOptions = {
files: [{ path: 'testResources/sampleProject/src/MyMath.js', shouldMutate: true }, { path: 'testResources/sampleProject/test/MyMathSpec.js', shouldMutate: false }],
files: [
file('./testResources/sampleProject/src/MyMath.js'),
file('./testResources/sampleProject/test/MyMathSpec.js')],
coverageEnabled: false,
strykerOptions: {},
port: 1234
Expand All @@ -39,12 +42,33 @@ describe.only('MochaTestRunner', function() {
});

it('should be able to run 2 times in a row', () => {
return expect(sut.run().then( () => sut.run())).to.eventually.satisfy((testResult: RunResult) => {
return expect(sut.run().then(() => sut.run())).to.eventually.satisfy((testResult: RunResult) => {
expect(testResult.succeeded).to.be.eq(5);
return true;
});
});
});

describe('with an error in an unincluded input file', () => {
before(() => {
let options = {
files: [
file('testResources/sampleProject/src/MyMath.js'),
file('testResources/sampleProject/src/Error.js', false, false),
file('testResources/sampleProject/test/MyMathSpec.js')],
coverageEnabled: false,
strykerOptions: {},
port: 1234
};
sut = new MochaTestRunner(options);
});

it('should report completed tests without errors', () => expect(sut.run()).to.eventually.satisfy((testResult: RunResult) => {
expect(testResult.result).to.be.eq(TestResult.Complete, 'Test result did not match');
return true;
}));
});
});

let file = (filePath: string, mutated: boolean = true, included: boolean = true) => ({ path: path.resolve(filePath), mutated, included });
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

someGlobalObjectWhichDoesNotExist.someFunctionWhichDoesNotExist('1', 2);

0 comments on commit 80297bc

Please sign in to comment.