Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(linter): eslint-plugin-jest: prefer-called-with #2163

Merged
merged 5 commits into from
Jan 25, 2024
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
2 changes: 2 additions & 0 deletions crates/oxc_linter/src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ mod jest {
pub mod no_standalone_expect;
pub mod no_test_prefixes;
pub mod no_test_return_statement;
pub mod prefer_called_with;
pub mod prefer_todo;
pub mod valid_describe_callback;
pub mod valid_expect;
Expand Down Expand Up @@ -422,6 +423,7 @@ oxc_macros::declare_all_lint_rules! {
jest::no_standalone_expect,
jest::no_test_prefixes,
jest::no_test_return_statement,
jest::prefer_called_with,
jest::prefer_todo,
jest::valid_describe_callback,
jest::valid_expect,
Expand Down
128 changes: 128 additions & 0 deletions crates/oxc_linter/src/rules/jest/prefer_called_with.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
use crate::{
context::LintContext,
rule::Rule,
utils::{collect_possible_jest_call_node, parse_expect_jest_fn_call, PossibleJestNode},
};

use oxc_ast::AstKind;
use oxc_diagnostics::{
miette::{self, Diagnostic},
thiserror::Error,
};
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;

#[derive(Debug, Error, Diagnostic)]
enum PreferCalledWithDiagnostic {
#[error("eslint-plugin-jest(prefer-called-with): Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()`.")]
#[diagnostic(severity(warning), help("Prefer toBeCalledWith(/* expected args */)"))]
UseToBeCalledWith(#[label] Span),
#[error("eslint-plugin-jest(prefer-called-with): Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()`.")]
#[diagnostic(severity(warning), help("Prefer toHaveBeenCalledWith(/* expected args */)"))]
UseHaveBeenCalledWith(#[label] Span),
}

#[derive(Debug, Default, Clone)]
pub struct PreferCalledWith;

declare_oxc_lint!(
/// ### What it does
/// Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()`
///
/// ### Example
///
/// ```javascript
///
/// // valid
/// expect(noArgsFunction).toBeCalledWith();
/// expect(roughArgsFunction).toBeCalledWith(expect.anything(), expect.any(Date));
/// expect(anyArgsFunction).toBeCalledTimes(1);
/// expect(uncalledFunction).not.toBeCalled();
///
/// // invalid
/// expect(someFunction).toBeCalled();
/// expect(someFunction).toHaveBeenCalled();
/// ```
///
PreferCalledWith,
style,
);

impl Rule for PreferCalledWith {
fn run_once(&self, ctx: &LintContext<'_>) {
for possible_jest_node in &collect_possible_jest_call_node(ctx) {
Self::run(possible_jest_node, ctx);
}
}
}

impl PreferCalledWith {
pub fn run<'a>(possible_jest_node: &PossibleJestNode<'a, '_>, ctx: &LintContext<'a>) {
let node = possible_jest_node.node;
let AstKind::CallExpression(call_expr) = node.kind() else {
return;
};

let Some(jest_fn_call) = parse_expect_jest_fn_call(call_expr, possible_jest_node, ctx)
else {
return;
};

let mut has_not_modifier = false;
for modifier in jest_fn_call.modifiers() {
if let Some(modifier_name) = modifier.name() {
if modifier_name.eq("not") {
has_not_modifier = true;
break;
}
}
}

if has_not_modifier {
return;
}

if let Some(matcher_property) = jest_fn_call.matcher() {
if let Some(matcher_name) = matcher_property.name() {
if matcher_name == "toBeCalled" {
ctx.diagnostic(PreferCalledWithDiagnostic::UseToBeCalledWith(
matcher_property.span,
));
} else if matcher_name == "toHaveBeenCalled" {
ctx.diagnostic(PreferCalledWithDiagnostic::UseHaveBeenCalledWith(
matcher_property.span,
));
}
}
}
}
}

#[test]
fn test() {
use crate::tester::Tester;

let pass = vec![
("expect(fn).toBeCalledWith();", None),
("expect(fn).toHaveBeenCalledWith();", None),
("expect(fn).toBeCalledWith(expect.anything());", None),
("expect(fn).toHaveBeenCalledWith(expect.anything());", None),
("expect(fn).not.toBeCalled();", None),
("expect(fn).rejects.not.toBeCalled();", None),
("expect(fn).not.toHaveBeenCalled();", None),
("expect(fn).not.toBeCalledWith();", None),
("expect(fn).not.toHaveBeenCalledWith();", None),
("expect(fn).resolves.not.toHaveBeenCalledWith();", None),
("expect(fn).toBeCalledTimes(0);", None),
("expect(fn).toHaveBeenCalledTimes(0);", None),
("expect(fn);", None),
];

let fail = vec![
("expect(fn).toBeCalled();", None),
("expect(fn).resolves.toBeCalled();", None),
("expect(fn).toHaveBeenCalled();", None),
];

Tester::new(PreferCalledWith::NAME, pass, fail).with_jest_plugin(true).test_and_snapshot();
}
27 changes: 27 additions & 0 deletions crates/oxc_linter/src/snapshots/prefer_called_with.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
source: crates/oxc_linter/src/tester.rs
assertion_line: 150
expression: prefer_called_with
---
⚠ eslint-plugin-jest(prefer-called-with): Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()`.
╭─[prefer_called_with.tsx:1:1]
1 │ expect(fn).toBeCalled();
· ──────────
╰────
help: Prefer toBeCalledWith(/* expected args */)

⚠ eslint-plugin-jest(prefer-called-with): Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()`.
╭─[prefer_called_with.tsx:1:1]
1 │ expect(fn).resolves.toBeCalled();
· ──────────
╰────
help: Prefer toBeCalledWith(/* expected args */)

⚠ eslint-plugin-jest(prefer-called-with): Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()`.
╭─[prefer_called_with.tsx:1:1]
1 │ expect(fn).toHaveBeenCalled();
· ────────────────
╰────
help: Prefer toHaveBeenCalledWith(/* expected args */)