Skip to content

Commit 51019f0

Browse files
committed
feat(linter/eslint-plugin-vitest): Extend test with vitest test cases and message based on framework
1 parent 9feb43c commit 51019f0

File tree

2 files changed

+102
-5
lines changed

2 files changed

+102
-5
lines changed

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

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@ use oxc_span::Span;
77

88
use crate::{context::LintContext, rule::Rule};
99

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

16+
fn no_mocks_vitest_import_diagnostic(span: Span) -> OxcDiagnostic {
17+
OxcDiagnostic::warn("Mocks should not be manually imported from a `__mocks__` directory.")
18+
.with_help("Instead use `vi.mock` and import from the original module path.")
19+
.with_label(span)
20+
}
21+
1622
// <https://github.com/jest-community/eslint-plugin-jest/blob/v28.9.0/docs/rules/no-mocks-import.md>
1723
#[derive(Debug, Default, Clone)]
1824
pub struct NoMocksImport;
@@ -66,7 +72,17 @@ impl Rule for NoMocksImport {
6672
for import_entry in &module_records.import_entries {
6773
let module_specifier = import_entry.module_request.name();
6874
if contains_mocks_dir(module_specifier) {
69-
ctx.diagnostic(no_mocks_import_diagnostic(import_entry.module_request.span));
75+
if ctx.frameworks().is_vitest() {
76+
ctx.diagnostic(no_mocks_vitest_import_diagnostic(
77+
import_entry.module_request.span,
78+
));
79+
}
80+
81+
if ctx.frameworks().is_jest() {
82+
ctx.diagnostic(no_mocks_jest_import_diagnostic(
83+
import_entry.module_request.span,
84+
));
85+
}
7086
}
7187
}
7288

@@ -87,7 +103,13 @@ impl Rule for NoMocksImport {
87103
};
88104

89105
if contains_mocks_dir(&string_literal.value) {
90-
ctx.diagnostic(no_mocks_import_diagnostic(string_literal.span));
106+
if ctx.frameworks().is_vitest() {
107+
ctx.diagnostic(no_mocks_vitest_import_diagnostic(string_literal.span));
108+
}
109+
110+
if ctx.frameworks().is_jest() {
111+
ctx.diagnostic(no_mocks_jest_import_diagnostic(string_literal.span));
112+
}
91113
}
92114
}
93115
}
@@ -104,7 +126,7 @@ fn contains_mocks_dir(value: &str) -> bool {
104126
fn test() {
105127
use crate::tester::Tester;
106128

107-
let pass = vec![
129+
let mut pass = vec![
108130
("import something from 'something'", None),
109131
("require('somethingElse')", None),
110132
("require('./__mocks__.js')", None),
@@ -117,7 +139,7 @@ fn test() {
117139
("entirelyDifferent(fn)", None),
118140
];
119141

120-
let fail = vec![
142+
let mut fail = vec![
121143
("require('./__mocks__')", None),
122144
("require('./__mocks__/')", None),
123145
("require('./__mocks__/index')", None),
@@ -127,6 +149,32 @@ fn test() {
127149
("import thing from './__mocks__/index'", None),
128150
];
129151

152+
let pass_vitest = vec![
153+
("import something from 'something'", None),
154+
("require('somethingElse')", None),
155+
("require('./__mocks__.js')", None),
156+
("require('./__mocks__x')", None),
157+
("require('./__mocks__x/x')", None),
158+
("require('./x__mocks__')", None),
159+
("require('./x__mocks__/x')", None),
160+
("require()", None),
161+
("var path = './__mocks__.js'; require(path)", None),
162+
("entirelyDifferent(fn)", None),
163+
];
164+
165+
let fail_vitest = vec![
166+
("require('./__mocks__')", None),
167+
("require('./__mocks__/')", None),
168+
("require('./__mocks__/index')", None),
169+
("require('__mocks__')", None),
170+
("require('__mocks__/')", None),
171+
("require('__mocks__/index')", None),
172+
("import thing from './__mocks__/index'", None),
173+
];
174+
175+
pass.extend(pass_vitest);
176+
fail.extend(fail_vitest);
177+
130178
Tester::new(NoMocksImport::NAME, NoMocksImport::PLUGIN, pass, fail)
131179
.with_jest_plugin(true)
132180
.with_vitest_plugin(true)

crates/oxc_linter/src/snapshots/jest_no_mocks_import.snap

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,52 @@ source: crates/oxc_linter/src/tester.rs
4949
· ───────────────────
5050
╰────
5151
help: Instead use `jest.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` 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` 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` 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` 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` 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` 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` and import from the original module path.

0 commit comments

Comments
 (0)