Skip to content

Commit 9d12206

Browse files
committed
refactor(linter): move rule prefer-each from vitest to jest + remapping
1 parent 870a583 commit 9d12206

5 files changed

Lines changed: 542 additions & 126 deletions

File tree

crates/oxc_linter/src/rules.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ mod jest {
236236
pub mod no_untyped_mock_factory;
237237
pub mod prefer_called_with;
238238
pub mod prefer_comparison_matcher;
239+
pub mod prefer_each;
239240
pub mod prefer_equality_matcher;
240241
pub mod prefer_expect_resolves;
241242
pub mod prefer_hooks_in_order;
@@ -515,7 +516,6 @@ mod promise {
515516
mod vitest {
516517
pub mod no_conditional_tests;
517518
pub mod no_import_node_test;
518-
pub mod prefer_each;
519519
pub mod prefer_to_be_falsy;
520520
pub mod prefer_to_be_object;
521521
pub mod prefer_to_be_truthy;
@@ -704,6 +704,7 @@ oxc_macros::declare_all_lint_rules! {
704704
jest::no_test_prefixes,
705705
jest::no_test_return_statement,
706706
jest::no_untyped_mock_factory,
707+
jest::prefer_each,
707708
jest::prefer_called_with,
708709
jest::prefer_comparison_matcher,
709710
jest::prefer_equality_matcher,
@@ -1000,7 +1001,6 @@ oxc_macros::declare_all_lint_rules! {
10001001
unicorn::throw_new_error,
10011002
vitest::no_conditional_tests,
10021003
vitest::no_import_node_test,
1003-
vitest::prefer_each,
10041004
vitest::prefer_to_be_falsy,
10051005
vitest::prefer_to_be_object,
10061006
vitest::prefer_to_be_truthy,

crates/oxc_linter/src/rules/vitest/prefer_each.rs renamed to crates/oxc_linter/src/rules/jest/prefer_each.rs

Lines changed: 143 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ declare_oxc_lint!(
6464
/// })
6565
/// ```
6666
PreferEach,
67-
vitest,
67+
jest,
6868
style,
6969
);
7070

@@ -147,7 +147,140 @@ impl PreferEach {
147147
fn test() {
148148
use crate::tester::Tester;
149149

150-
let pass = vec![
150+
let pass_jest = vec![
151+
r#"it("is true", () => { expect(true).toBe(false) });"#,
152+
r#"it.each(getNumbers())("only returns numbers that are greater than seven", number => {
153+
expect(number).toBeGreaterThan(7);
154+
});"#,
155+
r#"it("returns numbers that are greater than five", function () {
156+
for (const number of getNumbers()) {
157+
expect(number).toBeGreaterThan(5);
158+
}
159+
});"#,
160+
r#"it("returns things that are less than ten", function () {
161+
for (const thing in things) {
162+
expect(thing).toBeLessThan(10);
163+
}
164+
});"#,
165+
r#"it("only returns numbers that are greater than seven", function () {
166+
const numbers = getNumbers();
167+
for (let i = 0; i < numbers.length; i++) {
168+
expect(numbers[i]).toBeGreaterThan(7);
169+
}
170+
});"#,
171+
];
172+
173+
let fail_jest = vec![
174+
"for (const [input, expected] of data) {
175+
it(\\`results in $\\{expected}\\`, () => {
176+
expect(fn(input)).toBe(expected)
177+
});
178+
}",
179+
"for (const [input, expected] of data) {
180+
describe(\\`when the input is $\\{input}\\`, () => {
181+
it(\\`results in $\\{expected}\\`, () => {
182+
expect(fn(input)).toBe(expected)
183+
});
184+
});
185+
}",
186+
"for (const [input, expected] of data) {
187+
describe(\\`when the input is $\\{input}\\`, () => {
188+
it(\\`results in $\\{expected}\\`, () => {
189+
expect(fn(input)).toBe(expected)
190+
});
191+
});
192+
}
193+
for (const [input, expected] of data) {
194+
it.skip(\\`results in $\\{expected}\\`, () => {
195+
expect(fn(input)).toBe(expected)
196+
});
197+
}",
198+
"for (const [input, expected] of data) {
199+
it.skip(\\`results in $\\{expected}\\`, () => {
200+
expect(fn(input)).toBe(expected)
201+
});
202+
}",
203+
"it('is true', () => {
204+
expect(true).toBe(false);
205+
});
206+
for (const [input, expected] of data) {
207+
it.skip(\\`results in $\\{expected}\\`, () => {
208+
expect(fn(input)).toBe(expected)
209+
});
210+
}",
211+
"for (const [input, expected] of data) {
212+
it.skip(\\`results in $\\{expected}\\`, () => {
213+
expect(fn(input)).toBe(expected)
214+
});
215+
}
216+
it('is true', () => {
217+
expect(true).toBe(false);
218+
});",
219+
"it('is true', () => {
220+
expect(true).toBe(false);
221+
});
222+
for (const [input, expected] of data) {
223+
it.skip(\\`results in $\\{expected}\\`, () => {
224+
expect(fn(input)).toBe(expected)
225+
});
226+
}
227+
it('is true', () => {
228+
expect(true).toBe(false);
229+
});",
230+
"for (const [input, expected] of data) {
231+
it(\\`results in $\\{expected}\\`, () => {
232+
expect(fn(input)).toBe(expected)
233+
});
234+
it(\\`results in $\\{expected}\\`, () => {
235+
expect(fn(input)).toBe(expected)
236+
});
237+
}",
238+
"for (const [input, expected] of data) {
239+
it(\\`results in $\\{expected}\\`, () => {
240+
expect(fn(input)).toBe(expected)
241+
});
242+
}
243+
for (const [input, expected] of data) {
244+
it(\\`results in $\\{expected}\\`, () => {
245+
expect(fn(input)).toBe(expected)
246+
});
247+
}",
248+
"for (const [input, expected] of data) {
249+
it(\\`results in $\\{expected}\\`, () => {
250+
expect(fn(input)).toBe(expected)
251+
});
252+
}
253+
for (const [input, expected] of data) {
254+
test(\\`results in $\\{expected}\\`, () => {
255+
expect(fn(input)).toBe(expected)
256+
});
257+
}",
258+
"for (const [input, expected] of data) {
259+
beforeEach(() => setupSomething(input));
260+
test(\\`results in $\\{expected}\\`, () => {
261+
expect(doSomething()).toBe(expected)
262+
});
263+
}",
264+
r#"for (const [input, expected] of data) {
265+
it("only returns numbers that are greater than seven", function () {
266+
const numbers = getNumbers(input);
267+
for (let i = 0; i < numbers.length; i++) {
268+
expect(numbers[i]).toBeGreaterThan(7);
269+
}
270+
});
271+
}"#,
272+
r#"for (const [input, expected] of data) {
273+
beforeEach(() => setupSomething(input));
274+
it("only returns numbers that are greater than seven", function () {
275+
const numbers = getNumbers();
276+
for (let i = 0; i < numbers.length; i++) {
277+
expect(numbers[i]).toBeGreaterThan(7);
278+
}
279+
});
280+
}"#,
281+
];
282+
283+
let pass_vitest = vec![
151284
r#"it("is true", () => { expect(true).toBe(false) });"#,
152285
r#"it.each(getNumbers())("only returns numbers that are greater than seven", number => {
153286
expect(number).toBeGreaterThan(7);
@@ -171,7 +304,7 @@ fn test() {
171304
});"#,
172305
];
173306

174-
let fail = vec![
307+
let fail_vitest = vec![
175308
" for (const [input, expected] of data) {
176309
it(`results in ${expected}`, () => {
177310
expect(fn(input)).toBe(expected)
@@ -286,5 +419,12 @@ fn test() {
286419
"#,
287420
];
288421

422+
let mut pass = vec![];
423+
pass.extend(pass_jest);
424+
pass.extend(pass_vitest);
425+
let mut fail = vec![];
426+
fail.extend(fail_jest);
427+
fail.extend(fail_vitest);
428+
289429
Tester::new(PreferEach::NAME, PreferEach::PLUGIN, pass, fail).test_and_snapshot();
290430
}

0 commit comments

Comments
 (0)