source: frontend/node_modules/eslint-plugin-jest/docs/rules/prefer-to-be.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.6 KB
Line 
1# Suggest using `toBe()` for primitive literals (`prefer-to-be`)
2
3When asserting against primitive literals such as numbers and strings, the
4equality matchers all operate the same, but read slightly differently in code.
5
6This rule recommends using the `toBe` matcher in these situations, as it forms
7the most grammatically natural sentence. For `null`, `undefined`, and `NaN` this
8rule recommends using their specific `toBe` matchers, as they give better error
9messages as well.
10
11## Rule details
12
13This rule triggers a warning if `toEqual()` or `toStrictEqual()` are used to
14assert a primitive literal value such as numbers, strings, and booleans.
15
16The following patterns are considered warnings:
17
18```js
19expect(value).not.toEqual(5);
20expect(getMessage()).toStrictEqual('hello world');
21expect(loadMessage()).resolves.toEqual('hello world');
22```
23
24The following pattern is not warning:
25
26```js
27expect(value).not.toBe(5);
28expect(getMessage()).toBe('hello world');
29expect(loadMessage()).resolves.toBe('hello world');
30expect(didError).not.toBe(true);
31
32expect(catchError()).toStrictEqual({ message: 'oh noes!' });
33```
34
35For `null`, `undefined`, and `NaN`, this rule triggers a warning if `toBe` is
36used to assert against those literal values instead of their more specific
37`toBe` counterparts:
38
39```js
40expect(value).not.toBe(undefined);
41expect(getMessage()).toBe(null);
42expect(countMessages()).resolves.not.toBe(NaN);
43```
44
45The following pattern is not warning:
46
47```js
48expect(value).toBeDefined();
49expect(getMessage()).toBeNull();
50expect(countMessages()).resolves.not.toBeNaN();
51
52expect(catchError()).toStrictEqual({ message: undefined });
53```
Note: See TracBrowser for help on using the repository browser.