Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runner): run onTestFinished and onTestFailed during retry and repeats #6609

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions packages/runner/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,29 @@ export async function runTest(test: Test | Custom, runner: VitestRunner): Promis
failTask(test.result, e, runner.config.diffOptions)
}

try {
await callTaskHooks(test, test.onFinished || [], 'stack')
}
catch (e) {
failTask(test.result, e, runner.config.diffOptions)
}

if (test.result.state === 'fail') {
try {
await callTaskHooks(
test,
test.onFailed || [],
runner.config.sequence.hooks,
)
}
catch (e) {
failTask(test.result, e, runner.config.diffOptions)
}
}

delete test.onFailed
delete test.onFinished

if (test.result.state === 'pass') {
break
}
Expand All @@ -286,26 +309,6 @@ export async function runTest(test: Test | Custom, runner: VitestRunner): Promis
}
}

try {
await callTaskHooks(test, test.onFinished || [], 'stack')
}
catch (e) {
failTask(test.result, e, runner.config.diffOptions)
}

if (test.result.state === 'fail') {
try {
await callTaskHooks(
test,
test.onFailed || [],
runner.config.sequence.hooks,
)
}
catch (e) {
failTask(test.result, e, runner.config.diffOptions)
}
}

// if test is marked to be failed, flip the result
if (test.fails) {
if (test.result.state === 'pass') {
Expand Down
140 changes: 139 additions & 1 deletion test/core/test/on-finished.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, it, onTestFinished } from 'vitest'
import { describe, expect, it, onTestFailed, onTestFinished } from 'vitest'

const collected: any[] = []
const multiple: any[] = []
Expand Down Expand Up @@ -59,3 +59,141 @@ it('after', () => {
expect(collected).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
expect(multiple).toEqual([4, 3, 2, 1])
})

describe('repeats pass', () => {
const state: string[] = []

it('run', { repeats: 2 }, (t) => {
const tag = `(${t.task.result?.retryCount}, ${t.task.result?.repeatCount}) `
state.push(`${tag}run`)

onTestFinished(() => {
state.push(`${tag}finish`)
})

onTestFailed(() => {
state.push(`${tag}fail`)
})
})

it('assert', () => {
expect(state).toMatchInlineSnapshot(`
[
"(0, 0) run",
"(0, 0) finish",
"(0, 1) run",
"(0, 1) finish",
"(0, 2) run",
"(0, 2) finish",
]
`)
})
})

describe('repeats fail', () => {
const state: string[] = []

it.fails('run', { repeats: 2 }, (t) => {
const tag = `(${t.task.result?.retryCount}, ${t.task.result?.repeatCount}) `
state.push(`${tag}run`)

onTestFinished(() => {
state.push(`${tag}finish`)
})

onTestFailed(() => {
state.push(`${tag}fail`)
})

if (t.task.result?.repeatCount === 1) {
throw new Error('fail')
}
})

it('assert', () => {
expect(state).toMatchInlineSnapshot(`
[
"(0, 0) run",
"(0, 0) finish",
"(0, 1) run",
"(0, 1) finish",
"(0, 1) fail",
"(0, 2) run",
"(0, 2) finish",
"(0, 2) fail",
]
`)
})
})

describe('retry pass', () => {
const state: string[] = []

it('run', { retry: 2 }, (t) => {
const tag = `(${t.task.result?.retryCount}, ${t.task.result?.repeatCount}) `
state.push(`${tag}run`)

onTestFinished(() => {
state.push(`${tag}finish`)
})

onTestFailed(() => {
state.push(`${tag}fail`)
})

if (t.task.result?.retryCount && t.task.result?.retryCount > 1) {
return
}
throw new Error('fail')
})

it('assert', () => {
expect(state).toMatchInlineSnapshot(`
[
"(0, 0) run",
"(0, 0) finish",
"(0, 0) fail",
"(1, 0) run",
"(1, 0) finish",
"(1, 0) fail",
"(2, 0) run",
"(2, 0) finish",
]
`)
})
})

describe('retry fail', () => {
const state: string[] = []

it.fails('run', { retry: 2 }, (t) => {
const tag = `(${t.task.result?.retryCount}, ${t.task.result?.repeatCount}) `
state.push(`${tag}run`)

onTestFinished(() => {
state.push(`${tag}finish`)
})

onTestFailed(() => {
state.push(`${tag}fail`)
})

throw new Error('fail')
})

it('assert', () => {
expect(state).toMatchInlineSnapshot(`
[
"(0, 0) run",
"(0, 0) finish",
"(0, 0) fail",
"(1, 0) run",
"(1, 0) finish",
"(1, 0) fail",
"(2, 0) run",
"(2, 0) finish",
"(2, 0) fail",
]
`)
})
})
Loading