Skip to content

Commit d8e9d01

Browse files
Afsooncamc314
andauthored
feat(linter/eslint-plugin-vitest): set prefer-to-have-been-called-times compatible with jest (#20703)
Related to #4656 Co-authored-by: Cameron <cameron.clark@hey.com>
1 parent cfa7b0f commit d8e9d01

4 files changed

Lines changed: 102 additions & 4 deletions

File tree

crates/oxc_linter/data/vitest_compatible_jest_rules.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"prefer-strict-equal",
3636
"prefer-to-be",
3737
"prefer-to-contain",
38+
"prefer-to-have-been-called-times",
3839
"prefer-to-have-length",
3940
"prefer-todo",
4041
"require-hook",

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

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ declare_oxc_lint!(
5454
/// expect(uncalledFunction).not.toBeCalled();
5555
/// expect(method.mock.calls[0][0]).toStrictEqual(value);
5656
/// ```
57+
///
58+
/// This rule is compatible with [eslint-plugin-vitest](https://github.com/vitest-dev/eslint-plugin-vitest/blob/main/docs/rules/prefer-to-have-been-called-times.md),
59+
/// to use it, add the following configuration to your `.oxlintrc.json`:
60+
///
61+
/// ```json
62+
/// {
63+
/// "rules": {
64+
/// "vitest/prefer-to-have-been-called-times": "error"
65+
/// }
66+
/// }
67+
/// ```
5768
PreferToHaveBeenCalledTimes,
5869
jest,
5970
style,
@@ -182,7 +193,7 @@ impl PreferToHaveBeenCalledTimes {
182193
fn test() {
183194
use crate::tester::Tester;
184195

185-
let pass = vec![
196+
let mut pass = vec![
186197
"expect.assertions(1)",
187198
"expect(fn).toHaveBeenCalledTimes",
188199
"expect(fn.mock.calls).toHaveLength",
@@ -201,14 +212,14 @@ fn test() {
201212
"expect(fn.mock.calls).toContain(1, 2, 3);",
202213
];
203214

204-
let fail = vec![
215+
let mut fail = vec![
205216
"expect(method.mock.calls).toHaveLength(1);",
206217
"expect(method.mock.calls).resolves.toHaveLength(x);",
207218
r#"expect(method["mock"].calls).toHaveLength(0);"#,
208219
"expect(my.method.mock.calls).not.toHaveLength(0);",
209220
];
210221

211-
let fix = vec![
222+
let mut fix = vec![
212223
(
213224
"expect(method.mock.calls).toHaveLength(1);",
214225
"expect(method).toHaveBeenCalledTimes(1);",
@@ -245,8 +256,65 @@ fn test() {
245256
None,
246257
),
247258
];
259+
260+
let pass_vitest = vec![
261+
"expect.assertions(1)",
262+
"expect(fn).toHaveBeenCalledTimes",
263+
"expect(fn.mock.calls).toHaveLength",
264+
"expect(fn.mock.values).toHaveLength(0)",
265+
"expect(fn.values.calls).toHaveLength(0)",
266+
"expect(fn).toHaveBeenCalledTimes(0)",
267+
"expect(fn).resolves.toHaveBeenCalledTimes(10)",
268+
"expect(fn).not.toHaveBeenCalledTimes(10)",
269+
"expect(fn).toHaveBeenCalledTimes(1)",
270+
"expect(fn).toBeCalledTimes(0);",
271+
"expect(fn).toHaveBeenCalledTimes(0);",
272+
"expect(fn);",
273+
"expect(method.mock.calls[0][0]).toStrictEqual(value);",
274+
"expect(fn.mock.length).toEqual(1);",
275+
"expect(fn.mock.calls).toEqual([]);",
276+
"expect(fn.mock.calls).toContain(1, 2, 3);",
277+
];
278+
279+
pass.extend(pass_vitest);
280+
281+
let fail_vitest = vec![
282+
"expect(method.mock.calls).toHaveLength(1);",
283+
"expect(method.mock.calls).resolves.toHaveLength(x);",
284+
r#"expect(method["mock"].calls).toHaveLength(0);"#,
285+
"expect(my.method.mock.calls).not.toHaveLength(0);",
286+
];
287+
288+
fail.extend(fail_vitest);
289+
290+
let fix_vitest = vec![
291+
(
292+
"expect(method.mock.calls).toHaveLength(1);",
293+
"expect(method).toHaveBeenCalledTimes(1);",
294+
None,
295+
),
296+
(
297+
"expect(method.mock.calls).resolves.toHaveLength(x);",
298+
"expect(method).resolves.toHaveBeenCalledTimes(x);",
299+
None,
300+
),
301+
(
302+
r#"expect(method["mock"].calls).toHaveLength(0);"#,
303+
"expect(method).toHaveBeenCalledTimes(0);",
304+
None,
305+
),
306+
(
307+
"expect(my.method.mock.calls).not.toHaveLength(0);",
308+
"expect(my.method).not.toHaveBeenCalledTimes(0);",
309+
None,
310+
),
311+
];
312+
313+
fix.extend(fix_vitest);
314+
248315
Tester::new(PreferToHaveBeenCalledTimes::NAME, PreferToHaveBeenCalledTimes::PLUGIN, pass, fail)
249316
.with_jest_plugin(true)
317+
.with_vitest_plugin(true)
250318
.expect_fix(fix)
251319
.test_and_snapshot();
252320
}

crates/oxc_linter/src/snapshots/jest_prefer_to_have_been_called_times.snap

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,31 @@ source: crates/oxc_linter/src/tester.rs
2929
· ────────────────────────────────────────────────
3030
╰────
3131
help: Use `toHaveBeenCalledTimes()` to assert the number of times a mock function was called
32+
33+
⚠ eslint-plugin-jest(prefer-to-have-been-called-times): Prefer `toHaveBeenCalledTimes()` over `toHaveLength()` when asserting mock call counts
34+
╭─[prefer_to_have_been_called_times.tsx:1:1]
35+
1 │ expect(method.mock.calls).toHaveLength(1);
36+
· ─────────────────────────────────────────
37+
╰────
38+
help: Use `toHaveBeenCalledTimes()` to assert the number of times a mock function was called
39+
40+
⚠ eslint-plugin-jest(prefer-to-have-been-called-times): Prefer `toHaveBeenCalledTimes()` over `toHaveLength()` when asserting mock call counts
41+
╭─[prefer_to_have_been_called_times.tsx:1:1]
42+
1 │ expect(method.mock.calls).resolves.toHaveLength(x);
43+
· ──────────────────────────────────────────────────
44+
╰────
45+
help: Use `toHaveBeenCalledTimes()` to assert the number of times a mock function was called
46+
47+
⚠ eslint-plugin-jest(prefer-to-have-been-called-times): Prefer `toHaveBeenCalledTimes()` over `toHaveLength()` when asserting mock call counts
48+
╭─[prefer_to_have_been_called_times.tsx:1:1]
49+
1 │ expect(method["mock"].calls).toHaveLength(0);
50+
· ────────────────────────────────────────────
51+
╰────
52+
help: Use `toHaveBeenCalledTimes()` to assert the number of times a mock function was called
53+
54+
⚠ eslint-plugin-jest(prefer-to-have-been-called-times): Prefer `toHaveBeenCalledTimes()` over `toHaveLength()` when asserting mock call counts
55+
╭─[prefer_to_have_been_called_times.tsx:1:1]
56+
1 │ expect(my.method.mock.calls).not.toHaveLength(0);
57+
· ────────────────────────────────────────────────
58+
╰────
59+
help: Use `toHaveBeenCalledTimes()` to assert the number of times a mock function was called

crates/oxc_linter/src/utils/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub use self::{
3737
// the crates/oxc_linter/data/vitest_compatible_jest_rules.json
3838
// file is also updated. The JSON file is used by the oxlint-migrate
3939
// and eslint-plugin-oxlint repos to keep everything synced.
40-
const VITEST_COMPATIBLE_JEST_RULES: [&str; 43] = [
40+
const VITEST_COMPATIBLE_JEST_RULES: [&str; 44] = [
4141
"consistent-test-it",
4242
"expect-expect",
4343
"max-expects",
@@ -74,6 +74,7 @@ const VITEST_COMPATIBLE_JEST_RULES: [&str; 43] = [
7474
"prefer-strict-equal",
7575
"prefer-to-be",
7676
"prefer-to-contain",
77+
"prefer-to-have-been-called-times",
7778
"prefer-to-have-length",
7879
"prefer-todo",
7980
"require-hook",

0 commit comments

Comments
 (0)