Skip to content

Commit 8351021

Browse files
authored
docs: add aroundEach/aroundAll to the lifecycle docs (#9601)
1 parent 0d6b901 commit 8351021

File tree

1 file changed

+84
-24
lines changed

1 file changed

+84
-24
lines changed

docs/guide/lifecycle.md

Lines changed: 84 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,17 @@ The execution follows this order:
126126

127127
1. **File-level code** - All code outside `describe` blocks runs immediately
128128
2. **Test collection** - `describe` blocks are processed, and tests are registered as side effects of importing the test file
129-
3. **`beforeAll` hooks** - Run once before any tests in the suite
130-
4. **For each test:**
129+
3. **[`aroundAll`](/api/hooks#aroundall) hooks** - Wrap around all tests in the suite (must call `runSuite()`)
130+
4. **[`beforeAll`](/api/hooks#beforeall) hooks** - Run once before any tests in the suite
131+
5. **For each test:**
132+
- [`aroundEach`](/api/hooks#aroundeach) hooks wrap around the test (must call `runTest()`)
131133
- `beforeEach` hooks execute (in order defined, or based on [`sequence.hooks`](/config/sequence#sequence-hooks))
132134
- Test function executes
133135
- `afterEach` hooks execute (reverse order by default with `sequence.hooks: 'stack'`)
134136
- [`onTestFinished`](/api/hooks#ontestfinished) callbacks run (always in reverse order)
135137
- If test failed: [`onTestFailed`](/api/hooks#ontestfailed) callbacks run
136138
- Note: if `repeats` or `retry` are set, all of these steps are executed again
137-
5. **`afterAll` hooks** - Run once after all tests in the suite complete
139+
6. **[`afterAll`](/api/hooks#afterall) hooks** - Run once after all tests in the suite complete
138140

139141
**Example execution flow:**
140142

@@ -146,11 +148,25 @@ describe('User API', () => {
146148
// This runs immediately (collection phase)
147149
console.log('Suite defined')
148150

151+
aroundAll(async (runSuite) => {
152+
// Wraps around all tests in this suite
153+
console.log('aroundAll before')
154+
await runSuite()
155+
console.log('aroundAll after')
156+
})
157+
149158
beforeAll(() => {
150159
// Runs once before all tests in this suite
151160
console.log('beforeAll')
152161
})
153162

163+
aroundEach(async (runTest) => {
164+
// Wraps around each test
165+
console.log('aroundEach before')
166+
await runTest()
167+
console.log('aroundEach after')
168+
})
169+
154170
beforeEach(() => {
155171
// Runs before each test
156172
console.log('beforeEach')
@@ -180,29 +196,61 @@ describe('User API', () => {
180196
// Output:
181197
// File loaded
182198
// Suite defined
183-
// beforeAll
184-
// beforeEach
185-
// test 1
186-
// afterEach
187-
// beforeEach
188-
// test 2
189-
// afterEach
190-
// afterAll
199+
// aroundAll before
200+
// beforeAll
201+
// aroundEach before
202+
// beforeEach
203+
// test 1
204+
// afterEach
205+
// aroundEach after
206+
// aroundEach before
207+
// beforeEach
208+
// test 2
209+
// afterEach
210+
// aroundEach after
211+
// afterAll
212+
// aroundAll after
191213
```
192214

193215
#### Nested Suites
194216

195-
When using nested `describe` blocks, hooks follow a hierarchical pattern:
217+
When using nested `describe` blocks, hooks follow a hierarchical pattern. The `aroundAll` and `aroundEach` hooks wrap around their respective scopes, with parent hooks wrapping child hooks:
196218

197219
```ts
198220
describe('outer', () => {
221+
aroundAll(async (runSuite) => {
222+
console.log('outer aroundAll before')
223+
await runSuite()
224+
console.log('outer aroundAll after')
225+
})
226+
199227
beforeAll(() => console.log('outer beforeAll'))
228+
229+
aroundEach(async (runTest) => {
230+
console.log('outer aroundEach before')
231+
await runTest()
232+
console.log('outer aroundEach after')
233+
})
234+
200235
beforeEach(() => console.log('outer beforeEach'))
201236

202237
test('outer test', () => console.log('outer test'))
203238

204239
describe('inner', () => {
240+
aroundAll(async (runSuite) => {
241+
console.log('inner aroundAll before')
242+
await runSuite()
243+
console.log('inner aroundAll after')
244+
})
245+
205246
beforeAll(() => console.log('inner beforeAll'))
247+
248+
aroundEach(async (runTest) => {
249+
console.log('inner aroundEach before')
250+
await runTest()
251+
console.log('inner aroundEach after')
252+
})
253+
206254
beforeEach(() => console.log('inner beforeEach'))
207255

208256
test('inner test', () => console.log('inner test'))
@@ -216,18 +264,28 @@ describe('outer', () => {
216264
})
217265

218266
// Output:
219-
// outer beforeAll
220-
// outer beforeEach
221-
// outer test
222-
// outer afterEach
223-
// inner beforeAll
224-
// outer beforeEach
225-
// inner beforeEach
226-
// inner test
227-
// inner afterEach (with stack mode)
228-
// outer afterEach (with stack mode)
229-
// inner afterAll
230-
// outer afterAll
267+
// outer aroundAll before
268+
// outer beforeAll
269+
// outer aroundEach before
270+
// outer beforeEach
271+
// outer test
272+
// outer afterEach
273+
// outer aroundEach after
274+
// inner aroundAll before
275+
// inner beforeAll
276+
// outer aroundEach before
277+
// inner aroundEach before
278+
// outer beforeEach
279+
// inner beforeEach
280+
// inner test
281+
// inner afterEach
282+
// outer afterEach
283+
// inner aroundEach after
284+
// outer aroundEach after
285+
// inner afterAll
286+
// inner aroundAll after
287+
// outer afterAll
288+
// outer aroundAll after
231289
```
232290

233291
#### Concurrent Tests
@@ -278,7 +336,9 @@ Understanding where code executes is crucial for avoiding common pitfalls:
278336
| Global Setup | Main process | ❌ No (use `provide`/`inject`) | Once per Vitest run |
279337
| Setup Files | Worker (same as tests) | ✅ Yes | Before each test file |
280338
| File-level code | Worker | ✅ Yes | Once per test file |
339+
| `aroundAll` | Worker | ✅ Yes | Once per suite (wraps all tests) |
281340
| `beforeAll` / `afterAll` | Worker | ✅ Yes | Once per suite |
341+
| `aroundEach` | Worker | ✅ Yes | Per test (wraps each test) |
282342
| `beforeEach` / `afterEach` | Worker | ✅ Yes | Per test |
283343
| Test function | Worker | ✅ Yes | Once (or more with retries/repeats) |
284344
| Global Teardown | Main process | ❌ No | Once per Vitest run |

0 commit comments

Comments
 (0)