{"id":"612b79d7d2ac2c0d","repo":"vitest-dev/vitest","slug":"getassertionname-assertion-cannot-be-used-with","errorCode":null,"errorMessage":"${getAssertionName(assertion)} cannot be used with \"not\"","messagePattern":"(.+?) cannot be used with \"not\"","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/integrations/snapshot/chai.ts","lineNumber":78,"sourceCode":"    throw new Error('Assertion name is not set. This is a bug in Vitest. Please, open a new issue with reproduction.')\n  }\n  return name\n}\n\nfunction getTest(obj: Chai.Assertion) {\n  const test = chai.util.flag(obj, 'vitest-test') as Test | undefined\n  if (!test) {\n    throw new Error(`'${getAssertionName(obj)}' cannot be used without test context`)\n  }\n  if (test.fails) {\n    throw new TestSyntaxError(`'${getAssertionName(obj)}' cannot be used with 'test.fails'`)\n  }\n  return test\n}\n\nfunction validateAssertion(assertion: Chai.Assertion): void {\n  if (chai.util.flag(assertion, 'negate')) {\n    throw new Error(`${getAssertionName(assertion)} cannot be used with \"not\"`)\n  }\n}\n\nexport const SnapshotPlugin: ChaiPlugin = (chai, utils) => {\n  for (const key of ['matchSnapshot', 'toMatchSnapshot']) {\n    utils.addMethod(\n      chai.Assertion.prototype,\n      key,\n      wrapAssertion(utils, key, function (\n        this,\n        propertiesOrHint?: object | string,\n        hint?: string,\n      ) {\n        const result = toMatchSnapshotImpl({\n          assertion: this,\n          received: utils.flag(this, 'object'),\n          ...normalizeArguments(propertiesOrHint, hint),\n        })","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/integrations/snapshot/chai.ts#L60-L96","documentation":"`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.","triggerScenarios":"Writing `expect(value).not.toMatchSnapshot()`, `expect(value).not.toMatchInlineSnapshot()`, `.not.toMatchFileSnapshot()`, `.not.toThrowErrorMatchingSnapshot()`, or `.not.toThrowErrorMatchingInlineSnapshot()`.","commonSituations":"Copy-pasting an existing assertion and adding `.not`, or attempting to assert that a value has diverged from a stored snapshot.","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."],"exampleFix":"// before\nexpect(result).not.toMatchSnapshot()\n// after\nexpect(result).toMatchSnapshot()","handlingStrategy":"validation","validationCode":"// lint/grep your tests for `.not.toMatch` and `.not.toThrowErrorMatching` and remove `.not`","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never prepend `.not` to a snapshot matcher.","For divergence checks compare values explicitly instead of negating a snapshot."],"tags":["snapshot","negation","syntax","matcher"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}