2020-08-25 23:57:08 +00:00
|
|
|
# Disallow specific matchers & modifiers (`no-restricted-matchers`)
|
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
<!-- end auto-generated rule header -->
|
|
|
|
|
|
|
|
You may want to ban specific matchers & modifiers from being used.
|
|
|
|
|
|
|
|
## Rule details
|
|
|
|
|
2020-08-25 23:57:08 +00:00
|
|
|
This rule bans specific matchers & modifiers from being used, and can suggest
|
|
|
|
alternatives.
|
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
## Options
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
Bans are expressed in the form of a map, with the value being either a string
|
|
|
|
message to be shown, or `null` if the default rule message should be used.
|
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
Bans are checked against the start of the `expect` chain - this means that to
|
|
|
|
ban a specific matcher entirely you must specify all six permutations, but
|
|
|
|
allows you to ban modifiers as well.
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
By default, this map is empty, meaning no matchers or modifiers are banned.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"jest/no-restricted-matchers": [
|
|
|
|
"error",
|
|
|
|
{
|
|
|
|
"toBeFalsy": null,
|
|
|
|
"resolves": "Use `expect(await promise)` instead.",
|
2022-11-10 10:43:16 +00:00
|
|
|
"toHaveBeenCalledWith": null,
|
|
|
|
"not.toHaveBeenCalledWith": null,
|
|
|
|
"resolves.toHaveBeenCalledWith": null,
|
|
|
|
"rejects.toHaveBeenCalledWith": null,
|
|
|
|
"resolves.not.toHaveBeenCalledWith": null,
|
|
|
|
"rejects.not.toHaveBeenCalledWith": null
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Examples of **incorrect** code for this rule with the above configuration
|
|
|
|
|
|
|
|
```js
|
|
|
|
it('is false', () => {
|
2022-11-10 10:43:16 +00:00
|
|
|
// if this has a modifer (i.e. `not.toBeFalsy`), it would be considered fine
|
2020-08-25 23:57:08 +00:00
|
|
|
expect(a).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('resolves', async () => {
|
2022-11-10 10:43:16 +00:00
|
|
|
// all uses of this modifier are disallowed, regardless of matcher
|
2020-08-25 23:57:08 +00:00
|
|
|
await expect(myPromise()).resolves.toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when an error happens', () => {
|
|
|
|
it('does not upload the file', async () => {
|
2022-11-10 10:43:16 +00:00
|
|
|
// all uses of this matcher are disallowed
|
2020-08-25 23:57:08 +00:00
|
|
|
expect(uploadFileMock).not.toHaveBeenCalledWith('file.name');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
```
|