source: frontend/node_modules/eslint-plugin-jest/docs/rules/prefer-expect-resolves.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 1.4 KB
Line 
1# Prefer `await expect(...).resolves` over `expect(await ...)` syntax (`prefer-expect-resolves`)
2
3When working with promises, there are two primary ways you can test the resolved
4value:
5
61. use the `resolve` modifier on `expect`
7 (`await expect(...).resolves.<matcher>` style)
82. `await` the promise and assert against its result
9 (`expect(await ...).<matcher>` style)
10
11While the second style is arguably less dependent on `jest`, if the promise
12rejects it will be treated as a general error, resulting in less predictable
13behaviour and output from `jest`.
14
15Additionally, favoring the first style ensures consistency with its `rejects`
16counterpart, as there is no way of "awaiting" a rejection.
17
18## Rule details
19
20This rule triggers a warning if an `await` is done within an `expect`, and
21recommends using `resolves` instead.
22
23Examples of **incorrect** code for this rule
24
25```js
26it('passes', async () => {
27 expect(await someValue()).toBe(true);
28});
29
30it('is true', async () => {
31 const myPromise = Promise.resolve(true);
32
33 expect(await myPromise).toBe(true);
34});
35```
36
37Examples of **correct** code for this rule
38
39```js
40it('passes', async () => {
41 await expect(someValue()).resolves.toBe(true);
42});
43
44it('is true', async () => {
45 const myPromise = Promise.resolve(true);
46
47 await expect(myPromise).resolves.toBe(true);
48});
49
50it('errors', async () => {
51 await expect(Promise.rejects('oh noes!')).rejects.toThrow('oh noes!');
52});
53```
Note: See TracBrowser for help on using the repository browser.