-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
96 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,26 @@ | ||
import type { SuiteHooks } from '../types' | ||
import type { OnTestFailedHandler, SuiteHooks, Test } from '../types' | ||
import { getDefaultHookTimeout, withTimeout } from './context' | ||
import { getCurrentSuite } from './suite' | ||
import { getCurrentTest } from './test-state' | ||
|
||
// suite hooks | ||
export const beforeAll = (fn: SuiteHooks['beforeAll'][0], timeout?: number) => getCurrentSuite().on('beforeAll', withTimeout(fn, timeout ?? getDefaultHookTimeout(), true)) | ||
export const afterAll = (fn: SuiteHooks['afterAll'][0], timeout?: number) => getCurrentSuite().on('afterAll', withTimeout(fn, timeout ?? getDefaultHookTimeout(), true)) | ||
export const beforeEach = <ExtraContext = {}>(fn: SuiteHooks<ExtraContext>['beforeEach'][0], timeout?: number) => getCurrentSuite<ExtraContext>().on('beforeEach', withTimeout(fn, timeout ?? getDefaultHookTimeout(), true)) | ||
export const afterEach = <ExtraContext = {}>(fn: SuiteHooks<ExtraContext>['afterEach'][0], timeout?: number) => getCurrentSuite<ExtraContext>().on('afterEach', withTimeout(fn, timeout ?? getDefaultHookTimeout(), true)) | ||
|
||
export const onTestFailed = createTestHook<OnTestFailedHandler>('onTestFailed', (test, handler) => { | ||
test.onFailed ||= [] | ||
test.onFailed.push(handler) | ||
}) | ||
|
||
function createTestHook<T>(name: string, handler: (test: Test, handler: T) => void) { | ||
return (fn: T) => { | ||
const current = getCurrentTest() | ||
|
||
if (!current) | ||
throw new Error(`Hook ${name}() can only be called inside a test`) | ||
|
||
handler(current, fn) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { Test } from '../types' | ||
|
||
let _test: Test | undefined | ||
|
||
export function setCurrentTest(test: Test | undefined) { | ||
_test = test | ||
} | ||
|
||
export function getCurrentTest() { | ||
return _test | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { expect, it, onTestFailed } from 'vitest' | ||
|
||
const collected: any[] = [] | ||
|
||
it.fails('on-failed', () => { | ||
const square3 = 3 ** 2 | ||
const square4 = 4 ** 2 | ||
|
||
onTestFailed(() => { | ||
// eslint-disable-next-line no-console | ||
console.log('Unexpected error encountered, internal states:', { square3, square4 }) | ||
collected.push({ square3, square4 }) | ||
}) | ||
|
||
expect(Math.sqrt(square3 + square4)).toBe(4) | ||
}) | ||
|
||
it('after', () => { | ||
expect(collected).toMatchInlineSnapshot(` | ||
[ | ||
{ | ||
"square3": 9, | ||
"square4": 16, | ||
}, | ||
] | ||
`) | ||
}) |