{"record":{"id":"c8e3b698ed62396e","repo":"oxc-project/oxc","slug":"mocks-should-not-be-manually-imported-from-a-mo","errorCode":null,"errorMessage":"Mocks should not be manually imported from a `__mocks__` directory.","messagePattern":"Mocks should not be manually imported from a `__mocks__` directory\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/shared/jest_vitest/no_mocks_import.rs","lineNumber":10,"sourceCode":"use std::path::PathBuf;\n\nuse oxc_ast::{AstKind, ast::Argument};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_span::Span;\n\nuse crate::context::LintContext;\n\nfn no_mocks_import_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Mocks should not be manually imported from a `__mocks__` directory.\")\n        .with_help(\"Instead use `jest.mock` or `vi.mock` and import from the original module path.\")\n        .with_label(span)\n}\n\npub const DOCUMENTATION: &str = r\"### What it does\n\nThis rule reports imports from a path containing a `__mocks__` component.\n\n### Why is this bad?\n\nManually importing mocks from a `__mocks__` directory can lead to unexpected behavior\nand breaks Jest's automatic mocking system. Jest is designed to automatically resolve\nand use mocks from `__mocks__` directories when `jest.mock()` is called. Directly\nimporting from these directories bypasses Jest's module resolution system and can cause\ninconsistencies between test and production environments.\n\n### Examples\n","sourceCodeStart":1,"sourceCodeEnd":28,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/shared/jest_vitest/no_mocks_import.rs#L1-L28","documentation":"Diagnostic from oxlint rule `jest/no-mocks-import` (shared with vitest) in crates/oxc_linter/src/rules/shared/jest_vitest/no_mocks_import.rs:11. Files under a `__mocks__` directory are consumed automatically by Jest's module resolution adjacent to the real module; importing them by hand both bypasses and confuses the automatic mocking system. The rule reports any import whose specifier path contains a `__mocks__` path component.","triggerScenarios":"An import (or require/dynamic import argument) whose path string contains a `__mocks__` segment, e.g. import foo from '../__mocks__/fs' or jest.requireActual style references with __mocks__ in the path. The rule inspects call/import arguments and matches the path (PathBuf component check) before labeling the span.","commonSituations":"Manually pulling in a manual mock that Jest already wires automatically; porting tests from a framework without automatic mocks; sibling-adjacent __mocks__ directories (e.g. for node modules like fs) being imported directly by newcomers.","solutions":["Delete the manual import and rely on automatic mocking: for node modules adjacent __mocks__/fs.js is used automatically; for local modules add jest.mock('../fs') at the top","Import from the original module path in the test after registering the mock: jest.mock('../fs'); import fs from '../fs';","If you need the real implementation in the same file, use jest.requireActual('../fs') (or vi.importActual) rather than reaching into __mocks__"],"exampleFix":"// before\nimport fs from '../__mocks__/fs';\n\n// after\njest.mock('fs'); // or: jest.mock('../fs') for a local sibling mock\nimport fs from 'fs';","handlingStrategy":"validation","validationCode":"rg -n \"['\\\"/]__mocks__/\" src/ tests/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Rely on automatic __mocks__ resolution; never import from that directory","Register local manual mocks with jest.mock('./module') at the top of the test","Use jest.requireActual / vi.importActual when the real module is needed"],"tags":["jest","vitest","oxlint","testing","mocks","imports"],"backgroundTag":"manual-mock-import","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}