{"record":{"id":"6200820fd53b1165","repo":"vitest-dev/vitest","slug":"getassertionname-obj-cannot-be-used-with-te","errorCode":null,"errorMessage":"'${getAssertionName(obj)}' cannot be used with 'test.fails'","messagePattern":"'(.+?)' cannot be used with 'test\\.fails'","errorType":"exception","errorClass":"TestSyntaxError","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/integrations/snapshot/chai.ts","lineNumber":71,"sourceCode":"    testId: test.id,\n  }\n}\n\nfunction getAssertionName(assertion: Chai.Assertion): string {\n  const name = chai.util.flag(assertion, '_name') as string | undefined\n  if (!name) {\n    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,","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/integrations/snapshot/chai.ts#L53-L89","documentation":"Thrown by `getTest` as a `TestSyntaxError` when the active test was declared with `test.fails(...)` (or `it.fails`) and a snapshot assertion is used. `test.fails` inverts pass/fail semantics, which is incompatible with snapshot matching (a 'failing' snapshot would corrupt the stored snapshot), so Vitest disallows the combination. The message names the offending assertion.","triggerScenarios":"Writing `test.fails('x', () => { expect(value).toMatchSnapshot() })` or `it.fails(...)` with any snapshot assertion inside; nesting a snapshot assertion inside a test marked `.fails`.","commonSituations":"Using `test.fails` as a quick way to assert that something is 'wrong' and adding a snapshot in the same body; converting an existing failing test to `test.fails` without removing snapshot assertions.","solutions":["Remove the snapshot assertion from the `test.fails` body — use a non-snapshot expectation for the negative case.","If you want to snapshot, drop the `.fails` modifier and assert the expected failure explicitly (e.g. `expect(...).toThrow()`).","Split into two tests: one `.fails` for the error case and a normal test for the snapshot."],"exampleFix":"// before\ntest.fails('renders', () => {\n  expect(render()).toMatchSnapshot()\n})\n\n// after\ntest('renders', () => {\n  expect(render()).toMatchSnapshot()\n})","handlingStrategy":"validation","validationCode":"// do not combine test.fails with snapshot assertions\nif (testContext.fails) throw new Error('move snapshot out of test.fails')","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never place snapshot assertions inside test.fails/it.fails bodies.","Use explicit toThrow assertions for negative cases instead of test.fails + snapshot.","Split negative and snapshot checks into separate tests."],"tags":["snapshot","test-fails","syntax-error","chai"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}