Skip to content

Commit

Permalink
fix(runner): run onTestFinished and onTestFailed during retry a…
Browse files Browse the repository at this point in the history
…nd `repeats` (#6609)
  • Loading branch information
hi-ogawa authored Oct 2, 2024
1 parent 5f75790 commit c5e2909
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 21 deletions.
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",
]
`)
})
})

0 comments on commit c5e2909

Please sign in to comment.