{"record":{"id":"6b7ba242e732d817","repo":"oxc-project/oxc","slug":"jest-mock-factories-should-not-be-used-without","errorCode":null,"errorMessage":"`jest.mock()` factories should not be used without an explicit type parameter.","messagePattern":"`jest\\.mock\\(\\)` factories should not be used without an explicit type parameter\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/jest/no_untyped_mock_factory.rs","lineNumber":12,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{Argument, Expression},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\n\nuse crate::{context::LintContext, rule::Rule, utils::PossibleJestNode};\n\nfn add_type_parameter_to_module_mock_diagnostic(module_name: &str, span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\n        \"`jest.mock()` factories should not be used without an explicit type parameter.\",\n    )\n    .with_help(format!(\n        \"Add a type parameter to the mock factory such as `typeof import({module_name:?})`\"\n    ))\n    .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct NoUntypedMockFactory;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// This rule triggers a warning if `mock()` or `doMock()` is used without a generic\n    /// type parameter or return type.\n    ///\n    /// ### Why is this bad?","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/jest/no_untyped_mock_factory.rs#L1-L30","documentation":"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')>(...).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Add the type parameter: jest.mock<typeof import('./repo')>('./repo', () => ({ find: jest.fn() })) and make the factory return a compatible partial.","Use jest.requireMock or jest.mocked on the imported module if the type parameter form is awkward in your setup.","If the file is plain JavaScript, exclude it from TS-only rules via config overrides.","Suppress intentional untyped mocks with // oxlint-disable-next-line jest/no-untyped-mock-factory."],"exampleFix":"// before\njest.mock('./repo', () => ({\n  find: jest.fn(),\n}));\n\n// after\njest.mock<typeof import('./repo')>('./repo', () => ({\n  find: jest.fn(),\n}));","handlingStrategy":"type-guard","validationCode":"// list untyped jest.mock factories in TS files before enabling the rule\nconst { execSync } = require('node:child_process');\nconsole.log(execSync(\"rg -n \"jest\\\\.mock\\\\(\\\\s*['\\\"][^'\\\"]+['\\\"]\\\\s*,\\\\s*\\\\(\\\\)\" --glob '*.ts' --glob '*.tsx' tests/\", { encoding: 'utf8' }));","typeGuard":"// narrow before use: ensure the factory result matches the real module shape\ntype Repo = typeof import('./repo');\nfunction isRepoMock(m: unknown): m is jest.Mocked<Repo> {\n  return typeof m === 'object' && m !== null && 'find' in m;\n}\nif (!isRepoMock(mocked)) throw new Error('jest.mock factory does not match Repo');","tryCatchPattern":null,"preventionTips":["Standardize on jest.mock<typeof import('./mod')>(...) in all TS mock factories.","Use jest.mocked(importedFn) at usage sites to keep types flowing from the real module.","Add the type parameter at the moment you write the factory, not as a follow-up fix."],"tags":["lint","jest","typescript","mocking","testing","oxlint"],"backgroundTag":"jest-untyped-mock-factory","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}