Currently, hook execution subtly differs from test execution. Hooks always execute sequentially, even if they're not marked as being serial.
Prompted by #1178, hooks are removed from test.serial in #1670. I think we should keep such hooks, but they should work properly:
- Hooks always execute in the order they're defined (by category, e.g.
before, afterEach.always, etc)
beforeEach, afterEach and afterEach.always hooks execute for each test, when the test executes
- Serial tests are executed before concurrent ones, if any
- Concurrent tests are executed after serial ones, if any, without regard for the state of other concurrent tests
- For each test,
beforeEach hooks are executed after before hooks, if any
- For each test,
afterEach hooks are executed after the test has finished
- For each test,
afterEach.always hooks are executed after afterEach hooks, if any
- For serial tests, the
beforeEach hooks are executed after the previous serial test's afterEach.always hook (or afterEach hook, or test itself, if any)
after hooks are executed after all tests have finished
after.always hooks are executed after after hooks
- Unlike tests, serial hooks are not reordered to run first
- Serial hooks are executed after preceding hooks in their category have finished
- Concurrent hooks execute concurrently, without regard for the state of other concurrent hooks (in their category)
Here, "serial" means tests and hooks explicitly marked as serial. "Concurrent" means tests and hooks not marked as serial, that execute asynchronously either by using an asynchronous method, returning a promise or observable, or using the cb interface. Synchronous tests not explicitly marked as serial may execute concurrently with previous concurrent test, even though they finish immediately.
This proposal lets us bring back hooks to test.serial, but now their execution will be properly understood. It might break some tests with multiple concurrent hooks in each category, but I expect that to be rare.
Currently, hook execution subtly differs from test execution. Hooks always execute sequentially, even if they're not marked as being serial.
Prompted by #1178, hooks are removed from
test.serialin #1670. I think we should keep such hooks, but they should work properly:before,afterEach.always, etc)beforeEach,afterEachandafterEach.alwayshooks execute for each test, when the test executesbeforeEachhooks are executed afterbeforehooks, if anyafterEachhooks are executed after the test has finishedafterEach.alwayshooks are executed afterafterEachhooks, if anybeforeEachhooks are executed after the previous serial test'safterEach.alwayshook (orafterEachhook, or test itself, if any)afterhooks are executed after all tests have finishedafter.alwayshooks are executed afterafterhooksHere, "serial" means tests and hooks explicitly marked as serial. "Concurrent" means tests and hooks not marked as serial, that execute asynchronously either by using an asynchronous method, returning a promise or observable, or using the
cbinterface. Synchronous tests not explicitly marked as serial may execute concurrently with previous concurrent test, even though they finish immediately.This proposal lets us bring back hooks to
test.serial, but now their execution will be properly understood. It might break some tests with multiple concurrent hooks in each category, but I expect that to be rare.