{"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/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/browser/src/client/tester/context.ts#L301-L337","documentation":"page.screenshot() refuses to run when the active test is marked concurrent (currentTest.concurrent === true). Concurrent browser tests share the same iframe DOM, so a screenshot from one would capture state mutated by sibling tests, producing flaky/misleading images. Defined at packages/browser/src/client/tester/context.ts:318-324.","triggerScenarios":"Declaring a test with it.concurrent(...) or test.concurrent(...) and then calling page.screenshot() inside its body.","commonSituations":"Enabling concurrency for speed in browser suites, then adding visual regression screenshots; copying a concurrent test pattern from node tests into browser tests.","solutions":["Remove .concurrent from the test that needs a screenshot, or split the screenshot into a separate non-concurrent test.","If concurrency is set globally via sequence.concurrent: true, mark the screenshot test with it.sequentialOnly / it.not.concurrent.","Reorganize: run assertion-heavy tests concurrently and screenshot tests in a sequential describe block."],"exampleFix":"// before\nit.concurrent('renders', async () => {\n  await page.screenshot()\n})\n// after\nit('renders', async () => {\n  await page.screenshot()\n})","handlingStrategy":"validation","validationCode":"import { getWorkerState } from 'vitest'\nconst current = getWorkerState().current\nif (current && !current.concurrent) {\n  await page.screenshot()\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Don't combine .concurrent with screenshots in the same test.","Use describe.sequential around screenshot tests if global concurrency is enabled."],"tags":["browser","screenshot","concurrency","iframe"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}