{"record":{"id":"1b68e46a51217715","repo":"oxc-project/oxc","slug":"mock-functions-that-return-simple-values-should-us","errorCode":null,"errorMessage":"Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`.","messagePattern":"Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/shared/jest_vitest/prefer_mock_return_shorthand.rs","lineNumber":25,"sourceCode":"        VariableDeclarationKind,\n    },\n};\nuse oxc_ast_visit::{VisitJs, walk_js};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_semantic::{ReferenceId, ScopeFlags, SymbolId};\nuse oxc_span::{GetSpan, Span};\nuse rustc_hash::FxHashSet;\n\nuse crate::{AstNode, context::LintContext};\n\nfn prefer_mock_return_shorthand_diagnostic(\n    span: Span,\n    current_property: &str,\n    replacement: &str,\n) -> OxcDiagnostic {\n    let help = format!(\"Replace `{current_property}` with `{replacement}`.\");\n\n    OxcDiagnostic::warn(\n        \"Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`.\",\n    )\n    .with_help(help)\n    .with_label(span)\n}\n\npub const DOCUMENTATION: &str = r\"### What it does\n\nWhen working with mocks of functions that return simple values, Jest provides some API sugar functions to reduce the amount of boilerplate you have to write.\n\n### Why is this bad?\n\nNot using Jest's API sugar functions adds unnecessary boilerplate and makes tests harder to read. These helpers clearly express intent\nand reduce errors, keeping tests simple and maintainable.\n\n### Examples\n\nExamples of **incorrect** code for this rule:","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/shared/jest_vitest/prefer_mock_return_shorthand.rs#L7-L43","documentation":"This is the oxlint `prefer-mock-return-shorthand` rule (jest/vitest plugin). When a mock only needs to return a simple value, `mockImplementation(() => value)` is boilerplate; Jest/Vitest provide `mockReturnValue(value)` (and the `...Once` variants) for exactly this. The diagnostic names the current property and the replacement so the fix is mechanical.","triggerScenarios":"A call whose parsed mock property is `mockImplementation`/`mockImplementationOnce` where the implementation is a zero-parameter function whose body is a single return of a simple (non-promise) value — e.g. `jest.fn().mockImplementation(() => 42)`.","commonSituations":"Mocking config getters and small util functions; refactoring handwritten stubs; developers unaware of the return-value sugar APIs.","solutions":["Replace with the shorthand: `mockReturnValue(42)` / `mockReturnValueOnce(42)`.","Keep mockImplementation only when the value must be computed per call or the function takes parameters.","If the returned value is a promise, use `mockResolvedValue(v)` instead — a different shorthand that this rule complements."],"exampleFix":"// before\njest.fn().mockImplementation(() => 'ok');\n\n// after\njest.fn().mockReturnValue('ok');","handlingStrategy":"validation","validationCode":"// .oxlintrc.json\n{ \"rules\": { \"jest/prefer-mock-return-shorthand\": \"error\" } }\n\nnpx oxlint tests/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use the ladder: mockReturnValue < mockResolvedValue < mockImplementation — pick the least powerful that works.","Reserve mockImplementation for behavior that depends on call arguments or needs branching."],"tags":["jest","vitest","oxlint","testing","mocks","style","static-analysis"],"backgroundTag":"mock-return-shorthand","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}