Skip to content

Commit

Permalink
fix(linter): false positive in jest/expect-expect (#7341)
Browse files Browse the repository at this point in the history
closes #7237
  • Loading branch information
shulaoda authored Nov 18, 2024
1 parent 167d2d5 commit bf839c1
Showing 1 changed file with 40 additions and 35 deletions.
75 changes: 40 additions & 35 deletions crates/oxc_linter/src/rules/jest/expect_expect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,21 @@ fn check_statements<'a>(
visited: &mut FxHashSet<Span>,
ctx: &LintContext<'a>,
) -> bool {
statements.iter().any(|statement| {
if let Statement::ExpressionStatement(expr_stmt) = statement {
return check_assert_function_used(
&expr_stmt.expression,
assert_function_names,
visited,
ctx,
);
statements.iter().any(|statement| match statement {
Statement::ExpressionStatement(expr_stmt) => {
check_assert_function_used(&expr_stmt.expression, assert_function_names, visited, ctx)
}
false
Statement::BlockStatement(block_stmt) => {
check_statements(&block_stmt.body, assert_function_names, visited, ctx)
}
Statement::IfStatement(if_stmt) => {
if let Statement::BlockStatement(block_stmt) = &if_stmt.consequent {
check_statements(&block_stmt.body, assert_function_names, visited, ctx)
} else {
false
}
}
_ => false,
})
}

Expand Down Expand Up @@ -287,32 +292,6 @@ fn convert_pattern(pattern: &str) -> CompactStr {
format!("(?ui)^{pattern}(\\.|$)").into()
}

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

let mut pass: Vec<(&str, Option<serde_json::Value>)> = vec![];

let mut fail = vec![];

let pass_vitest = vec![(
"
import { test } from 'vitest';
test.skip(\"skipped test\", () => {})
",
None,
)];

let fail_vitest = vec![];

pass.extend(pass_vitest);
fail.extend(fail_vitest);

Tester::new(ExpectExpect::NAME, pass, fail)
.with_jest_plugin(true)
.with_vitest_plugin(true)
.test();
}
#[test]
fn test() {
use crate::tester::Tester;
Expand Down Expand Up @@ -438,6 +417,32 @@ fn test() {
"#,
None,
),
(
r#"
it("should not warn on await expect", async () => {
if(true) {
const asyncFunction = async () => {
throw new Error('nope')
};
await expect(asyncFunction()).rejects.toThrow();
}
});
"#,
None,
),
(
r#"
it("should not warn on await expect", async () => {
{
const asyncFunction = async () => {
throw new Error('nope')
};
await expect(asyncFunction()).rejects.toThrow();
}
});
"#,
None,
),
];

let mut fail = vec![
Expand Down

0 comments on commit bf839c1

Please sign in to comment.