Skip to content

Commit

Permalink
feat(mocha): deprecate mocha < v6 (#2379)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Mocha@<6 is deprecated and support for it will be removed in Stryker v5
  • Loading branch information
nicojs committed Aug 13, 2020
1 parent 2343dcf commit fee0754
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 5 deletions.
196 changes: 196 additions & 0 deletions e2e/test/mocha-old-version/package-lock.json

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

24 changes: 24 additions & 0 deletions e2e/test/mocha-old-version/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "mocha-old-version",
"version": "0.0.0",
"private": true,
"description": "A test package for mocha 5",
"main": "index.js",
"scripts": {
"pretest": "rimraf \"reports\"",
"test": "stryker run",
"posttest": "mocha --no-config --require ../../tasks/ts-node-register.js verify/*.ts"
},
"localDependencies": {
"@stryker-mutator/api": "../../../packages/api",
"@stryker-mutator/core": "../../../packages/core",
"@stryker-mutator/instrumenter": "../../../packages/instrumenter",
"@stryker-mutator/mocha-runner": "../../../packages/mocha-runner",
"@stryker-mutator/util": "../../../packages/util"
},
"author": "",
"license": "ISC",
"devDependencies": {
"mocha": "^5.2.0"
}
}
6 changes: 6 additions & 0 deletions e2e/test/mocha-old-version/src/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports.add = function(num1, num2) {
return num1 + num2;
};



14 changes: 14 additions & 0 deletions e2e/test/mocha-old-version/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",
"testRunner": "mocha",
"concurrency": 1,
"coverageAnalysis": "off",
"reporters": ["clear-text", "html", "event-recorder"],
"mochaOptions": {
"spec": ["test/**/*.js"]
},
"fileLogLevel": "info",
"plugins": [
"@stryker-mutator/mocha-runner"
]
}
10 changes: 10 additions & 0 deletions e2e/test/mocha-old-version/test/unit/add.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { add } = require('../../src/add');
const { expect } = require('chai');

describe('add', () => {

it('2 + 3 = 5', () => {
expect(add(2, 3)).to.be.equal(5);
});

});
17 changes: 17 additions & 0 deletions e2e/test/mocha-old-version/verify/verify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { promises as fs } from 'fs';
import { expect } from 'chai';
import { it } from 'mocha';

describe('Verify stryker runs with mocha < 6', () => {

let strykerLog: string;

before(async () => {
strykerLog = await fs.readFile('./stryker.log', 'utf8');
});

it('should warn about old mocha version', async () => {
expect(strykerLog).contains('DEPRECATED: Mocha < 6 detected. Please upgrade to at least Mocha version 6. Stryker will drop support for Mocha < 6 in V5');
});

});
10 changes: 7 additions & 3 deletions packages/mocha-runner/src/LibWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import path = require('path');

import * as Mocha from 'mocha';
import glob = require('glob');

import { MochaOptions } from '../src-generated/mocha-runner-options';

const mochaRoot = path.dirname(require.resolve('mocha/package.json'));

let loadOptions: undefined | ((argv?: string[] | string) => { [key: string]: any } | undefined);
let collectFiles: undefined | ((options: MochaOptions) => string[]);
let handleRequires: undefined | ((requires?: string[]) => Promise<any>);
Expand All @@ -14,21 +18,21 @@ try {
* @since 6.0.0'
* @see https://mochajs.org/api/module-lib_cli_options.html#.loadOptions
*/
loadOptions = require('mocha/lib/cli/options').loadOptions;
loadOptions = require(`${mochaRoot}/lib/cli/options`).loadOptions;
} catch {
// Mocha < 6 doesn't support `loadOptions`
}

try {
// https://github.com/mochajs/mocha/blob/master/lib/cli/run-helpers.js#L132
const runHelpers = require('mocha/lib/cli/run-helpers');
const runHelpers = require(`${mochaRoot}/lib/cli/run-helpers`);
collectFiles = runHelpers.handleFiles;
handleRequires = runHelpers.handleRequires; // handleRequires is available since mocha v7.2
loadRootHooks = runHelpers.loadRootHooks; // loadRootHooks is available since mocha v7.2

if (!collectFiles) {
// Might be moved: https://github.com/mochajs/mocha/commit/15b96afccaf508312445770e3af1c145d90b28c6#diff-39b692a81eb0c9f3614247af744ab4a8
collectFiles = require('mocha/lib/cli/collect-files');
collectFiles = require(`${mochaRoot}/lib/cli/collect-files`);
}
} catch {
// Mocha < 6 doesn't support `handleFiles`
Expand Down
2 changes: 1 addition & 1 deletion packages/mocha-runner/src/MochaOptionsLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default class MochaOptionsLoader {
this.log.debug("Mocha >= 6 detected. Using mocha's `%s` to load mocha options", LibWrapper.loadOptions.name);
return this.loadMocha6Options(overrides);
} else {
this.log.warn('DEPRECATED: Mocha < 6 detected. Please upgrade to at least Mocha version 6.');
this.log.warn('DEPRECATED: Mocha < 6 detected. Please upgrade to at least Mocha version 6. Stryker will drop support for Mocha < 6 in V5.');
this.log.debug('Mocha < 6 detected. Using custom logic to parse mocha options');
return this.loadLegacyMochaOptsFile(overrides);
}
Expand Down
4 changes: 3 additions & 1 deletion packages/mocha-runner/test/unit/MochaOptionsLoader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ describe(MochaOptionsLoader.name, () => {
it('should log deprecated mocha version warning', async () => {
existsFileStub.returns(false);
sut.load(options);
expect(testInjector.logger.warn).calledWith('DEPRECATED: Mocha < 6 detected. Please upgrade to at least Mocha version 6.');
expect(testInjector.logger.warn).calledWith(
'DEPRECATED: Mocha < 6 detected. Please upgrade to at least Mocha version 6. Stryker will drop support for Mocha < 6 in V5.'
);
});

it('should load a mocha.opts file if specified', () => {
Expand Down

0 comments on commit fee0754

Please sign in to comment.