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

Function screenshot

packages/browser/src/client/tester/context.ts:310–356  ·  view source on GitHub ↗
(options = {})

Source from the content-addressed store, hash-verified

308 })
309 },
310 async screenshot(options = {}) {
311 const currentTest = getWorkerState().current
312 if (!currentTest) {
313 throw new Error('Cannot take a screenshot outside of a test.')
314 }
315
316 if (currentTest.concurrent) {
317 throw new Error(
318 'Cannot take a screenshot in a concurrent test because '
319 + 'concurrent tests run at the same time in the same iframe and affect each other\'s environment. '
320 + 'Use a non-concurrent test to take a screenshot.',
321 )
322 }
323
324 const repeatCount = currentTest.result?.repeatCount ?? 0
325 const taskName = getTaskFullName(currentTest)
326 const number = screenshotIds[repeatCount]?.[taskName] ?? 1
327
328 screenshotIds[repeatCount] ??= {}
329 screenshotIds[repeatCount][taskName] = number + 1
330
331 const name
332 = options.path || `${taskName.replace(/[^a-z0-9]/gi, '-')}-${number}.png`
333
334 const [element, ...mask] = await Promise.all([
335 options.element ? convertToSelector(options.element, options) : undefined,
336 ...('mask' in options
337 ? (options.mask as Array<Element | Locator>).map(el => convertToSelector(el, options))
338 : []),
339 ])
340
341 const normalizedOptions = 'mask' in options
342 ? { ...options, mask }
343 : options
344
345 return ensureAwaited(error => triggerCommand(
346 '__vitest_screenshot',
347 [
348 name,
349 processTimeoutOptions({
350 ...normalizedOptions,
351 element,
352 } as any /** TODO */),
353 ],
354 error,
355 ))
356 },
357 mark<T>(
358 name: string,
359 bodyOrOptions?: MarkOptions | (() => T | Promise<T>),

Callers

nothing calls this directly

Calls 7

getWorkerStateFunction · 0.90
convertToSelectorFunction · 0.90
ensureAwaitedFunction · 0.90
processTimeoutOptionsFunction · 0.90
allMethod · 0.80
getTaskFullNameFunction · 0.70
triggerCommandFunction · 0.70

Tested by

no test coverage detected