oxc-project/oxc · warning · OxcDiagnostic

Jest tests should not return a value

Error message

Jest tests should not return a value

What it means

This is the oxlint `no-test-return-statement` rule (jest/vitest plugin). Jest ignores any value a test callback returns, so an explicit `return` inside a `test`/`it` callback is at best noise and at worst a misunderstanding: developers often `return` a promise thinking it is awaited, but the recommended pattern is `async`/`await`. The rule reports `ReturnStatement` nodes found inside jest test function callbacks.

Source

Thrown at crates/oxc_linter/src/rules/shared/jest_vitest/no_test_return_statement.rs:15

use oxc_ast::{
    AstKind,
    ast::{CallExpression, Expression, ReturnStatement},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_semantic::{AstNode, NodeId};
use oxc_span::{GetSpan, Span};

use crate::{
    context::LintContext,
    utils::{JestFnKind, JestGeneralFnKind, PossibleJestNode, is_type_of_jest_fn_call},
};

fn no_test_return_statement_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Jest tests should not return a value")
        .with_help("Use `await` for async assertions or remove the return statement.")
        .with_note("Jest ignores returned values from tests.")
        .with_label(span)
}

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

Disallow explicitly returning from tests.

### Why is this bad?

Tests in Jest should be void and not return values.
If you are returning Promises then you should update the test to use
`async/await`.

### Examples

Examples of **incorrect** code for this rule:

View on GitHub (pinned to e1e7af627c)

Solutions

  1. For returned promises, mark the test callback `async` and `await` the call instead of returning it.
  2. Delete the `return` keyword and keep the expression as a plain statement (`expect(x).toBe(1);`).
  3. If the value is genuinely needed by the caller, move that logic out of the test into a helper.

Example fix

// before
test('loads data', () => {
  return fetchData().then((d) => {
    expect(d).toBe('ok');
  });
});

// after
test('loads data', async () => {
  const d = await fetchData();
  expect(d).toBe('ok');
});
Defensive patterns

Strategy: validation

Validate before calling

// .oxlintrc.json
{ "rules": { "jest/no-test-return-statement": "error" } }

npx oxlint tests/

Prevention

When it happens

Trigger: Any `return` statement in the callback passed to `test()`, `it()`, or their `.each`/`.skip`/`.only` variants — e.g. `test('x', () => { return fetchResult(); })` or `it('y', function () { return expect(x).toBe(1); });`.

Common situations: Copy-pasting a helper function body into a test without removing the return; old-style promise tests written before `async/await` that `return promise` to make Jest wait; returning the result of `expect()` as a stylistic habit carried over from other frameworks.

Related errors


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