vitest-dev/vitest · error · Error
' ' cannot be used without test context
Error message
'${getAssertionName(obj)}' cannot be used without test context What it means
Thrown by `getTest` when a snapshot assertion (`toMatchSnapshot`, `toMatchInlineSnapshot`, `toMatchFileSnapshot`, `toThrowErrorMatchingSnapshot[InlineSnapshot]`) is called but the `vitest-test` flag is missing from the Chai assertion object. Vitest attaches the active `Test` instance to that flag at assertion-creation time; without it the snapshot cannot be attributed to a test file/name, so the call is rejected. The message names the offending assertion.
Solutions
- Move the `expect(...).toMatchSnapshot()` call inside an `it`/`test` body so the test context is active.
- If inside an async callback, ensure it is awaited within the test so the context is preserved.
- Import `expect` from `vitest` (not a standalone copy) so the test-aware instance is used.
Example fix
// before (module scope)
expect(value).toMatchSnapshot()
// after
test('snapshots value', () => {
expect(value).toMatchSnapshot()
}) Defensive patterns
Strategy: validation
Validate before calling
import { getWorkerState } from 'vitest'
function inTestContext() {
return !!getWorkerState()?.currentTask
}
if (!inTestContext()) throw new Error('snapshot must run inside a test')
expect(value).toMatchSnapshot() Prevention
- Keep expect(...).toMatchSnapshot() inside an it/test body.
- Import expect from vitest so the test-aware instance is used.
- Avoid module-level or detached-worker snapshot calls.
When it happens
Trigger: Using `expect(...).toMatchSnapshot()` outside of a running test (e.g. at module top level, in a worker, inside a plain script, in a setup hook that runs outside test context); calling snapshot assertions via a manually constructed `expect` that bypasses Vitest's test-aware wrapper.
Common situations: Calling `expect` inside module-level code or `globalThis` callbacks; using `expect` from a different package than the test runner; extracting `expect` into a helper that runs outside the test's async context; running assertion code in a detached worker.
Related errors
- cannot be used with "not
- Assertion name is not set. This is a bug in Vitest. Please…
- expected must be a function, received
- ' ' cannot be used with 'test.fails
- A function to advance timers was called but the timers APIs…
AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11).
Data as JSON: /api/errors/e4f6733187030be1.
Report an issue: GitHub.
Appendix: source
Thrown at packages/vitest/src/integrations/snapshot/chai.ts:68
return {
filepath: test.file.filepath,
name: getNames(test).slice(1).join(' > '),
testId: test.id,
}
}
function getAssertionName(assertion: Chai.Assertion): string {
const name = chai.util.flag(assertion, '_name') as string | undefined
if (!name) {
throw new Error('Assertion name is not set. This is a bug in Vitest. Please, open a new issue with reproduction.')
}
return name
}
function getTest(obj: Chai.Assertion) {
const test = chai.util.flag(obj, 'vitest-test') as Test | undefined
if (!test) {
throw new Error(`'${getAssertionName(obj)}' cannot be used without test context`)
}
if (test.fails) {
throw new TestSyntaxError(`'${getAssertionName(obj)}' cannot be used with 'test.fails'`)
}
return test
}
function validateAssertion(assertion: Chai.Assertion): void {
if (chai.util.flag(assertion, 'negate')) {
throw new Error(`${getAssertionName(assertion)} cannot be used with "not"`)
}
}
export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
for (const key of ['matchSnapshot', 'toMatchSnapshot']) {
utils.addMethod(
chai.Assertion.prototype,
key,View on GitHub (pinned to 1fa9837ec2)