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

Function runTest

packages/runner/src/run.ts:584–785  ·  view source on GitHub ↗
(test: Test, runner: VitestRunner)

Source from the content-addressed store, hash-verified

582}
583
584export async function runTest(test: Test, runner: VitestRunner): Promise<void> {
585 await runner.onBeforeRunTask?.(test)
586
587 if (test.mode !== 'run' && test.mode !== 'queued') {
588 updateTask('test-prepare', test, runner)
589 updateTask('test-finished', test, runner)
590 return
591 }
592
593 if (test.result?.state === 'fail') {
594 // should not be possible to get here, I think this is just copy pasted from suite
595 // TODO: maybe someone fails tests in `beforeAll` hooks?
596 // https://github.com/vitest-dev/vitest/pull/7069
597 updateTask('test-failed-early', test, runner)
598 return
599 }
600
601 const start = now()
602
603 test.result = {
604 state: 'run',
605 startTime: unixNow(),
606 retryCount: 0,
607 }
608 updateTask('test-prepare', test, runner)
609
610 const cleanupRunningTest = addRunningTest(test)
611 setCurrentTest(test)
612
613 const suite = test.suite || test.file
614 const $ = runner.trace!
615
616 const repeats = test.repeats ?? 0
617 for (let repeatCount = 0; repeatCount <= repeats; repeatCount++) {
618 const retry = getRetryCount(test.retry)
619 for (let retryCount = 0; retryCount <= retry; retryCount++) {
620 let beforeEachCleanups: unknown[] = []
621 // fixtureCheckpoint is passed by callAroundEachHooks - it represents the count
622 // of fixture cleanup functions AFTER all aroundEach fixtures have been resolved
623 // but BEFORE the test runs. This allows us to clean up only fixtures created
624 // inside runTest while preserving aroundEach fixtures for teardown.
625 await callAroundEachHooks(suite, test, async (fixtureCheckpoint) => {
626 try {
627 await runner.onBeforeTryTask?.(test, {
628 retry: retryCount,
629 repeats: repeatCount,
630 })
631
632 test.result!.repeatCount = repeatCount
633
634 beforeEachCleanups = await $('test.beforeEach', () => callSuiteHook(
635 suite,
636 test,
637 'beforeEach',
638 runner,
639 [test.context, suite],
640 ))
641

Callers 3

callAroundEachHooksFunction · 0.85
runSuiteChildFunction · 0.85

Calls 15

addRunningTestFunction · 0.90
setCurrentTestFunction · 0.90
getFnFunction · 0.90
callFixtureCleanupFromFunction · 0.90
callFixtureCleanupFunction · 0.90
processErrorFunction · 0.90
getRetryCountFunction · 0.85
callAroundEachHooksFunction · 0.85
callSuiteHookFunction · 0.85
failTaskFunction · 0.85
callCleanupHooksFunction · 0.85
callTestHooksFunction · 0.85

Tested by

no test coverage detected