MCPcopy
hub / github.com/vitest-dev/vitest / beforeEach

Function beforeEach

packages/runner/src/hooks.ts:154–182  ·  view source on GitHub ↗
(
  fn: BeforeEachListener<ExtraContext>,
  timeout: number = getDefaultHookTimeout(),
)

Source from the content-addressed store, hash-verified

152 * ```
153 */
154export function beforeEach<ExtraContext = object>(
155 fn: BeforeEachListener<ExtraContext>,
156 timeout: number = getDefaultHookTimeout(),
157): void {
158 assertTypes(fn, '"beforeEach" callback', ['function'])
159 const stackTraceError = new Error('STACK_TRACE_ERROR')
160
161 const wrapper: BeforeEachListener<ExtraContext> = (context, suite) => {
162 const fixtureResolver = withFixtures(fn, { suite })
163 return fixtureResolver(context)
164 }
165
166 return getCurrentSuite<ExtraContext>().on(
167 'beforeEach',
168 Object.assign(
169 withTimeout(
170 wrapper,
171 timeout ?? getDefaultHookTimeout(),
172 true,
173 stackTraceError,
174 abortIfTimeout,
175 ),
176 {
177 [CLEANUP_TIMEOUT_KEY]: timeout,
178 [CLEANUP_STACK_TRACE_KEY]: stackTraceError,
179 },
180 ),
181 )
182}
183
184/**
185 * Registers a callback function to be executed after each test within the current suite has completed.

Callers 15

ViewReport.spec.tsFile · 0.85
dom.test.tsFile · 0.85
userEvent.test.tsFile · 0.85
utils.test.tsFile · 0.85
basic.test.tsFile · 0.85
basic.test.tsxFile · 0.85
browser-setup.tsFile · 0.85
browser-setup.tsFile · 0.85

Calls 5

assertTypesFunction · 0.90
getCurrentSuiteFunction · 0.90
withTimeoutFunction · 0.90
getDefaultHookTimeoutFunction · 0.85
onMethod · 0.45

Tested by

no test coverage detected