diff --git a/types/mocha/index.d.ts b/types/mocha/index.d.ts index a89d2df658..c6bad09685 100644 --- a/types/mocha/index.d.ts +++ b/types/mocha/index.d.ts @@ -228,6 +228,13 @@ declare class Mocha { * @see https://mochajs.org/api/mocha#parallelMode */ parallelMode(enabled?: boolean): this; + + /** + * Assigns hooks to the root suite. + * + * @see https://mochajs.org/api/mocha#rootHooks + */ + rootHooks(hooks: Mocha.RootHookObject): this; } declare namespace Mocha { @@ -2101,6 +2108,37 @@ declare namespace Mocha { error(err: any): void; } + /** + * An alternative way to define root hooks that works with parallel runs. + * + * Root hooks work with any interface, but the property names do not change. + * In other words, if you are using the tdd interface, suiteSetup maps to beforeAll, and setup maps to beforeEach. + * + * As with other hooks, `this` refers to to the current context object. + * + * @see https://mochajs.org/#root-hook-plugins + */ + interface RootHookObject { + /** + * In serial mode, run after all tests end, once only. + * In parallel mode, run after all tests end, for each file. + */ + afterAll?: Func | AsyncFunc | Func[] | AsyncFunc[]; + /** + * In serial mode (Mocha's default), before all tests begin, once only. + * In parallel mode, run before all tests begin, for each file. + */ + beforeAll?: Func | AsyncFunc | Func[] | AsyncFunc[]; + /** + * In both modes, run after every test. + */ + afterEach?: Func | AsyncFunc | Func[] | AsyncFunc[]; + /** + * In both modes, run before each test. + */ + beforeEach?: Func | AsyncFunc | Func[] | AsyncFunc[]; + } + /** * Initialize a new `Test` with the given `title` and callback `fn`. * @@ -2204,7 +2242,7 @@ declare namespace Mocha { jobs?: number; /** Assigns hooks to the root suite */ - rootHooks?: any; + rootHooks?: RootHookObject; asyncOnly?: boolean; delay?: boolean; diff --git a/types/mocha/mocha-tests.ts b/types/mocha/mocha-tests.ts index 1d5ff89b7b..439e2ed72d 100644 --- a/types/mocha/mocha-tests.ts +++ b/types/mocha/mocha-tests.ts @@ -828,6 +828,15 @@ function testParallelMode() { mocha.parallelMode(); } +function testRootHooks() { + mocha.rootHooks({ + beforeAll(done) { + done(); + }, + afterEach: [done => done()], + }); +} + function testUnloadFiles() { mocha.unloadFiles(); } @@ -884,6 +893,21 @@ function test_constructor_jobs_option() { const m: Mocha = new LocalMocha({ jobs: 4 }); } +function test_constructor_root_hooks() { + const m: Mocha = new LocalMocha({ + rootHooks: { + beforeEach(done) { + done(); + }, + afterEach(done) { + done(); + }, + afterAll: [done => done()], + beforeAll: [done => done()], + }, + }); +} + function test_constructor_all_options() { const m: Mocha = new LocalMocha({ slow: 25,