···34343535Here, the `beforeEach` ensures that user is added for each test.
36363737-`beforeEach` can also return an optional cleanup function (equivalent to [`afterEach`](#aftereach)):
3737+`beforeEach` can also return an optional cleanup function. It's similar to [`afterEach`](#aftereach). The only difference is that it's executed after all other `afterEach` hooks:
38383939```ts
4040import { beforeEach } from 'vitest'
···4343 // called once before each test run
4444 await prepareSomething()
45454646- // clean up function, called once after each test run
4646+ // clean up function, called once after each test run, after afterEach hooks
4747 return async () => {
4848 await resetSomething()
4949 }
···102102103103Here the `beforeAll` ensures that the mock data is set up before tests run.
104104105105-`beforeAll` can also return an optional cleanup function (equivalent to [`afterAll`](#afterall)):
105105+`beforeAll` can also return an optional cleanup function. It's similar to [`afterAll`](#afterall). The only difference is that it's executed after all other `afterAll` hooks:
106106107107```ts
108108import { beforeAll } from 'vitest'
···111111 // called once before all tests run
112112 await startMocking()
113113114114- // clean up function, called once after all tests run
114114+ // clean up function, called once after all tests run, after afterAll hooks
115115 return async () => {
116116 await stopMocking()
117117 }
+15
docs/guide/lifecycle.md
···137137 - `beforeEach` hooks execute (in order defined, or based on [`sequence.hooks`](/config/sequence#sequence-hooks))
138138 - Test function executes
139139 - `afterEach` hooks execute (reverse order by default with `sequence.hooks: 'stack'`)
140140+ - Cleanup functions returned from `beforeEach` hooks execute (reverse order by default with `sequence.hooks: 'stack'`)
140141 - [`onTestFinished`](/api/hooks#ontestfinished) callbacks run (always in reverse order)
141142 - If test failed: [`onTestFailed`](/api/hooks#ontestfailed) callbacks run
142143 - Note: if `repeats` or `retry` are set, all of these steps are executed again
1431446. **[`afterAll`](/api/hooks#afterall) hooks:** Run once after all tests in the suite complete
145145+7. **Cleanup functions returned from `beforeAll` hooks:** Run once after all tests in the suite complete
144146145147**Example execution flow:**
146148···162164 beforeAll(() => {
163165 // Runs once before all tests in this suite
164166 console.log('beforeAll')
167167+168168+ return function beforeAllCleanup() {
169169+ // Runs once afterAll hooks have run
170170+ console.log('beforeAllCleanup')
171171+ }
165172 })
166173167174 aroundEach(async (runTest) => {
···174181 beforeEach(() => {
175182 // Runs before each test
176183 console.log('beforeEach')
184184+185185+ return function beforeEachCleanup() {
186186+ // Runs after afterEach hooks have run
187187+ console.log('beforeEachCleanup')
188188+ }
177189 })
178190179191 test('creates user', () => {
···206218// beforeEach
207219// test 1
208220// afterEach
221221+// beforeEachCleanup
209222// aroundEach after
210223// aroundEach before
211224// beforeEach
212225// test 2
213226// afterEach
227227+// beforeEachCleanup
214228// aroundEach after
215229// afterAll
230230+// beforeAllCleanup
216231// aroundAll after
217232```
218233