oxc-project/oxc · error · OxcDiagnostic
Mocked modules must be dynamic imported.
Error message
Mocked modules must be dynamic imported.
What it means
Emitted by the vitest/prefer-import-in-mock rule at a `vi.mock()` call whose module path is given as a plain string instead of a dynamic `import()` expression. It fires because `vi.mock(import('./path'), ...)` gives vitest better type information and IDE IntelliSense than the string form.
Source
Thrown at crates/oxc_linter/src/rules/vitest/prefer_import_in_mock.rs:19
use oxc_ast::{AstKind, ast::Argument};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use schemars::JsonSchema;
use serde::Deserialize;
use crate::{
context::LintContext,
rule::{DefaultRuleConfig, Rule},
utils::{PossibleJestNode, parse_general_jest_fn_call},
};
fn prefer_import_in_mock_diagnostic(span: Span, path: &str) -> OxcDiagnostic {
let help = format!(
"Dynamic import improves the type information and IntelliSense. Substitute `{path}` with `import('{path}')`"
);
OxcDiagnostic::warn("Mocked modules must be dynamic imported.").with_help(help).with_label(span)
}
#[derive(Debug, Default, Clone)]
pub struct PreferImportInMock(Box<PreferImportInMockConfig>);
impl std::ops::Deref for PreferImportInMock {
type Target = PreferImportInMockConfig;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct PreferImportInMockConfig {
/// Whether the rule should generate fixes or not.
fixable: bool,View on GitHub (pinned to e1e7af627c)
Solutions
- Replace `vi.mock('./path', ...)` with `vi.mock(import('./path'), ...)`
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at crates/oxc_linter/src/rules/vitest/prefer_import_in_mock.rs:19 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/514ead1d0a012d15.
Report an issue: GitHub.