Skip to content

Commit 858a1a8

Browse files
committed
docs(prefer-to-be): update with info about advanced toBe* matchers
1 parent 8201c23 commit 858a1a8

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

docs/rules/prefer-to-be.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ When asserting against primitive literals such as numbers and strings, the
44
equality matchers all operate the same, but read slightly differently in code.
55

66
This rule recommends using the `toBe` matcher in these situations, as it forms
7-
the most grammatically natural sentence.
7+
the most grammatically natural sentence. For `null`, `undefined`, and `NaN` this
8+
rule recommends using their specific `toBe` matchers, as they give better error
9+
messages as well.
810

911
## Rule details
1012

@@ -28,3 +30,23 @@ expect(loadMessage()).resolves.toBe('hello world');
2830

2931
expect(catchError()).toStrictEqual({ message: 'oh noes!' });
3032
```
33+
34+
For `null`, `undefined`, and `NaN`, this rule triggers a warning if `toBe` is
35+
used to assert against those literal values instead of their more specific
36+
`toBe` counterparts:
37+
38+
```js
39+
expect(value).not.toBe(undefined);
40+
expect(getMessage()).toBe(null);
41+
expect(countMessages()).resolves.not.toBe(NaN);
42+
```
43+
44+
The following pattern is not warning:
45+
46+
```js
47+
expect(value).toBeDefined();
48+
expect(getMessage()).toBeNull();
49+
expect(countMessages()).resolves.not.toBeNaN();
50+
51+
expect(catchError()).toStrictEqual({ message: undefined });
52+
```

0 commit comments

Comments
 (0)