oxc-project/oxc · warning

Prefer mock resolved/rejected shorthands for promises.

Error message

Prefer mock resolved/rejected shorthands for promises.

What it means

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.

Source

Thrown at crates/oxc_linter/src/rules/shared/jest_vitest/prefer_mock_promise_shorthand.rs:12

use oxc_ast::{
    AstKind,
    ast::{Argument, CallExpression, Expression, Statement},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_span::Span;
use oxc_str::Str;

use crate::{context::LintContext, fixer::RuleFixer, utils::get_node_name};

fn use_mock_shorthand(preferred_name: &str, span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Prefer mock resolved/rejected shorthands for promises.")
        .with_help(format!("Prefer {preferred_name:?}"))
        .with_label(span)
}

pub const DOCUMENTATION: &str = r"### What it does

When working with mocks of functions that return promises, Jest provides some
API sugar functions to reduce the amount of boilerplate you have to write.
These methods should be preferred when possible.

### Why is this bad?

Using generic mock functions like `mockImplementation(() => Promise.resolve())`
or `mockReturnValue(Promise.reject())` is more verbose and less readable than
Jest's specialized promise shorthands. The shorthand methods like
`mockResolvedValue()` and `mockRejectedValue()` are more expressive and
make the test intent clearer.

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Replace with the shorthand: `mockImplementation(() => Promise.resolve(v))` → `mockResolvedValue(v)`; `mockReturnValue(Promise.reject(e))` → `mockRejectedValue(e)`; keep the `Once` suffix when present.
  2. Run `oxlint --fix` to convert all single-argument cases automatically.
  3. For multi-argument Promise.resolve/reject (e.g. thenables with executor args) rewrite manually.

Example fix

// before
jest.fn().mockImplementation(() => Promise.resolve(42));
jest.spyOn(fs, 'read').mockReturnValue(Promise.reject(new Error('io')));

// after
jest.fn().mockResolvedValue(42);
jest.spyOn(fs, 'read').mockRejectedValue(new Error('io'));
Defensive patterns

Strategy: validation

Validate before calling

oxlint --jest-plugin --fix test/ # converts single-arg Promise.resolve/reject mocks

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/0262fc2d808ada0b. Report an issue: GitHub.