chore: upgrade to Mocha v8 (#5997)

* chore: upgrade to Mocha v8

Mocha v8 has some nice improvements and also unlocks the potential
to have parallel test runs which may speed up CI.
This commit is contained in:
Jack Franklin 2020-06-18 16:26:30 +01:00 committed by GitHub
parent 9522f80116
commit 759b28080a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 38 deletions

View File

@ -18,9 +18,10 @@ const base = require('./base');
module.exports = { module.exports = {
...base, ...base,
file: ['./test/mocha-utils.js'], require: ['./test/mocha-utils.js'],
spec: 'test/*.spec.js', spec: 'test/*.spec.js',
parallel: process.env.CI && !process.env.COVERAGE,
// retry twice more, so we run each test up to 3 times if needed. // retry twice more, so we run each test up to 3 times if needed.
retries: 2, retries: process.env.CI ? 2 : 0,
timeout: 25 * 1000, timeout: 25 * 1000,
}; };

View File

@ -84,7 +84,7 @@
"expect": "^25.2.7", "expect": "^25.2.7",
"jpeg-js": "^0.3.7", "jpeg-js": "^0.3.7",
"minimist": "^1.2.0", "minimist": "^1.2.0",
"mocha": "^7.1.1", "mocha": "^8.0.1",
"ncp": "^2.0.0", "ncp": "^2.0.0",
"pixelmatch": "^4.0.2", "pixelmatch": "^4.0.2",
"pngjs": "^5.0.0", "pngjs": "^5.0.0",

View File

@ -102,16 +102,17 @@ const trackCoverage = () => {
clearOldCoverage(); clearOldCoverage();
const coverageMap = new Map(); const coverageMap = new Map();
before(() => { return {
const api = require('../lib/api'); beforeAll: () => {
const events = require('../lib/common/Events'); const api = require('../lib/api');
for (const [className, classType] of Object.entries(api)) const events = require('../lib/common/Events');
traceAPICoverage(coverageMap, events, className, classType); for (const [className, classType] of Object.entries(api))
}); traceAPICoverage(coverageMap, events, className, classType);
},
after(() => { afterAll: () => {
writeCoverage(coverageMap); writeCoverage(coverageMap);
}); },
};
}; };
module.exports = { module.exports = {

View File

@ -140,7 +140,14 @@ global.describeChromeOnly = (...args) => {
if (isChrome) return describe(...args); if (isChrome) return describe(...args);
}; };
if (process.env.COVERAGE) trackCoverage(); let coverageHooks = {
beforeAll: () => {},
afterAll: () => {},
};
if (process.env.COVERAGE) {
coverageHooks = trackCoverage();
}
console.log( console.log(
`Running unit tests with: `Running unit tests with:
@ -176,31 +183,39 @@ exports.setupTestPageAndContextHooks = () => {
}); });
}; };
before(async () => { exports.mochaHooks = {
const { server, httpsServer } = await setupServer(); beforeAll: [
async () => {
const { server, httpsServer } = await setupServer();
state.puppeteer = puppeteer; state.puppeteer = puppeteer;
state.defaultBrowserOptions = defaultBrowserOptions; state.defaultBrowserOptions = defaultBrowserOptions;
state.server = server; state.server = server;
state.httpsServer = httpsServer; state.httpsServer = httpsServer;
state.isFirefox = isFirefox; state.isFirefox = isFirefox;
state.isChrome = isChrome; state.isChrome = isChrome;
state.isHeadless = isHeadless; state.isHeadless = isHeadless;
state.puppeteerPath = path.resolve(path.join(__dirname, '..')); state.puppeteerPath = path.resolve(path.join(__dirname, '..'));
}); },
coverageHooks.beforeAll,
],
beforeEach(async () => { beforeEach: async () => {
state.server.reset(); state.server.reset();
state.httpsServer.reset(); state.httpsServer.reset();
}); },
after(async () => { afterAll: [
await state.server.stop(); async () => {
state.server = null; await state.server.stop();
await state.httpsServer.stop(); state.server = null;
state.httpsServer = null; await state.httpsServer.stop();
}); state.httpsServer = null;
},
coverageHooks.afterAll,
],
afterEach(() => { afterEach: () => {
sinon.restore(); sinon.restore();
}); },
};