{"record":{"id":"81950560ff823ab9","repo":"oxc-project/oxc","slug":"snapshot-hint-must-be-a-string-literal","errorCode":null,"errorMessage":"Snapshot hint must be a string literal.","messagePattern":"Snapshot hint must be a string literal\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/shared/jest_vitest/prefer_snapshot_hint.rs","lineNumber":34,"sourceCode":"        JestFnKind, JestGeneralFnKind, PossibleJestNode, collect_possible_jest_call_node,\n        parse_expect_jest_fn_call,\n    },\n};\n\nfn snapshot_matcher_too_many_arguments_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"`toMatchSnapshot` takes at most two arguments.\")\n        .with_help(\"Pass a hint string, or a property matcher object followed by a hint string.\")\n        .with_label(span)\n}\n\nfn snapshot_missing_hint_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Snapshot is missing a hint.\")\n        .with_help(\"Include a hint string to identify this snapshot in the snapshot file.\")\n        .with_label(span)\n}\n\nfn snapshot_hint_must_be_string_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Snapshot hint must be a string literal.\")\n        .with_help(\n            \"Provide a string literal as the hint, or pass a property matcher object as the first argument and the hint string as the second.\",\n        )\n        .with_label(span)\n}\n\n#[derive(Debug, Clone, Copy, Eq, PartialEq, Deserialize, JsonSchema, Default)]\n#[serde(rename_all = \"lowercase\")]\npub enum SnapshotHintMode {\n    /// Require a hint to always be provided when using external snapshot matchers.\n    Always,\n    /// Require a hint to be provided when there are multiple external snapshot matchers within the scope (meaning it includes nested calls).\n    #[default]\n    Multi,\n}\n\npub const DOCUMENTATION: &str = r\"### What it does\n","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/shared/jest_vitest/prefer_snapshot_hint.rs#L16-L52","documentation":"This is the oxlint `prefer-snapshot-hint` rule, hint-type branch. The hint argument exists so humans can identify a snapshot in the snapshot file, which only works if it is a string literal; Jest stringifies whatever it receives, so a template expression or identifier passes through opaquely and defeats the purpose. When the single argument to an external snapshot matcher is not a string literal, this diagnostic asks for a literal hint (or property matcher + literal hint).","triggerScenarios":"A `toMatchSnapshot`/`toThrowErrorMatchingSnapshot` call in a hint-requiring scope whose first argument is not a string literal — e.g. `expect(x).toMatchSnapshot(42)`, `toMatchSnapshot(someVar)`, or a single non-literal argument where a hint was expected.","commonSituations":"Passing a property-matcher object but forgetting the second hint argument; hoisting hint text into a constant; template literals that are not literals in the AST sense.","solutions":["Pass the hint as a string literal: `toMatchSnapshot('login form')`.","When using property matchers, put the object first and the literal hint second: `toMatchSnapshot({ id: expect.any(Number) }, 'user row')`.","Avoid dynamic/hoisted hint expressions — the whole point is a stable, greppable label."],"exampleFix":"// before\nexpect(result).toMatchSnapshot(hintFromConfig);\n\n// after\nexpect(result).toMatchSnapshot('migration result');","handlingStrategy":"validation","validationCode":"// .oxlintrc.json\n{ \"rules\": { \"jest/prefer-snapshot-hint\": \"error\" } }\n\nnpx oxlint tests/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Hints must be inline string literals — constants and template expressions defeat snapshot lookup.","When passing property matchers, always follow with the hint as the second argument."],"tags":["jest","vitest","oxlint","testing","snapshots","api-misuse","static-analysis"],"backgroundTag":"snapshot-hint","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}