{"record":{"id":"0262fc2d808ada0b","repo":"oxc-project/oxc","slug":"prefer-mock-resolved-rejected-shorthands-for-promi","errorCode":null,"errorMessage":"Prefer mock resolved/rejected shorthands for promises.","messagePattern":"Prefer mock resolved/rejected shorthands for promises\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/shared/jest_vitest/prefer_mock_promise_shorthand.rs","lineNumber":12,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{Argument, CallExpression, Expression, Statement},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_span::Span;\nuse oxc_str::Str;\n\nuse crate::{context::LintContext, fixer::RuleFixer, utils::get_node_name};\n\nfn use_mock_shorthand(preferred_name: &str, span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Prefer mock resolved/rejected shorthands for promises.\")\n        .with_help(format!(\"Prefer {preferred_name:?}\"))\n        .with_label(span)\n}\n\npub const DOCUMENTATION: &str = r\"### What it does\n\nWhen working with mocks of functions that return promises, Jest provides some\nAPI sugar functions to reduce the amount of boilerplate you have to write.\nThese methods should be preferred when possible.\n\n### Why is this bad?\n\nUsing generic mock functions like `mockImplementation(() => Promise.resolve())`\nor `mockReturnValue(Promise.reject())` is more verbose and less readable than\nJest's specialized promise shorthands. The shorthand methods like\n`mockResolvedValue()` and `mockRejectedValue()` are more expressive and\nmake the test intent clearer.\n","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/shared/jest_vitest/prefer_mock_promise_shorthand.rs#L1-L30","documentation":"Diagnostic from oxlint's shared jest/vitest prefer-mock-promise-shorthand rule. Jest's mock API has dedicated sugar for promises; the rule fires on `mockReturnValue`/`mockReturnValueOnce`/`mockImplementation`/`mockImplementationOnce` calls whose supplied value or returned expression is literally `Promise.resolve(...)` or `Promise.reject(...)`, and suggests the `mockResolvedValue`/`mockRejectedValue` (+Once) shorthands. Autofix is offered when the Promise call has at most one argument.","triggerScenarios":"Per run()/report(): callee property is one of the four mock setters; for mockReturnValue the first argument expression is a `Promise.resolve`/`Promise.reject` call; for mockImplementation the argument must be a zero-parameter arrow (expression form or single return statement) or a function expression whose single statement returns that Promise call — e.g. `jest.fn().mockImplementation(() => Promise.resolve(42))`, `.mockReturnValue(Promise.reject(err))`, `.mockReturnValueOnce(Promise.resolve(x))`. Implementations with parameters or non-Promise returns do not fire.","commonSituations":"Mocking async service calls in unit tests; older codebases written before the shorthand API (added in Jest 23) became common; churned mocks where Promise.resolve wrapping survived multiple refactors.","solutions":["Replace with the shorthand: `mockImplementation(() => Promise.resolve(v))` → `mockResolvedValue(v)`; `mockReturnValue(Promise.reject(e))` → `mockRejectedValue(e)`; keep the `Once` suffix when present.","Run `oxlint --fix` to convert all single-argument cases automatically.","For multi-argument Promise.resolve/reject (e.g. thenables with executor args) rewrite manually."],"exampleFix":"// before\njest.fn().mockImplementation(() => Promise.resolve(42));\njest.spyOn(fs, 'read').mockReturnValue(Promise.reject(new Error('io')));\n\n// after\njest.fn().mockResolvedValue(42);\njest.spyOn(fs, 'read').mockRejectedValue(new Error('io'));","handlingStrategy":"validation","validationCode":"oxlint --jest-plugin --fix test/ # converts single-arg Promise.resolve/reject mocks","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Type mockResolvedValue/mockRejectedValue directly instead of wrapping promises.","Remember the Once variants (mockResolvedValueOnce) map one-to-one.","When implementations need parameters or multi-step logic, the rule stays quiet — that is your cue the mock is non-trivial."],"tags":["jest","vitest","mocking","promises","style","oxlint","lint"],"backgroundTag":"mock-promise-shorthand","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"}