Skip to content

Commit 37b6a3f

Browse files
committed
Export and test ensureTestShellAfterHook
1 parent ce2f446 commit 37b6a3f

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

packages/e2e-tests/test/test-shell.spec.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { TestShell } from './test-shell';
1+
import { ensureTestShellAfterHook, TestShell } from './test-shell';
22

33
describe('TestShell', function () {
4-
context('sub-suite', function () {
4+
context('hooks and tests', function () {
55
before(async function () {
66
const shell = this.startTestShell({ args: ['--nodb'] });
77
await shell.waitForPrompt();
@@ -28,6 +28,24 @@ describe('TestShell', function () {
2828
});
2929
});
3030

31+
context('adding an after each running after cleanup', function () {
32+
beforeEach(async function () {
33+
const shell = this.startTestShell({ args: ['--nodb'] });
34+
await shell.waitForPrompt();
35+
});
36+
37+
// Calling this before the "afterEach" below, ensures the cleanup hook gets added before it
38+
ensureTestShellAfterHook('afterEach', this);
39+
40+
afterEach(function () {
41+
TestShell.assertNoOpenShells();
42+
});
43+
44+
it('works', function () {
45+
/* */
46+
});
47+
});
48+
3149
after(function () {
3250
TestShell.assertNoOpenShells();
3351
});

packages/e2e-tests/test/test-shell.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -359,17 +359,19 @@ type AfterEachInjectedSuite = {
359359
};
360360

361361
/**
362-
* Registers an after (all or each) hook to kill test shells started during the hooks or tests
362+
* Registers an after (all or each) hook to kill test shells started during the hooks or tests.
363+
* You don't have to call this from tests, but you can if you want to register an after (each) hook
364+
* which runs after the shells have been killed.
363365
*/
364-
function ensureAfterHook(
366+
export function ensureTestShellAfterHook(
365367
hookName: 'afterEach',
366368
suite: Mocha.Suite
367369
): asserts suite is AfterEachInjectedSuite & Mocha.Suite;
368-
function ensureAfterHook(
370+
export function ensureTestShellAfterHook(
369371
hookName: 'afterAll',
370372
suite: Mocha.Suite
371373
): asserts suite is AfterAllInjectedSuite & Mocha.Suite;
372-
function ensureAfterHook(
374+
export function ensureTestShellAfterHook(
373375
hookName: 'afterEach' | 'afterAll',
374376
suite: Partial<AfterAllInjectedSuite & AfterEachInjectedSuite> & Mocha.Suite
375377
): void {
@@ -409,19 +411,19 @@ Mocha.Context.prototype.startTestShell = function (
409411
runnable.originalTitle === '"before each" hook' ||
410412
runnable.originalTitle === '"after each" hook'
411413
) {
412-
ensureAfterHook('afterEach', parent);
414+
ensureTestShellAfterHook('afterEach', parent);
413415
parent[TEST_SHELLS_AFTER_EACH].add(shell);
414416
} else if (
415417
runnable.originalTitle === '"before all" hook' ||
416418
runnable.originalTitle === '"after all" hook'
417419
) {
418-
ensureAfterHook('afterAll', parent);
420+
ensureTestShellAfterHook('afterAll', parent);
419421
parent[TEST_SHELLS_AFTER_ALL].add(shell);
420422
} else {
421423
throw new Error(`Unexpected ${runnable.originalTitle || runnable.title}`);
422424
}
423425
} else if (runnable instanceof Mocha.Test) {
424-
ensureAfterHook('afterEach', parent);
426+
ensureTestShellAfterHook('afterEach', parent);
425427
parent[TEST_SHELLS_AFTER_EACH].add(shell);
426428
} else {
427429
throw new Error('Unexpected Runnable: Expected a Hook or a Test');

0 commit comments

Comments
 (0)