Skip to content

Commit a1d9bbd

Browse files
Afsooncamc314
andauthored
feat(linter/eslint-plugin-vitest): reuse no-mocks-import jest linter rule (#16540)
Related to #4656 Implement [no-mocks-import](https://github.com/vitest-dev/eslint-plugin-vitest/blob/main/docs/rules/no-mocks-import.md) lint ruler adding a new entry in the mapping between jest and vitest compatible rules. Oxlint-migrate PR related: oxc-project/oxlint-migrate#269 --------- Co-authored-by: Cameron Clark <[email protected]>
1 parent 8854039 commit a1d9bbd

File tree

3 files changed

+99
-11
lines changed

3 files changed

+99
-11
lines changed

crates/oxc_linter/src/rules/jest/no_mocks_import.rs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{context::LintContext, rule::Rule};
99

1010
fn no_mocks_import_diagnostic(span: Span) -> OxcDiagnostic {
1111
OxcDiagnostic::warn("Mocks should not be manually imported from a `__mocks__` directory.")
12-
.with_help("Instead use `jest.mock` and import from the original module path.")
12+
.with_help("Instead use `jest.mock` or `vi.mock` and import from the original module path.")
1313
.with_label(span)
1414
}
1515

@@ -43,6 +43,17 @@ declare_oxc_lint!(
4343
/// import thing from 'thing';
4444
/// require('thing');
4545
/// ```
46+
///
47+
/// This rule is compatible with [eslint-plugin-vitest](https://github.com/vitest-dev/eslint-plugin-vitest/blob/main/docs/rules/no-mocks-import.md),
48+
/// to use it, add the following configuration to your `.oxlintrc.json`:
49+
///
50+
/// ```json
51+
/// {
52+
/// "rules": {
53+
/// "vitest/no-mocks-import": "error"
54+
/// }
55+
/// }
56+
/// ```
4657
NoMocksImport,
4758
jest,
4859
style
@@ -93,7 +104,7 @@ fn contains_mocks_dir(value: &str) -> bool {
93104
fn test() {
94105
use crate::tester::Tester;
95106

96-
let pass = vec![
107+
let mut pass = vec![
97108
("import something from 'something'", None),
98109
("require('somethingElse')", None),
99110
("require('./__mocks__.js')", None),
@@ -106,7 +117,7 @@ fn test() {
106117
("entirelyDifferent(fn)", None),
107118
];
108119

109-
let fail = vec![
120+
let mut fail = vec![
110121
("require('./__mocks__')", None),
111122
("require('./__mocks__/')", None),
112123
("require('./__mocks__/index')", None),
@@ -116,7 +127,34 @@ fn test() {
116127
("import thing from './__mocks__/index'", None),
117128
];
118129

130+
let pass_vitest = vec![
131+
("import something from 'something'", None),
132+
("require('somethingElse')", None),
133+
("require('./__mocks__.js')", None),
134+
("require('./__mocks__x')", None),
135+
("require('./__mocks__x/x')", None),
136+
("require('./x__mocks__')", None),
137+
("require('./x__mocks__/x')", None),
138+
("require()", None),
139+
("var path = './__mocks__.js'; require(path)", None),
140+
("entirelyDifferent(fn)", None),
141+
];
142+
143+
let fail_vitest = vec![
144+
("require('./__mocks__')", None),
145+
("require('./__mocks__/')", None),
146+
("require('./__mocks__/index')", None),
147+
("require('__mocks__')", None),
148+
("require('__mocks__/')", None),
149+
("require('__mocks__/index')", None),
150+
("import thing from './__mocks__/index'", None),
151+
];
152+
153+
pass.extend(pass_vitest);
154+
fail.extend(fail_vitest);
155+
119156
Tester::new(NoMocksImport::NAME, NoMocksImport::PLUGIN, pass, fail)
120157
.with_jest_plugin(true)
158+
.with_vitest_plugin(true)
121159
.test_and_snapshot();
122160
}

crates/oxc_linter/src/snapshots/jest_no_mocks_import.snap

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,95 @@ source: crates/oxc_linter/src/tester.rs
66
1require('./__mocks__')
77
· ─────────────
88
╰────
9-
help: Instead use `jest.mock` and import from the original module path.
9+
help: Instead use `jest.mock` or `vi.mock` and import from the original module path.
1010

1111
eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory.
1212
╭─[no_mocks_import.tsx:1:9]
1313
1require('./__mocks__/')
1414
· ──────────────
1515
╰────
16-
help: Instead use `jest.mock` and import from the original module path.
16+
help: Instead use `jest.mock` or `vi.mock` and import from the original module path.
1717

1818
eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory.
1919
╭─[no_mocks_import.tsx:1:9]
2020
1require('./__mocks__/index')
2121
· ───────────────────
2222
╰────
23-
help: Instead use `jest.mock` and import from the original module path.
23+
help: Instead use `jest.mock` or `vi.mock` and import from the original module path.
2424

2525
eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory.
2626
╭─[no_mocks_import.tsx:1:9]
2727
1require('__mocks__')
2828
· ───────────
2929
╰────
30-
help: Instead use `jest.mock` and import from the original module path.
30+
help: Instead use `jest.mock` or `vi.mock` and import from the original module path.
3131

3232
eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory.
3333
╭─[no_mocks_import.tsx:1:9]
3434
1require('__mocks__/')
3535
· ────────────
3636
╰────
37-
help: Instead use `jest.mock` and import from the original module path.
37+
help: Instead use `jest.mock` or `vi.mock` and import from the original module path.
3838

3939
eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory.
4040
╭─[no_mocks_import.tsx:1:9]
4141
1require('__mocks__/index')
4242
· ─────────────────
4343
╰────
44-
help: Instead use `jest.mock` and import from the original module path.
44+
help: Instead use `jest.mock` or `vi.mock` and import from the original module path.
4545

4646
eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory.
4747
╭─[no_mocks_import.tsx:1:19]
4848
1import thing from './__mocks__/index'
4949
· ───────────────────
5050
╰────
51-
help: Instead use `jest.mock` and import from the original module path.
51+
help: Instead use `jest.mock` or `vi.mock` and import from the original module path.
52+
53+
eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory.
54+
╭─[no_mocks_import.tsx:1:9]
55+
1require('./__mocks__')
56+
· ─────────────
57+
╰────
58+
help: Instead use `jest.mock` or `vi.mock` and import from the original module path.
59+
60+
eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory.
61+
╭─[no_mocks_import.tsx:1:9]
62+
1require('./__mocks__/')
63+
· ──────────────
64+
╰────
65+
help: Instead use `jest.mock` or `vi.mock` and import from the original module path.
66+
67+
eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory.
68+
╭─[no_mocks_import.tsx:1:9]
69+
1require('./__mocks__/index')
70+
· ───────────────────
71+
╰────
72+
help: Instead use `jest.mock` or `vi.mock` and import from the original module path.
73+
74+
eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory.
75+
╭─[no_mocks_import.tsx:1:9]
76+
1require('__mocks__')
77+
· ───────────
78+
╰────
79+
help: Instead use `jest.mock` or `vi.mock` and import from the original module path.
80+
81+
eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory.
82+
╭─[no_mocks_import.tsx:1:9]
83+
1require('__mocks__/')
84+
· ────────────
85+
╰────
86+
help: Instead use `jest.mock` or `vi.mock` and import from the original module path.
87+
88+
eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory.
89+
╭─[no_mocks_import.tsx:1:9]
90+
1require('__mocks__/index')
91+
· ─────────────────
92+
╰────
93+
help: Instead use `jest.mock` or `vi.mock` and import from the original module path.
94+
95+
eslint-plugin-jest(no-mocks-import): Mocks should not be manually imported from a `__mocks__` directory.
96+
╭─[no_mocks_import.tsx:1:19]
97+
1import thing from './__mocks__/index'
98+
· ───────────────────
99+
╰────
100+
help: Instead use `jest.mock` or `vi.mock` and import from the original module path.

crates/oxc_linter/src/utils/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub use self::{
3333
/// List of Jest rules that have Vitest equivalents.
3434
// When adding a new rule to this list, please ensure oxlint-migrate is also updated.
3535
// See https://github.com/oxc-project/oxlint-migrate/blob/2c336c67d75adb09a402ae66fb3099f1dedbe516/scripts/constants.ts
36-
const VITEST_COMPATIBLE_JEST_RULES: [&str; 35] = [
36+
const VITEST_COMPATIBLE_JEST_RULES: [&str; 36] = [
3737
"consistent-test-it",
3838
"expect-expect",
3939
"max-expects",
@@ -48,6 +48,7 @@ const VITEST_COMPATIBLE_JEST_RULES: [&str; 35] = [
4848
"no-hooks",
4949
"no-identical-title",
5050
"no-interpolation-in-snapshots",
51+
"no-mocks-import",
5152
"no-restricted-jest-methods",
5253
"no-restricted-matchers",
5354
"no-standalone-expect",

0 commit comments

Comments
 (0)