oxc-project/oxc · warning

`jest.mock()` factories should not be used without an explic

Error message

`jest.mock()` factories should not be used without an explicit type parameter.

What it means

This is oxlint's 'jest/no-untyped-mock-factory' diagnostic, a TypeScript-only rule. It fires when jest.mock() is called with a module-name string literal plus a factory function but no explicit type parameter. Without a type parameter, the factory's return type is unchecked against the real module, so a typo in a mocked method silently produces wrong mocks; the help text recommends jest.mock<typeof import('./module')>(...).

Source

Thrown at crates/oxc_linter/src/rules/jest/no_untyped_mock_factory.rs:12

use oxc_ast::{
    AstKind,
    ast::{Argument, Expression},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;

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

fn add_type_parameter_to_module_mock_diagnostic(module_name: &str, span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn(
        "`jest.mock()` factories should not be used without an explicit type parameter.",
    )
    .with_help(format!(
        "Add a type parameter to the mock factory such as `typeof import({module_name:?})`"
    ))
    .with_label(span)
}

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

declare_oxc_lint!(
    /// ### What it does
    ///
    /// This rule triggers a warning if `mock()` or `doMock()` is used without a generic
    /// type parameter or return type.
    ///
    /// ### Why is this bad?

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Add the type parameter: jest.mock<typeof import('./repo')>('./repo', () => ({ find: jest.fn() })) and make the factory return a compatible partial.
  2. Use jest.requireMock or jest.mocked on the imported module if the type parameter form is awkward in your setup.
  3. If the file is plain JavaScript, exclude it from TS-only rules via config overrides.
  4. Suppress intentional untyped mocks with // oxlint-disable-next-line jest/no-untyped-mock-factory.

Example fix

// before
jest.mock('./repo', () => ({
  find: jest.fn(),
}));

// after
jest.mock<typeof import('./repo')>('./repo', () => ({
  find: jest.fn(),
}));
Defensive patterns

Strategy: type-guard

Validate before calling

// list untyped jest.mock factories in TS files before enabling the rule
const { execSync } = require('node:child_process');
console.log(execSync("rg -n "jest\\.mock\\(\\s*['\"][^'\"]+['\"]\\s*,\\s*\\(\\)" --glob '*.ts' --glob '*.tsx' tests/", { encoding: 'utf8' }));

Type guard

// narrow before use: ensure the factory result matches the real module shape
type Repo = typeof import('./repo');
function isRepoMock(m: unknown): m is jest.Mocked<Repo> {
  return typeof m === 'object' && m !== null && 'find' in m;
}
if (!isRepoMock(mocked)) throw new Error('jest.mock factory does not match Repo');

Prevention

When it happens

Trigger: Enable the rule in a TypeScript context and lint jest.mock('./repo', () => ({ find: jest.fn() })) — a call node whose callee resolves to jest.mock, whose first argument is a string literal, whose factory is present, and whose type_arguments are None. The diagnostic includes the module name in the help.

Common situations: Teams writing typed Jest mocks for the first time copy the JS form of jest.mock into TS files. During migration from JavaScript to TypeScript, existing mock factories keep working but lose type safety, and this rule surfaces each one. Mocking only part of a module (partial mocks) is the most common real-world case.

Related errors


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