vitest-dev/vitest · error · Error
${getAssertionName(assertion)} cannot be used with "not"
Error message
${getAssertionName(assertion)} cannot be used with "not" What it means
`validateAssertion` runs before every snapshot matcher and rejects usage with chai's negation flag (`.not`). Snapshot negation (`expect(x).not.toMatchSnapshot()`) has no meaningful semantics — there is no 'does not match this snapshot' update path — so Vitest throws `<matcher> cannot be used with "not"` to prevent a confusing always-failing assertion.
Source
Thrown at packages/vitest/src/integrations/snapshot/chai.ts:78
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,
wrapAssertion(utils, key, function (
this,
propertiesOrHint?: object | string,
hint?: string,
) {
const result = toMatchSnapshotImpl({
assertion: this,
received: utils.flag(this, 'object'),
...normalizeArguments(propertiesOrHint, hint),
})View on GitHub (pinned to d568f8ce37)
Solutions
- Drop `.not` from the snapshot matcher.
- To assert divergence from a stored snapshot, compare the value explicitly (e.g. `expect(value).not.toEqual(storedValue)`) and delete the stale snapshot with `-u` if needed.
- Use `toMatchFileSnapshot` with intentional content if you want to lock a specific non-equality.
Example fix
// before expect(result).not.toMatchSnapshot() // after expect(result).toMatchSnapshot()
Defensive patterns
Strategy: validation
Validate before calling
// lint/grep your tests for `.not.toMatch` and `.not.toThrowErrorMatching` and remove `.not`
Prevention
- Never prepend `.not` to a snapshot matcher.
- For divergence checks compare values explicitly instead of negating a snapshot.
When it happens
Trigger: Writing `expect(value).not.toMatchSnapshot()`, `expect(value).not.toMatchInlineSnapshot()`, `.not.toMatchFileSnapshot()`, `.not.toThrowErrorMatchingSnapshot()`, or `.not.toThrowErrorMatchingInlineSnapshot()`.
Common situations: Copy-pasting an existing assertion and adding `.not`, or attempting to assert that a value has diverged from a stored snapshot.
Related errors
- expect.poll() is not supported in combination with .${key}()
- '${getAssertionName(obj)}' cannot be used with 'test.fails'
- expected must be a function, received ${typeof expected}
- snapshot function didn't throw
- '${getAssertionName(obj)}' cannot be used without test conte
AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03).
Data as JSON: /data/errors/612b79d7d2ac2c0d.json.
Report an issue: GitHub.