From 759b28080a20fbdf98c6ae52c44d6ca082e8f3c3 Mon Sep 17 00:00:00 2001 From: Jack Franklin Date: Thu, 18 Jun 2020 16:26:30 +0100 Subject: [PATCH] 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. --- mocha-config/puppeteer-unit-tests.js | 5 ++- package.json | 2 +- test/coverage-utils.js | 21 ++++----- test/mocha-utils.js | 65 +++++++++++++++++----------- 4 files changed, 55 insertions(+), 38 deletions(-) diff --git a/mocha-config/puppeteer-unit-tests.js b/mocha-config/puppeteer-unit-tests.js index 01dcd44e..23fb4f8c 100644 --- a/mocha-config/puppeteer-unit-tests.js +++ b/mocha-config/puppeteer-unit-tests.js @@ -18,9 +18,10 @@ const base = require('./base'); module.exports = { ...base, - file: ['./test/mocha-utils.js'], + require: ['./test/mocha-utils.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. - retries: 2, + retries: process.env.CI ? 2 : 0, timeout: 25 * 1000, }; diff --git a/package.json b/package.json index 0384659e..abfac2c8 100644 --- a/package.json +++ b/package.json @@ -84,7 +84,7 @@ "expect": "^25.2.7", "jpeg-js": "^0.3.7", "minimist": "^1.2.0", - "mocha": "^7.1.1", + "mocha": "^8.0.1", "ncp": "^2.0.0", "pixelmatch": "^4.0.2", "pngjs": "^5.0.0", diff --git a/test/coverage-utils.js b/test/coverage-utils.js index cff80d7e..cd490e9f 100644 --- a/test/coverage-utils.js +++ b/test/coverage-utils.js @@ -102,16 +102,17 @@ const trackCoverage = () => { clearOldCoverage(); const coverageMap = new Map(); - before(() => { - const api = require('../lib/api'); - const events = require('../lib/common/Events'); - for (const [className, classType] of Object.entries(api)) - traceAPICoverage(coverageMap, events, className, classType); - }); - - after(() => { - writeCoverage(coverageMap); - }); + return { + beforeAll: () => { + const api = require('../lib/api'); + const events = require('../lib/common/Events'); + for (const [className, classType] of Object.entries(api)) + traceAPICoverage(coverageMap, events, className, classType); + }, + afterAll: () => { + writeCoverage(coverageMap); + }, + }; }; module.exports = { diff --git a/test/mocha-utils.js b/test/mocha-utils.js index 469545e2..d72b1803 100644 --- a/test/mocha-utils.js +++ b/test/mocha-utils.js @@ -140,7 +140,14 @@ global.describeChromeOnly = (...args) => { if (isChrome) return describe(...args); }; -if (process.env.COVERAGE) trackCoverage(); +let coverageHooks = { + beforeAll: () => {}, + afterAll: () => {}, +}; + +if (process.env.COVERAGE) { + coverageHooks = trackCoverage(); +} console.log( `Running unit tests with: @@ -176,31 +183,39 @@ exports.setupTestPageAndContextHooks = () => { }); }; -before(async () => { - const { server, httpsServer } = await setupServer(); +exports.mochaHooks = { + beforeAll: [ + async () => { + const { server, httpsServer } = await setupServer(); - state.puppeteer = puppeteer; - state.defaultBrowserOptions = defaultBrowserOptions; - state.server = server; - state.httpsServer = httpsServer; - state.isFirefox = isFirefox; - state.isChrome = isChrome; - state.isHeadless = isHeadless; - state.puppeteerPath = path.resolve(path.join(__dirname, '..')); -}); + state.puppeteer = puppeteer; + state.defaultBrowserOptions = defaultBrowserOptions; + state.server = server; + state.httpsServer = httpsServer; + state.isFirefox = isFirefox; + state.isChrome = isChrome; + state.isHeadless = isHeadless; + state.puppeteerPath = path.resolve(path.join(__dirname, '..')); + }, + coverageHooks.beforeAll, + ], -beforeEach(async () => { - state.server.reset(); - state.httpsServer.reset(); -}); + beforeEach: async () => { + state.server.reset(); + state.httpsServer.reset(); + }, -after(async () => { - await state.server.stop(); - state.server = null; - await state.httpsServer.stop(); - state.httpsServer = null; -}); + afterAll: [ + async () => { + await state.server.stop(); + state.server = null; + await state.httpsServer.stop(); + state.httpsServer = null; + }, + coverageHooks.afterAll, + ], -afterEach(() => { - sinon.restore(); -}); + afterEach: () => { + sinon.restore(); + }, +};