-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
2,245 additions
and
800 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Prefer `await expect(...).resolves` over `expect(await ...)` syntax (`prefer-expect-resolves`) | ||
|
||
When working with promises, there are two primary ways you can test the resolved | ||
value: | ||
|
||
1. use the `resolve` modifier on `expect` | ||
(`await expect(...).resolves.<matcher>` style) | ||
2. `await` the promise and assert against its result | ||
(`expect(await ...).<matcher>` style) | ||
|
||
While the second style is arguably less dependent on `jest`, if the promise | ||
rejects it will be treated as a general error, resulting in less predictable | ||
behaviour and output from `jest`. | ||
|
||
Additionally, favoring the first style ensures consistency with its `rejects` | ||
counterpart, as there is no way of "awaiting" a rejection. | ||
|
||
## Rule details | ||
|
||
This rule triggers a warning if an `await` is done within an `expect`, and | ||
recommends using `resolves` instead. | ||
|
||
Examples of **incorrect** code for this rule | ||
|
||
```js | ||
it('passes', async () => { | ||
expect(await someValue()).toBe(true); | ||
}); | ||
|
||
it('is true', async () => { | ||
const myPromise = Promise.resolve(true); | ||
|
||
expect(await myPromise).toBe(true); | ||
}); | ||
``` | ||
|
||
Examples of **correct** code for this rule | ||
|
||
```js | ||
it('passes', async () => { | ||
await expect(someValue()).resolves.toBe(true); | ||
}); | ||
|
||
it('is true', async () => { | ||
const myPromise = Promise.resolve(true); | ||
|
||
await expect(myPromise).resolves.toBe(true); | ||
}); | ||
|
||
it('errors', async () => { | ||
await expect(Promise.rejects('oh noes!')).rejects.toThrow('oh noes!'); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Suggest using `toBe()` for primitive literals (`prefer-to-be`) | ||
|
||
When asserting against primitive literals such as numbers and strings, the | ||
equality matchers all operate the same, but read slightly differently in code. | ||
|
||
This rule recommends using the `toBe` matcher in these situations, as it forms | ||
the most grammatically natural sentence. For `null`, `undefined`, and `NaN` this | ||
rule recommends using their specific `toBe` matchers, as they give better error | ||
messages as well. | ||
|
||
## Rule details | ||
|
||
This rule triggers a warning if `toEqual()` or `toStrictEqual()` are used to | ||
assert a primitive literal value such as numbers, strings, and booleans. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
expect(value).not.toEqual(5); | ||
expect(getMessage()).toStrictEqual('hello world'); | ||
expect(loadMessage()).resolves.toEqual('hello world'); | ||
``` | ||
|
||
The following pattern is not warning: | ||
|
||
```js | ||
expect(value).not.toBe(5); | ||
expect(getMessage()).toBe('hello world'); | ||
expect(loadMessage()).resolves.toBe('hello world'); | ||
expect(didError).not.toBe(true); | ||
|
||
expect(catchError()).toStrictEqual({ message: 'oh noes!' }); | ||
``` | ||
|
||
For `null`, `undefined`, and `NaN`, this rule triggers a warning if `toBe` is | ||
used to assert against those literal values instead of their more specific | ||
`toBe` counterparts: | ||
|
||
```js | ||
expect(value).not.toBe(undefined); | ||
expect(getMessage()).toBe(null); | ||
expect(countMessages()).resolves.not.toBe(NaN); | ||
``` | ||
|
||
The following pattern is not warning: | ||
|
||
```js | ||
expect(value).toBeDefined(); | ||
expect(getMessage()).toBeNull(); | ||
expect(countMessages()).resolves.not.toBeNaN(); | ||
|
||
expect(catchError()).toStrictEqual({ message: undefined }); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.