oxc-project/oxc · warning
`jest.setTimeout` should only be called in a global scope
Error message
`jest.setTimeout` should only be called in a global scope
What it means
This is oxlint's 'jest/no-confusing-set-timeout' diagnostic for scope violations. It fires when jest.setTimeout(...) is called anywhere other than the top level of the test file (inside describe, test, beforeAll, hooks, or callbacks). Only the module scope is global; a setTimeout buried in a block looks like it scopes to that block but actually affects the whole file, which misleads readers.
Source
Thrown at crates/oxc_linter/src/rules/jest/no_confusing_set_timeout.rs:15
use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::{AstNode, NodeId, ReferenceId};
use oxc_span::Span;
use rustc_hash::{FxHashMap, FxHashSet};
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!(View on GitHub (pinned to e1e7af627c)
Solutions
- Move the jest.setTimeout(ms) call to the top level of the test file, before any tests.
- If only one test needs more time, pass the timeout as the third/fifth argument to it/test instead: test('name', fn, 10000).
- If the whole suite needs it, set testTimeout in jest.config.js and remove the per-file call entirely.
- Suppress a deliberate case with // oxlint-disable-next-line jest/no-confusing-set-timeout.
Example fix
// before
describe('integration', () => {
jest.setTimeout(30000);
it('is slow', () => { /* ... */ });
});
// after
jest.setTimeout(30000);
describe('integration', () => {
it('is slow', () => { /* ... */ });
}); Defensive patterns
Strategy: validation
Validate before calling
// flag jest.setTimeout calls that are indented (inside blocks)
const { execSync } = require('node:child_process');
console.log(execSync("rg -n '^\\s+jest\\.setTimeout' tests/", { encoding: 'utf8' })); Prevention
- Keep jest.setTimeout only at column 0 of test files.
- Prefer per-test timeout arguments (test('name', fn, 10000)) for localized slowness.
- Centralize timeout configuration in jest.config.js testTimeout instead of per-file calls.
When it happens
Trigger: Enable the rule and lint a file where a parsed jest.setTimeout() call node's enclosing function scope is not the program (module) scope — e.g. describe(() => { jest.setTimeout(5000); }). The diagnostic is emitted on the call span.
Common situations: Developers try to give one slow describe block a longer timeout by calling jest.setTimeout inside it, not realizing the call is file-global and order-dependent. It also appears when moving setup code into beforeAll hooks or shared setup files.
Understand the failure class
- Timeouts: ETIMEDOUT, deadlines, and hung requests — what actually expires when a request times out.
Related errors
- Do not call `jest.setTimeout` multiple times
- `jest.setTimeout` should be placed before any other jest met
- Require a message for {matcher_name:?}.
- {deprecated:?} has been deprecated in favor of {new:?}
- Function parameter(s) use the `done` argument
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/bc70040098308361.
Report an issue: GitHub.