oxc-project/oxc · warning

`jest.setTimeout` should be placed before any other jest met

Error message

`jest.setTimeout` should be placed before any other jest methods.

What it means

This is the ordering branch of oxlint's 'jest/no-confusing-set-timeout' rule. It reports a jest.setTimeout call that appears after other jest method calls in the module. Placement matters because Jest reads timeout configuration while evaluating the module, so a late setTimeout may run after some tests or mocks were already registered, making its effect unpredictable.

Source

Thrown at crates/oxc_linter/src/rules/jest/no_confusing_set_timeout.rs:26

use crate::{
    context::LintContext,
    rule::Rule,
    utils::{PossibleJestNode, collect_possible_jest_call_node, parse_jest_fn_call},
};

fn non_global_set_timeout_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("`jest.setTimeout` should only be called in a global scope")
        .with_label(span)
}

fn no_multiple_set_timeouts_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Do not call `jest.setTimeout` multiple times")
        .with_help("Only the last call to `jest.setTimeout` will have an effect.")
        .with_label(span)
}

fn no_unorder_set_timeout_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("`jest.setTimeout` should be placed before any other jest methods.")
        .with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct NoConfusingSetTimeout;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallow confusing usages of `jest.setTimeout`.
    ///
    /// ### Why is this bad?
    ///
    /// - being called anywhere other than in global scope
    /// - being called multiple times
    /// - being called after other Jest functions like hooks, `describe`, `test`, or `it`
    ///
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Move jest.setTimeout(ms) to the very top of the file, above all jest.mock/jest.spyOn/other jest calls.
  2. Better: remove it entirely and set testTimeout in jest.config.js so ordering never matters.
  3. If the call must stay where it is for a deliberate reason, add // oxlint-disable-next-line jest/no-confusing-set-timeout.

Example fix

// before
jest.mock('./db');
jest.setTimeout(30000);

// after
jest.setTimeout(30000);
jest.mock('./db');
Defensive patterns

Strategy: validation

Validate before calling

// flag files where jest.setTimeout appears after another jest.* call line
const { execSync } = require('node:child_process');
console.log(execSync("rg -n 'jest\\.' tests/ | awk -F: '$0 !~ /setTimeout/ {seen[FILENAME]=1} seen[FILENAME] && /setTimeout/'", { encoding: 'utf8' }));

Prevention

When it happens

Trigger: Enable the rule and lint a file where a jest.setTimeout call node's NodeId is greater than the NodeId of a previously seen different jest.* method call (tracked in an FxHashMap during the run). The diagnostic 'should be placed before any other jest methods' is emitted on the late call.

Common situations: Developers append jest.setTimeout at the bottom of a growing setup section, after jest.mock(...) or jest.useFakeTimers() lines. Merging setup files commonly lands the timeout call mid-file. Reviewers rarely notice, but the rule's ordering check does.

Understand the failure class

Related errors


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