Skip to content

Commit bf839c1

Browse files
authored
fix(linter): false positive in jest/expect-expect (#7341)
closes #7237
1 parent 167d2d5 commit bf839c1

File tree

1 file changed

+40
-35
lines changed

1 file changed

+40
-35
lines changed

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

+40-35
Original file line numberDiff line numberDiff line change
@@ -248,16 +248,21 @@ fn check_statements<'a>(
248248
visited: &mut FxHashSet<Span>,
249249
ctx: &LintContext<'a>,
250250
) -> bool {
251-
statements.iter().any(|statement| {
252-
if let Statement::ExpressionStatement(expr_stmt) = statement {
253-
return check_assert_function_used(
254-
&expr_stmt.expression,
255-
assert_function_names,
256-
visited,
257-
ctx,
258-
);
251+
statements.iter().any(|statement| match statement {
252+
Statement::ExpressionStatement(expr_stmt) => {
253+
check_assert_function_used(&expr_stmt.expression, assert_function_names, visited, ctx)
259254
}
260-
false
255+
Statement::BlockStatement(block_stmt) => {
256+
check_statements(&block_stmt.body, assert_function_names, visited, ctx)
257+
}
258+
Statement::IfStatement(if_stmt) => {
259+
if let Statement::BlockStatement(block_stmt) = &if_stmt.consequent {
260+
check_statements(&block_stmt.body, assert_function_names, visited, ctx)
261+
} else {
262+
false
263+
}
264+
}
265+
_ => false,
261266
})
262267
}
263268

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

290-
#[test]
291-
fn debug() {
292-
use crate::tester::Tester;
293-
294-
let mut pass: Vec<(&str, Option<serde_json::Value>)> = vec![];
295-
296-
let mut fail = vec![];
297-
298-
let pass_vitest = vec![(
299-
"
300-
import { test } from 'vitest';
301-
test.skip(\"skipped test\", () => {})
302-
",
303-
None,
304-
)];
305-
306-
let fail_vitest = vec![];
307-
308-
pass.extend(pass_vitest);
309-
fail.extend(fail_vitest);
310-
311-
Tester::new(ExpectExpect::NAME, pass, fail)
312-
.with_jest_plugin(true)
313-
.with_vitest_plugin(true)
314-
.test();
315-
}
316295
#[test]
317296
fn test() {
318297
use crate::tester::Tester;
@@ -438,6 +417,32 @@ fn test() {
438417
"#,
439418
None,
440419
),
420+
(
421+
r#"
422+
it("should not warn on await expect", async () => {
423+
if(true) {
424+
const asyncFunction = async () => {
425+
throw new Error('nope')
426+
};
427+
await expect(asyncFunction()).rejects.toThrow();
428+
}
429+
});
430+
"#,
431+
None,
432+
),
433+
(
434+
r#"
435+
it("should not warn on await expect", async () => {
436+
{
437+
const asyncFunction = async () => {
438+
throw new Error('nope')
439+
};
440+
await expect(asyncFunction()).rejects.toThrow();
441+
}
442+
});
443+
"#,
444+
None,
445+
),
441446
];
442447

443448
let mut fail = vec![

0 commit comments

Comments
 (0)