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

Function cleanup

packages/browser/src/client/tester/tester.ts:259–292  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

257}
258
259async function cleanup() {
260 const state = getWorkerState()
261 const config = getConfig()
262 const rpc = state.rpc as any as BrowserRPC
263
264 const cleanupSymbol = Symbol.for('vitest:component-cleanup')
265
266 if (cleanupSymbol in page) {
267 try {
268 await (page[cleanupSymbol] as any)()
269 }
270 catch (error: any) {
271 await unhandledError(error, 'Cleanup Error')
272 }
273 }
274 // need to cleanup for each tester
275 // since playwright keyboard API is stateful on page instance level
276 await userEvent.cleanup()
277 .catch(error => unhandledError(error, 'Cleanup Error'))
278
279 await Promise.all(
280 getBrowserState().cleanups.map(fn => fn()),
281 ).catch(error => unhandledError(error, 'Cleanup Error'))
282
283 // if isolation is disabled, Vitest reuses the same iframe and we
284 // don't need to switch the context back at all
285 if (contextSwitched) {
286 await rpc.wdioSwitchContext('parent')
287 .catch(error => unhandledError(error, 'Cleanup Error'))
288 }
289 await stopCoverageInsideWorker(config.coverage, moduleRunner, { isolate: config.browser.isolate }).catch((error) => {
290 return unhandledError(error, 'Coverage Error')
291 })
292}
293
294function unhandledError(e: Error, type: string) {
295 return client.rpc.onUnhandledError({

Callers 1

tester.tsFile · 0.70

Calls 8

getWorkerStateFunction · 0.90
getConfigFunction · 0.90
getBrowserStateFunction · 0.90
unhandledErrorFunction · 0.85
stopCoverageInsideWorkerFunction · 0.85
cleanupMethod · 0.80
allMethod · 0.80
fnFunction · 0.50

Tested by

no test coverage detected