diff --git a/crates/oxc_linter/src/rules/jest/no_mocks_import.rs b/crates/oxc_linter/src/rules/jest/no_mocks_import.rs index 939c0e8dfbf79..71cd1926dfaf8 100644 --- a/crates/oxc_linter/src/rules/jest/no_mocks_import.rs +++ b/crates/oxc_linter/src/rules/jest/no_mocks_import.rs @@ -9,7 +9,7 @@ use crate::{context::LintContext, rule::Rule}; fn no_mocks_import_diagnostic(span: Span) -> OxcDiagnostic { OxcDiagnostic::warn("Mocks should not be manually imported from a `__mocks__` directory.") - .with_help("Instead use `jest.mock` and import from the original module path.") + .with_help("Instead use `jest.mock` or `vi.mock` and import from the original module path.") .with_label(span) } @@ -43,6 +43,17 @@ declare_oxc_lint!( /// import thing from 'thing'; /// require('thing'); /// ``` + /// + /// This rule is compatible with [eslint-plugin-vitest](https://github.com/vitest-dev/eslint-plugin-vitest/blob/main/docs/rules/no-mocks-import.md), + /// to use it, add the following configuration to your `.oxlintrc.json`: + /// + /// ```json + /// { + /// "rules": { + /// "vitest/no-mocks-import": "error" + /// } + /// } + /// ``` NoMocksImport, jest, style @@ -93,7 +104,7 @@ fn contains_mocks_dir(value: &str) -> bool { fn test() { use crate::tester::Tester; - let pass = vec![ + let mut pass = vec![ ("import something from 'something'", None), ("require('somethingElse')", None), ("require('./__mocks__.js')", None), @@ -106,7 +117,7 @@ fn test() { ("entirelyDifferent(fn)", None), ]; - let fail = vec![ + let mut fail = vec![ ("require('./__mocks__')", None), ("require('./__mocks__/')", None), ("require('./__mocks__/index')", None), @@ -116,7 +127,34 @@ fn test() { ("import thing from './__mocks__/index'", None), ]; + let pass_vitest = vec![ + ("import something from 'something'", None), + ("require('somethingElse')", None), + ("require('./__mocks__.js')", None), + ("require('./__mocks__x')", None), + ("require('./__mocks__x/x')", None), + ("require('./x__mocks__')", None), + ("require('./x__mocks__/x')", None), + ("require()", None), + ("var path = './__mocks__.js'; require(path)", None), + ("entirelyDifferent(fn)", None), + ]; + + let fail_vitest = vec![ + ("require('./__mocks__')", None), + ("require('./__mocks__/')", None), + ("require('./__mocks__/index')", None), + ("require('__mocks__')", None), + ("require('__mocks__/')", None), + ("require('__mocks__/index')", None), + ("import thing from './__mocks__/index'", None), + ]; + + pass.extend(pass_vitest); + fail.extend(fail_vitest); + Tester::new(NoMocksImport::NAME, NoMocksImport::PLUGIN, pass, fail) .with_jest_plugin(true) + .with_vitest_plugin(true) .test_and_snapshot(); } diff --git a/crates/oxc_linter/src/snapshots/jest_no_mocks_import.snap b/crates/oxc_linter/src/snapshots/jest_no_mocks_import.snap index 2bf7fc02b1907..8ba3285b34996 100644 --- a/crates/oxc_linter/src/snapshots/jest_no_mocks_import.snap +++ b/crates/oxc_linter/src/snapshots/jest_no_mocks_import.snap @@ -6,46 +6,95 @@ source: crates/oxc_linter/src/tester.rs 1 │ require('./__mocks__') · ───────────── ╰──── - help: Instead use `jest.mock` and import from the original module path. + help: Instead use `jest.mock` or `vi.mock` and import from the original module path. ⚠ eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory. ╭─[no_mocks_import.tsx:1:9] 1 │ require('./__mocks__/') · ────────────── ╰──── - help: Instead use `jest.mock` and import from the original module path. + help: Instead use `jest.mock` or `vi.mock` and import from the original module path. ⚠ eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory. ╭─[no_mocks_import.tsx:1:9] 1 │ require('./__mocks__/index') · ─────────────────── ╰──── - help: Instead use `jest.mock` and import from the original module path. + help: Instead use `jest.mock` or `vi.mock` and import from the original module path. ⚠ eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory. ╭─[no_mocks_import.tsx:1:9] 1 │ require('__mocks__') · ─────────── ╰──── - help: Instead use `jest.mock` and import from the original module path. + help: Instead use `jest.mock` or `vi.mock` and import from the original module path. ⚠ eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory. ╭─[no_mocks_import.tsx:1:9] 1 │ require('__mocks__/') · ──────────── ╰──── - help: Instead use `jest.mock` and import from the original module path. + help: Instead use `jest.mock` or `vi.mock` and import from the original module path. ⚠ eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory. ╭─[no_mocks_import.tsx:1:9] 1 │ require('__mocks__/index') · ───────────────── ╰──── - help: Instead use `jest.mock` and import from the original module path. + help: Instead use `jest.mock` or `vi.mock` and import from the original module path. ⚠ eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory. ╭─[no_mocks_import.tsx:1:19] 1 │ import thing from './__mocks__/index' · ─────────────────── ╰──── - help: Instead use `jest.mock` and import from the original module path. + help: Instead use `jest.mock` or `vi.mock` and import from the original module path. + + ⚠ eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory. + ╭─[no_mocks_import.tsx:1:9] + 1 │ require('./__mocks__') + · ───────────── + ╰──── + help: Instead use `jest.mock` or `vi.mock` and import from the original module path. + + ⚠ eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory. + ╭─[no_mocks_import.tsx:1:9] + 1 │ require('./__mocks__/') + · ────────────── + ╰──── + help: Instead use `jest.mock` or `vi.mock` and import from the original module path. + + ⚠ eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory. + ╭─[no_mocks_import.tsx:1:9] + 1 │ require('./__mocks__/index') + · ─────────────────── + ╰──── + help: Instead use `jest.mock` or `vi.mock` and import from the original module path. + + ⚠ eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory. + ╭─[no_mocks_import.tsx:1:9] + 1 │ require('__mocks__') + · ─────────── + ╰──── + help: Instead use `jest.mock` or `vi.mock` and import from the original module path. + + ⚠ eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory. + ╭─[no_mocks_import.tsx:1:9] + 1 │ require('__mocks__/') + · ──────────── + ╰──── + help: Instead use `jest.mock` or `vi.mock` and import from the original module path. + + ⚠ eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory. + ╭─[no_mocks_import.tsx:1:9] + 1 │ require('__mocks__/index') + · ───────────────── + ╰──── + help: Instead use `jest.mock` or `vi.mock` and import from the original module path. + + ⚠ eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory. + ╭─[no_mocks_import.tsx:1:19] + 1 │ import thing from './__mocks__/index' + · ─────────────────── + ╰──── + help: Instead use `jest.mock` or `vi.mock` and import from the original module path. diff --git a/crates/oxc_linter/src/utils/mod.rs b/crates/oxc_linter/src/utils/mod.rs index 005a82538855e..abfd32a3980c5 100644 --- a/crates/oxc_linter/src/utils/mod.rs +++ b/crates/oxc_linter/src/utils/mod.rs @@ -33,7 +33,7 @@ pub use self::{ /// List of Jest rules that have Vitest equivalents. // When adding a new rule to this list, please ensure oxlint-migrate is also updated. // See https://github.com/oxc-project/oxlint-migrate/blob/2c336c67d75adb09a402ae66fb3099f1dedbe516/scripts/constants.ts -const VITEST_COMPATIBLE_JEST_RULES: [&str; 35] = [ +const VITEST_COMPATIBLE_JEST_RULES: [&str; 36] = [ "consistent-test-it", "expect-expect", "max-expects", @@ -48,6 +48,7 @@ const VITEST_COMPATIBLE_JEST_RULES: [&str; 35] = [ "no-hooks", "no-identical-title", "no-interpolation-in-snapshots", + "no-mocks-import", "no-restricted-jest-methods", "no-restricted-matchers", "no-standalone-expect",