Skip to content

Commit

Permalink
feat(mocha): support mocha 6.2
Browse files Browse the repository at this point in the history
  • Loading branch information
nicojs committed Sep 2, 2019
1 parent 0ecc0ec commit feddcf1
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 43 deletions.
14 changes: 7 additions & 7 deletions e2e/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"link-parent-bin": "~1.0.0",
"load-grunt-tasks": "~4.0.0",
"mocha": "~5.2.0",
"mutation-testing-metrics": "^1.0.7",
"mutation-testing-metrics": "^1.1.1",
"rxjs": "^6.3.3",
"semver": "^5.6.0",
"ts-node": "~7.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"lodash": "~4.17.4",
"log4js": "~4.5.1",
"mkdirp": "~0.5.1",
"mutation-testing-metrics": "^1.0.7",
"mutation-testing-metrics": "^1.1.1",
"progress": "~2.0.0",
"rimraf": "~2.6.1",
"rxjs": "~6.5.1",
Expand Down
4 changes: 4 additions & 0 deletions packages/mocha-runner/src/LibWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ try {
try {
// https://github.com/mochajs/mocha/blob/master/lib/cli/run-helpers.js#L132
handleFiles = require('mocha/lib/cli/run-helpers').handleFiles;
if (!handleFiles) {
// Might be moved: https://github.com/mochajs/mocha/commit/15b96afccaf508312445770e3af1c145d90b28c6#diff-39b692a81eb0c9f3614247af744ab4a8
handleFiles = require('mocha/lib/cli/collect-files');
}
} catch {
// Mocha < 6 doesn't support `handleFiles`
}
Expand Down
2 changes: 2 additions & 0 deletions packages/mocha-runner/src/MochaOptionsLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { filterConfig, mochaOptionsKey, serializeArguments } from './utils';
*/
export const DEFAULT_MOCHA_OPTIONS = Object.freeze({
extension: ['js'],
file: [],
ignore: [],
opts: './test/mocha.opts',
spec: ['test'],
timeout: 2000,
Expand Down
6 changes: 4 additions & 2 deletions packages/mocha-runner/src/MochaTestRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ export default class MochaTestRunner implements TestRunner {
try {
// process.exit unfortunate side effect: https://github.com/mochajs/mocha/blob/07ea8763c663bdd3fe1f8446cdb62dae233f4916/lib/cli/run-helpers.js#L174
(process as any).exit = () => { };
return handleFiles(this.mochaOptions);
} finally {
const files = handleFiles(this.mochaOptions);
return files;
}
finally {
process.exit = originalProcessExit;
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/mocha-runner/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const SUPPORTED_MOCHA_OPTIONS = Object.freeze([
'ui',
'grep',
'exclude',
'ignore',
'spec',
'file'
]);
Expand Down
6 changes: 6 additions & 0 deletions packages/mocha-runner/test/helpers/factories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { MochaOptions } from '../../src/MochaOptions';
import { DEFAULT_MOCHA_OPTIONS } from '../../src/MochaOptionsLoader';

export function createMochaOptions(overrides: Partial<MochaOptions>): MochaOptions {
return { ...DEFAULT_MOCHA_OPTIONS, ...overrides };
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,16 @@ describe(`${MochaOptionsLoader.name} integration`, () => {
'/path/to/some/file',
'/path/to/some/other/file'
],
ignore: [
'/path/to/some/excluded/file'
],
require: [
'@babel/register'
],
spec: [
'test/**/*.spec.js'
],
timeout: 0,
timeout: false,
ui: 'bdd'
});
});
Expand All @@ -88,6 +91,8 @@ describe(`${MochaOptionsLoader.name} integration`, () => {
expect(actualConfig).deep.eq({
['async-only']: true,
extension: ['js'],
file: [],
ignore: [],
opts: configFile,
spec: ['/tests/**/*.js', '/foo/*.js'],
timeout: 2000,
Expand All @@ -101,7 +106,7 @@ describe(`${MochaOptionsLoader.name} integration`, () => {
expect(actualConfig).deep.eq({
...DEFAULT_MOCHA_OPTIONS,
['async-only']: true,
extension: ['json', 'js'],
extension: ['json'],
package: pkgFile,
timeout: 20,
ui: 'tdd'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ export interface RunMessage {
}

export default class MochaTestFrameworkIntegrationTestWorker {
private readonly sut: MochaTestRunner;
public readonly sut: MochaTestRunner;

constructor() {
testInjector.options.mochaOptions = {
file: [],
ignore: [],
spec: [
path.resolve(__dirname, '..', '..', 'testResources', 'sampleProject', 'MyMathSpec.js')
]
],
};
this.sut = testInjector.injector
.provideValue(commonTokens.sandboxFileNames, [
Expand All @@ -30,11 +32,11 @@ export default class MochaTestFrameworkIntegrationTestWorker {
.injectClass(MochaTestRunner);

this.listenForParentProcess();
try {
this.sut.init();
} catch (err) {
this.sendError(err);
}
// try {
this.sut.init();
// } catch (err) {
// this.sendError(err);
// }
}

public listenForParentProcess() {
Expand Down
11 changes: 6 additions & 5 deletions packages/mocha-runner/test/integration/QUnitSample.it.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { testInjector } from '@stryker-mutator/test-helpers';
import { expect } from 'chai';
import * as path from 'path';
import MochaTestRunner from '../../src/MochaTestRunner';
import { createMochaOptions } from '../helpers/factories';

describe('QUnit sample', () => {
let files: string[];
Expand All @@ -19,13 +20,13 @@ describe('QUnit sample', () => {
}

it('should work when configured with "qunit" ui', async () => {
const mochaOptions = {
const mochaOptions = createMochaOptions({
require: [],
spec: [resolve('./testResources/qunit-sample/MyMathSpec.js')],
ui: 'qunit'
};
});
testInjector.options.mochaOptions = mochaOptions;
files = mochaOptions.spec;
files = mochaOptions.spec || [];
const sut = createSut();
await sut.init();
const actualResult = await sut.run({});
Expand All @@ -44,11 +45,11 @@ describe('QUnit sample', () => {
resolve('./testResources/qunit-sample/MyMathSpec.js'),
resolve('./testResources/qunit-sample/MyMath.js')
];
testInjector.options.mochaOptions = {
testInjector.options.mochaOptions = createMochaOptions({
files: [
resolve('./testResources/qunit-sample/MyMathSpec.js')
]
};
});
const sut = createSut();
await sut.init();
const actualResult = await sut.run({});
Expand Down
13 changes: 6 additions & 7 deletions packages/mocha-runner/test/integration/SampleProject.it.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { testInjector } from '@stryker-mutator/test-helpers';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as path from 'path';
import { MochaOptions } from '../../src/MochaOptions';
import MochaTestRunner from '../../src/MochaTestRunner';
import { createMochaOptions } from '../helpers/factories';
chai.use(chaiAsPromised);
const expect = chai.expect;

Expand Down Expand Up @@ -39,7 +39,7 @@ describe('Running a sample project', () => {
resolve('./testResources/sampleProject/MyMath.js'),
resolve('./testResources/sampleProject/MyMathSpec.js')
];
testInjector.options.mochaOptions = { spec };
testInjector.options.mochaOptions = createMochaOptions({ spec });
sut = createSut();
return sut.init();
});
Expand All @@ -66,10 +66,9 @@ describe('Running a sample project', () => {
resolve('testResources/sampleProject/MyMath.js'),
resolve('testResources/sampleProject/MyMathSpec.js'),
];
const mochaOptions: MochaOptions = {
testInjector.options.mochaOptions = createMochaOptions({
files: spec
};
testInjector.options.mochaOptions = mochaOptions;
});
sut = createSut();
return sut.init();
});
Expand All @@ -87,7 +86,7 @@ describe('Running a sample project', () => {
resolve('testResources/sampleProject/MyMath.js'),
resolve('testResources/sampleProject/MyMathFailedSpec.js')
];
testInjector.options.mochaOptions = { spec };
testInjector.options.mochaOptions = createMochaOptions({ spec });
sut = createSut();
return sut.init();
});
Expand All @@ -102,7 +101,7 @@ describe('Running a sample project', () => {

beforeEach(() => {
spec = [resolve('./testResources/sampleProject/MyMath.js')];
testInjector.options.mochaOptions = { spec };
testInjector.options.mochaOptions = createMochaOptions({ spec });
sut = createSut();
return sut.init();
});
Expand Down
21 changes: 13 additions & 8 deletions packages/mocha-runner/test/unit/MochaOptionsLoader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ describe(MochaOptionsLoader.name, () => {
rawOptions.ui = 'quux';
rawOptions.grep = 'quuz';
rawOptions.exclude = 'corge';
rawOptions.ignore = 'garply';
rawOptions.file = 'grault';
rawOptions.spec = ['test/**/*.js'];

Expand All @@ -69,6 +70,7 @@ describe(MochaOptionsLoader.name, () => {
extension: 'foo',
file: 'grault',
grep: 'quuz',
ignore: 'garply',
opts: './test/mocha.opts',
override: true,
require: 'bar',
Expand All @@ -94,7 +96,7 @@ describe(MochaOptionsLoader.name, () => {

it('should respect mocha\'s defaults', () => {
const options = sut.load(testInjector.options);
expect(options).deep.eq(defaultMochaOptions());
expect(options).deep.eq(createMochaOptions());
});
});

Expand Down Expand Up @@ -159,7 +161,7 @@ describe(MochaOptionsLoader.name, () => {
it('should not load default mocha.opts file if not found', () => {
existsFileStub.returns(false);
const options = sut.load(config);
expect(options).deep.eq(defaultMochaOptions());
expect(options).deep.eq(createMochaOptions());
expect(testInjector.logger.debug).calledWith('No mocha opts file found, not loading additional mocha options (%s.opts was not defined).', 'mochaOptions');
});

Expand Down Expand Up @@ -211,15 +213,15 @@ describe(MochaOptionsLoader.name, () => {
ui: 'exports'
};
const options = sut.load(config);
expect(options).deep.equal({
expect(options).deep.equal(createMochaOptions({
asyncOnly: false,
extension: ['js'],
opts: 'path/to/opts/file',
require: ['ts-node/register'],
spec: ['test'],
timeout: 4000,
ui: 'exports'
});
}));
});

it('should ignore additional properties', () => {
Expand All @@ -246,25 +248,28 @@ describe(MochaOptionsLoader.name, () => {
opts: 'some/mocha.opts/file',
};
const options = sut.load(config);
expect(options).deep.eq({
expect(options).deep.eq(createMochaOptions({
extension: ['js'],
opts: 'some/mocha.opts/file',
spec: [
'test'
],
timeout: undefined,
ui: undefined
});
}));
});
});

function defaultMochaOptions() {
function createMochaOptions(overrides?: Partial<MochaOptions>) {
return {
extension: ['js'],
file: [],
ignore: [],
opts: './test/mocha.opts',
spec: ['test'],
timeout: 2000,
ui: 'bdd'
ui: 'bdd',
... overrides
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// project's `package.json`.
{
"diff": true,
"extension": ["json"],
"extension": ["json", "js"],
"opts": "./test/mocha.opts",
"package": "./package.json",
"reporter": "spec",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// project's `package.json`.
{
"diff": true,
"extension": ["jsonc"],
"extension": ["jsonc", "js"],
"opts": "./test/mocha.opts",
"package": /* 📦 */ "./package.json",
"reporter": /* 📋 */ "spec",
Expand Down
2 changes: 1 addition & 1 deletion packages/test-helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"homepage": "https://github.com/stryker-mutator/stryker/tree/master/packages/test-helpers#readme",
"license": "ISC",
"dependencies": {
"mutation-testing-metrics": "^1.0.7"
"mutation-testing-metrics": "^1.1.1"
},
"devDependencies": {
"@stryker-mutator/api": "^2.0.2",
Expand Down

0 comments on commit feddcf1

Please sign in to comment.