Skip to content

Commit

Permalink
fix: always run onTestFinished in reverse order
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Apr 23, 2024
1 parent e0ae829 commit 1ef5e36
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
4 changes: 4 additions & 0 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,10 @@ test('performs an organization query', async () => {
})
```
::: tip
This hook is always called in reverse order and is not affected by [`sequence.hooks`](/config/#sequence-hooks) option.
:::
### onTestFailed
This hook is called only after the test has failed. It is called after `afterEach` hooks since they can influence the test result. It receives a `TaskResult` object with the current test result. This hook is useful for debugging.
Expand Down
4 changes: 4 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1865,6 +1865,10 @@ Changes the order in which hooks are executed.
- `list` will order all hooks in the order they are defined
- `parallel` will run hooks in a single group in parallel (hooks in parent suites will still run before the current suite's hooks)
::: tip
This option doesn't affect [`onTestFinished`](/api/#ontestfinished). It is always called in reverse order.
:::
#### sequence.setupFiles <Badge type="info">0.29.3+</Badge> {#sequence-setupfiles}
- **Type**: `'list' | 'parallel'`
Expand Down
18 changes: 16 additions & 2 deletions packages/runner/src/run.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import limit from 'p-limit'
import type { Awaitable } from '@vitest/utils'
import { getSafeTimers, shuffle } from '@vitest/utils'
import { processError } from '@vitest/utils/error'
import type { DiffOptions } from '@vitest/utils/diff'
Expand Down Expand Up @@ -33,6 +34,19 @@ function getSuiteHooks(suite: Suite, name: keyof SuiteHooks, sequence: SequenceH
return hooks
}

async function callTaskHooks(task: Task, hooks: ((result: TaskResult) => Awaitable<void>)[], sequence: SequenceHooks) {
if (sequence === 'stack')
hooks = hooks.slice().reverse()

if (sequence === 'parallel') {
await Promise.all(hooks.map(fn => fn(task.result!)))
}
else {
for (const fn of hooks)
await fn(task.result!)
}
}

export async function callSuiteHook<T extends keyof SuiteHooks>(
suite: Suite,
currentTask: Task,
Expand Down Expand Up @@ -211,15 +225,15 @@ export async function runTest(test: Test | Custom, runner: VitestRunner) {
}

try {
await Promise.all(test.onFinished?.map(fn => fn(test.result!)) || [])
await callTaskHooks(test, test.onFinished || [], 'stack')
}
catch (e) {
failTask(test.result, e, runner.config.diffOptions)
}

if (test.result.state === 'fail') {
try {
await Promise.all(test.onFailed?.map(fn => fn(test.result!)) || [])
await callTaskHooks(test, test.onFailed || [], runner.config.sequence.hooks)
}
catch (e) {
failTask(test.result, e, runner.config.diffOptions)
Expand Down
13 changes: 3 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1ef5e36

Please sign in to comment.