fix(linter): detect assert function in Await Expression (#1202)

fixes: #1199
This commit is contained in:
Wenzhe Wang 2023-11-09 13:53:24 +08:00 committed by GitHub
parent 884a819cd6
commit 5a4e61132e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 0 deletions

View file

@ -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();

View file

@ -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