Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions crates/oxc_linter/src/rules/jest/no_mocks_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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();
}
63 changes: 56 additions & 7 deletions crates/oxc_linter/src/snapshots/jest_no_mocks_import.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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.
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
Loading