Skip to content

Commit

Permalink
feat(linter): allow to call expect inside waitFor (#2521)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Apr 19, 2024
1 parent af3c2a8 commit a3a1360
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b
```
Contributed by @ematipico

#### Enhancements

- The rule `noMisplacedAssertions` now considers valid calling `expect` inside `waitFor`:
```js
import { waitFor } from '@testing-library/react';

await waitFor(() => {
expect(111).toBe(222);
});
```
Contributed by @ematipico


### Parser


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ declare_rule! {
/// })
/// ```
///
/// ```js
/// import { waitFor } from '@testing-library/react';
/// await waitFor(() => {
/// expect(111).toBe(222);
/// });
/// ```
///
pub NoMisplacedAssertion {
version: "1.6.4",
name: "noMisplacedAssertion",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
describe("msg", () => {
it("msg", () => {
expect("something").toBeTrue()
})
it("msg", () => {
expect("something").toBeTrue()
})
})

test("something", () => {
expect("something").toBeTrue()
expect("something").toBeTrue()
})

Deno.test("something", () => {
expect("something").toBeTrue()
})
expect("something").toBeTrue()
})

await waitFor(() => {
expect(111).toBe(222);
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ expression: valid.js
# Input
```jsx
describe("msg", () => {
it("msg", () => {
expect("something").toBeTrue()
})
it("msg", () => {
expect("something").toBeTrue()
})
})

test("something", () => {
expect("something").toBeTrue()
expect("something").toBeTrue()
})

Deno.test("something", () => {
expect("something").toBeTrue()
expect("something").toBeTrue()
})

await waitFor(() => {
expect(111).toBe(222);
});

```
9 changes: 5 additions & 4 deletions crates/biome_js_syntax/src/expr_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1086,9 +1086,10 @@ impl AnyJsExpression {
}

/// Checks whether the current function call is:
/// - `it`
/// - `test`
/// - `Deno.test`
/// - `it`: many libraries such as Node.js, Mocha, Jest, etc.
/// - `test`: many libraries such as Node.js, bun, etc.
/// - [`Deno.test`](https://docs.deno.com/runtime/manual/basics/testing/)
/// - [`waitFor`](https://testing-library.com/docs/dom-testing-library/api-async/#waitfor)
pub fn contains_it_call(&self) -> bool {
let mut members = CalleeNamesIterator::new(self.clone());

Expand All @@ -1100,7 +1101,7 @@ impl AnyJsExpression {
let second = rev.next().map(|t| t.text());

match first {
Some("test" | "it") => true,
Some("test" | "it" | "waitFor") => true,
Some("Deno") => matches!(second, Some("test")),
_ => false,
}
Expand Down

0 comments on commit a3a1360

Please sign in to comment.