-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(documentation will be in another PR) Adds "root hook plugins", a system to define root hooks via files loaded with `--require`. This enables root hooks to work in parallel mode. Because parallel mode runs files in a non-deterministic order, and files do not share a `Mocha` instance, it is not possible to share these hooks with other test files. This change also works well with third-party libraries for Mocha which need the behavior; these can now be trivially consumed by adding `--require` or `require: 'some-library'` in Mocha's config file. The way it works is: 1. When a file is loaded via `--require`, we check to see if that file exports a property named `mochaHooks` (can be multiple files). 1. If it does, we save a reference to the property. 1. After Yargs' validation phase, we use async middleware to execute root hook plugin functions--or if they are objects, just collect them--and we flatten all hooks found into four buckets corresponding to the four hook types. 1. Once `Mocha` is instantiated, if it is given a `rootHooks` option, those hooks are applied to the root suite. This works with parallel tests because we can save a reference to the flattened hooks in each worker process, and a new `Mocha` instance is created with them for each test file. * * * Tangential: - Because a root hook plugin can be defined as an `async` function, I noticed that `utils.type()` does not return `function` for async functions; it returns `asyncfunction`. I've added a (Node-specific, for now) test for this. - `handleRequires` is now `async`, since it will need to be anyway to support ESM and calls to `import()`. - fixed incorrect call to `fs.existsSync()` Ref: #4198
- Loading branch information
Showing
15 changed files
with
500 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
test/integration/fixtures/options/require/root-hook-defs-a.fixture.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use strict'; | ||
|
||
exports.mochaHooks = { | ||
beforeAll() { | ||
console.log('beforeAll'); | ||
}, | ||
beforeEach() { | ||
console.log('beforeEach'); | ||
}, | ||
afterAll() { | ||
console.log('afterAll'); | ||
}, | ||
afterEach() { | ||
console.log('afterEach'); | ||
} | ||
}; |
36 changes: 36 additions & 0 deletions
36
test/integration/fixtures/options/require/root-hook-defs-b.fixture.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict'; | ||
|
||
exports.mochaHooks = { | ||
beforeAll: [ | ||
function() { | ||
console.log('beforeAll array 1'); | ||
}, | ||
function() { | ||
console.log('beforeAll array 2'); | ||
} | ||
], | ||
beforeEach: [ | ||
function() { | ||
console.log('beforeEach array 1'); | ||
}, | ||
function() { | ||
console.log('beforeEach array 2'); | ||
} | ||
], | ||
afterAll: [ | ||
function() { | ||
console.log('afterAll array 1'); | ||
}, | ||
function() { | ||
console.log('afterAll array 2'); | ||
} | ||
], | ||
afterEach: [ | ||
function() { | ||
console.log('afterEach array 1'); | ||
}, | ||
function() { | ||
console.log('afterEach array 2'); | ||
} | ||
] | ||
}; |
16 changes: 16 additions & 0 deletions
16
test/integration/fixtures/options/require/root-hook-defs-c.fixture.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use strict'; | ||
|
||
exports.mochaHooks = async () => ({ | ||
beforeAll() { | ||
console.log('beforeAll'); | ||
}, | ||
beforeEach() { | ||
console.log('beforeEach'); | ||
}, | ||
afterAll() { | ||
console.log('afterAll'); | ||
}, | ||
afterEach() { | ||
console.log('afterEach'); | ||
} | ||
}); |
36 changes: 36 additions & 0 deletions
36
test/integration/fixtures/options/require/root-hook-defs-d.fixture.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict'; | ||
|
||
exports.mochaHooks = async() => ({ | ||
beforeAll: [ | ||
function() { | ||
console.log('beforeAll array 1'); | ||
}, | ||
function() { | ||
console.log('beforeAll array 2'); | ||
} | ||
], | ||
beforeEach: [ | ||
function() { | ||
console.log('beforeEach array 1'); | ||
}, | ||
function() { | ||
console.log('beforeEach array 2'); | ||
} | ||
], | ||
afterAll: [ | ||
function() { | ||
console.log('afterAll array 1'); | ||
}, | ||
function() { | ||
console.log('afterAll array 2'); | ||
} | ||
], | ||
afterEach: [ | ||
function() { | ||
console.log('afterEach array 1'); | ||
}, | ||
function() { | ||
console.log('afterEach array 2'); | ||
} | ||
] | ||
}); |
6 changes: 6 additions & 0 deletions
6
test/integration/fixtures/options/require/root-hook-test-2.fixture.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// run with --require root-hook-defs-a.fixture.js --require | ||
// root-hook-defs-b.fixture.js | ||
|
||
it('should also have some root hooks', function() { | ||
// test | ||
}); |
6 changes: 6 additions & 0 deletions
6
test/integration/fixtures/options/require/root-hook-test.fixture.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// run with --require root-hook-defs-a.fixture.js --require | ||
// root-hook-defs-b.fixture.js | ||
|
||
it('should have some root hooks', function() { | ||
// test | ||
}); |
Oops, something went wrong.