From 5a4e61132e52634ed54e5a29ffc384225d7c5462 Mon Sep 17 00:00:00 2001 From: Wenzhe Wang Date: Thu, 9 Nov 2023 13:53:24 +0800 Subject: [PATCH] fix(linter): detect assert function in Await Expression (#1202) fixes: #1199 --- .../src/rules/jest/expect_expect.rs | 25 +++++++++++++++++++ .../src/snapshots/expect_expect.snap | 13 ++++++++++ 2 files changed, 38 insertions(+) diff --git a/crates/oxc_linter/src/rules/jest/expect_expect.rs b/crates/oxc_linter/src/rules/jest/expect_expect.rs index 300bf3333..1b5370b25 100644 --- a/crates/oxc_linter/src/rules/jest/expect_expect.rs +++ b/crates/oxc_linter/src/rules/jest/expect_expect.rs @@ -162,6 +162,9 @@ fn check_assert_function_used<'a>( }; return check_statements(&body.statements, assert_function_names, ctx); } + Expression::AwaitExpression(expr) => { + return check_assert_function_used(&expr.argument, assert_function_names, ctx); + } _ => {} }; @@ -354,6 +357,17 @@ fn test() { ", Some(serde_json::json!([{ "assertFunctionNames": ["tester.foo.bar.expect"] }])), ), + ( + r#" + it("should not warn on await expect", async () => { + const asyncFunction = async () => { + throw new Error('nope') + }; + await expect(asyncFunction()).rejects.toThrow(); + }); + "#, + None, + ), ]; let fail = vec![ @@ -422,6 +436,17 @@ fn test() { ", None, ), + ( + r#" + it("should warn on non-assert await expression", async () => { + const asyncFunction = async () => { + throw new Error('nope') + }; + await foo(asyncFunction()).rejects.toThrow(); + }); + "#, + None, + ), ]; Tester::new(ExpectExpect::NAME, pass, fail).with_jest_plugin(true).test_and_snapshot(); diff --git a/crates/oxc_linter/src/snapshots/expect_expect.snap b/crates/oxc_linter/src/snapshots/expect_expect.snap index bd53df101..2a774565c 100644 --- a/crates/oxc_linter/src/snapshots/expect_expect.snap +++ b/crates/oxc_linter/src/snapshots/expect_expect.snap @@ -123,4 +123,17 @@ expression: expect_expect ╰──── help: Add assertion(s) in this Test + ⚠ eslint-plugin-jest(expect-expect): Test has no assertions + ╭─[expect_expect.tsx:1:1] + 1 │ + 2 │ ╭─▶ it("should warn on non-assert await expression", async () => { + 3 │ │ const asyncFunction = async () => { + 4 │ │ throw new Error('nope') + 5 │ │ }; + 6 │ │ await foo(asyncFunction()).rejects.toThrow(); + 7 │ ╰─▶ }); + 8 │ + ╰──── + help: Add assertion(s) in this Test +