{"record":{"id":"f0f19bf4746d388b","repo":"vitest-dev/vitest","slug":"cannot-take-a-screenshot-in-a-concurrent-test-beca","errorCode":null,"errorMessage":"Cannot take a screenshot in a concurrent test because concurrent tests run at the same time in the same iframe and affect each other's environment. Use a non-concurrent test to take a screenshot.","messagePattern":"Cannot take a screenshot in a concurrent test because concurrent tests run at the same time in the same iframe and affect each other's environment\\. Use a non-concurrent test to take a screenshot\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/browser/src/client/tester/context.ts","lineNumber":319,"sourceCode":"        if (e.data.event === 'viewport:done' && e.data.iframeId === id) {\n          channel.removeEventListener('message', handler)\n          resolve()\n        }\n        if (e.data.event === 'viewport:fail' && e.data.iframeId === id) {\n          channel.removeEventListener('message', handler)\n          reject(new Error(e.data.error))\n        }\n      })\n    })\n  },\n  async screenshot(options = {}) {\n    const currentTest = getWorkerState().current\n    if (!currentTest) {\n      throw new Error('Cannot take a screenshot outside of a test.')\n    }\n\n    if (currentTest.concurrent) {\n      throw new Error(\n        'Cannot take a screenshot in a concurrent test because '\n        + 'concurrent tests run at the same time in the same iframe and affect each other\\'s environment. '\n        + 'Use a non-concurrent test to take a screenshot.',\n      )\n    }\n\n    const repeatCount = currentTest.result?.repeatCount ?? 0\n    const taskName = getTaskFullName(currentTest)\n    const number = screenshotIds[repeatCount]?.[taskName] ?? 1\n\n    screenshotIds[repeatCount] ??= {}\n    screenshotIds[repeatCount][taskName] = number + 1\n\n    const name\n      = options.path || `${taskName.replace(/[^a-z0-9]/gi, '-')}-${number}.png`\n\n    const [element, ...mask] = await Promise.all([\n      options.element ? serializeElement(options.element, options) : undefined,","sourceCodeStart":301,"sourceCodeEnd":337,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/browser/src/client/tester/context.ts#L301-L337","documentation":"Concurrent tests in Vitest browser mode share the same iframe, so a screenshot would capture an interleaved DOM from sibling tests. Vitest forbids screenshots in tests marked .concurrent to prevent misleading artifacts. The guard sits at the top of page.screenshot() right after the current-test check.","triggerScenarios":"Declaring a test as it.concurrent('...', ...) (or it.concurrent.only) and calling page.screenshot() inside it; enabling test.concurrent at the suite/describe level; importing a helper that screenshots while the suite is configured concurrent.","commonSituations":"Enabling concurrent mode globally to speed up the suite; copy-pasting a screenshot helper into a concurrent suite; running with --no-isolate or concurrent config defaults.","solutions":["Remove .concurrent from the specific test that needs a screenshot (or its describe block).","Keep concurrency for fast assertions but isolate the visual test by splitting it into a separate non-concurrent suite.","Replace the screenshot with a DOM assertion (e.g. toMatchAriaSnapshot) which is safe under concurrency."],"exampleFix":"// before\nit.concurrent('shows chart', async () => { await page.screenshot() })\n// after\nit('shows chart', async () => { await page.screenshot() })","handlingStrategy":"validation","validationCode":"import { getWorkerState } from 'vitest'\nconst t = getWorkerState().current\nif (t?.concurrent) {\n  // do not screenshot in concurrent tests\n} else {\n  await page.screenshot()\n}","typeGuard":"function canScreenshot(): boolean {\n  const t = getWorkerState().current\n  return !!t && !t.concurrent\n}","tryCatchPattern":null,"preventionTips":["Do not mark screenshot tests as .concurrent.","Isolate visual tests into their own non-concurrent suite.","Prefer toMatchAriaSnapshot when running concurrent suites."],"tags":["browser","screenshot","concurrency","isolation"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}