vitest-dev/vitest · error · Error
cannot be used with "not
Error message
${getAssertionName(assertion)} cannot be used with "not" What it means
Thrown by `validateAssertion` when a snapshot assertion is chained with `.not` (the Chai negation flag is set). Snapshot matching is an equivalence check against a stored artifact; negating it ('this value does NOT match the stored snapshot') is ambiguous and not supported, so Vitest rejects the combination. The message names the offending assertion.
Solutions
- Drop the `.not` and update the snapshot to the expected value (or assert the specific difference with `toEqual`/`toMatchObject`).
- If you need to assert a value is NOT a specific shape, use `expect(value).not.toEqual(unwanted)` with an explicit expected rather than a snapshot.
- For inline checks, compare against an explicit literal instead of an inline snapshot.
Example fix
// before
expect(result).not.toMatchSnapshot()
// after
expect(result).toEqual({ status: 'ok' }) Defensive patterns
Strategy: validation
Validate before calling
// never chain .not before a snapshot matcher // static check: reject 'not.toMatchSnapshot' / 'not.toMatchInlineSnapshot' in review
Prevention
- Never use .not with snapshot matchers.
- For 'is not equal to X' assertions, use toEqual with an explicit expected value.
- Add an ESLint custom rule or code-review check to forbid .not.toMatchSnapshot.
When it happens
Trigger: Writing `expect(value).not.toMatchSnapshot()`, `expect(value).not.toMatchInlineSnapshot()`, or `expect(fn).not.toThrowErrorMatchingSnapshot()`.
Common situations: Reaching for `.not` out of habit when asserting a value differs from a snapshot; misunderstanding that snapshots are write-once/compare rather than boolean matches.
Related errors
- ' ' cannot be used without test context
- 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/612b79d7d2ac2c0d.
Report an issue: GitHub.
Appendix: 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 1fa9837ec2)