From 7c7ea141f01edfd9e6e0b603c6f4b7bc37c51c92 Mon Sep 17 00:00:00 2001 From: Jeremy Rose Date: Thu, 11 Jun 2020 11:35:50 -0700 Subject: [PATCH] test: extract defer helper (#24019) --- spec-main/index.js | 18 ++++++++++++++---- spec-main/spec-helpers.ts | 2 +- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/spec-main/index.js b/spec-main/index.js index a90a35a5aa3e..92c0d18d5ddf 100644 --- a/spec-main/index.js +++ b/spec-main/index.js @@ -60,6 +60,20 @@ app.whenReady().then(async () => { } const mocha = new Mocha(mochaOptions); + // The cleanup method is registered this way rather than through an + // `afterEach` at the top level so that it can run before other `afterEach` + // methods. + // + // The order of events is: + // 1. test completes, + // 2. `defer()`-ed methods run, in reverse order, + // 3. regular `afterEach` hooks run. + const { runCleanupFunctions } = require('./spec-helpers'); + mocha.suite.on('suite', function attach (suite) { + suite.afterEach('cleanup', runCleanupFunctions); + suite.on('suite', attach); + }); + if (!process.env.MOCHA_REPORTER) { mocha.ui('bdd').reporter('tap'); } @@ -110,8 +124,4 @@ app.whenReady().then(async () => { chai.use(require('dirty-chai')); const runner = mocha.run(cb); - const { runCleanupFunctions } = require('./spec-helpers'); - runner.on('test end', () => { - runCleanupFunctions(); - }); }); diff --git a/spec-main/spec-helpers.ts b/spec-main/spec-helpers.ts index 1bf85622db9f..ca1a5e78f65c 100644 --- a/spec-main/spec-helpers.ts +++ b/spec-main/spec-helpers.ts @@ -19,7 +19,7 @@ export async function runCleanupFunctions () { } export function defer (f: CleanupFunction) { - cleanupFunctions.push(f); + cleanupFunctions.unshift(f); } class RemoteControlApp {