source: frontend/node_modules/eslint-plugin-jest/docs/rules/valid-expect.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: 3.6 KB
Line 
1# Enforce valid `expect()` usage (`valid-expect`)
2
3Ensure `expect()` is called with a single argument and there is an actual
4expectation made.
5
6## Rule details
7
8This rule triggers a warning if `expect()` is called with more than one argument
9or without arguments. It would also issue a warning if there is nothing called
10on `expect()`, e.g.:
11
12```js
13expect();
14expect('something');
15```
16
17or when a matcher function was not called, e.g.:
18
19```js
20expect(true).toBeDefined;
21```
22
23or when an async assertion was not `await`ed or returned, e.g.:
24
25```js
26expect(Promise.resolve('Hi!')).resolves.toBe('Hi!');
27```
28
29This rule is enabled by default.
30
31## Options
32
33```json5
34{
35 type: 'object',
36 properties: {
37 alwaysAwait: {
38 type: 'boolean',
39 default: false,
40 },
41 asyncMatchers: {
42 type: 'array',
43 items: { type: 'string' },
44 default: ['toResolve', 'toReject'],
45 },
46 minArgs: {
47 type: 'number',
48 minimum: 1,
49 },
50 maxArgs: {
51 type: 'number',
52 minimum: 1,
53 },
54 },
55 additionalProperties: false,
56}
57```
58
59### `alwaysAwait`
60
61Enforces to use `await` inside block statements. Using `return` will trigger a
62warning. Returning one line statements with arrow functions is _always allowed_.
63
64Examples of **incorrect** code for the { "alwaysAwait": **true** } option:
65
66```js
67// alwaysAwait: true
68test('test1', async () => {
69 await expect(Promise.resolve(2)).resolves.toBeDefined();
70 return expect(Promise.resolve(1)).resolves.toBe(1); // `return` statement will trigger a warning
71});
72```
73
74Examples of **correct** code for the { "alwaysAwait": **true** } option:
75
76```js
77// alwaysAwait: true
78test('test1', async () => {
79 await expect(Promise.resolve(2)).resolves.toBeDefined();
80 await expect(Promise.resolve(1)).resolves.toBe(1);
81});
82
83test('test2', () => expect(Promise.resolve(2)).resolves.toBe(2));
84```
85
86### `asyncMatchers`
87
88Allows specifying which matchers return promises, and so should be considered
89async when checking if an `expect` should be returned or awaited.
90
91By default, this has a list of all the async matchers provided by
92`jest-extended` (namely, `toResolve` and `toReject`).
93
94### `minArgs` & `maxArgs`
95
96Enforces the minimum and maximum number of arguments that `expect` can take, and
97is required to take.
98
99Both of these properties have a default value of `1`, which is the number of
100arguments supported by vanilla `expect`.
101
102This is useful when you're using libraries that increase the number of arguments
103supported by `expect`, such as
104[`jest-expect-message`](https://www.npmjs.com/package/jest-expect-message).
105
106### Default configuration
107
108The following patterns are considered warnings:
109
110```js
111test('all the things', async () => {
112 expect();
113 expect().toEqual('something');
114 expect('something', 'else');
115 expect('something');
116 await expect('something');
117 expect(true).toBeDefined;
118 expect(Promise.resolve('hello')).resolves;
119 expect(Promise.resolve('hello')).resolves.toEqual('hello');
120 Promise.resolve(expect(Promise.resolve('hello')).resolves.toEqual('hello'));
121 Promise.all([
122 expect(Promise.resolve('hello')).resolves.toEqual('hello'),
123 expect(Promise.resolve('hi')).resolves.toEqual('hi'),
124 ]);
125});
126```
127
128The following patterns are not warnings:
129
130```js
131test('all the things', async () => {
132 expect('something').toEqual('something');
133 expect([1, 2, 3]).toEqual([1, 2, 3]);
134 expect(true).toBeDefined();
135 await expect(Promise.resolve('hello')).resolves.toEqual('hello');
136 await Promise.resolve(
137 expect(Promise.resolve('hello')).resolves.toEqual('hello'),
138 );
139 await Promise.all(
140 expect(Promise.resolve('hello')).resolves.toEqual('hello'),
141 expect(Promise.resolve('hi')).resolves.toEqual('hi'),
142 );
143});
144```
Note: See TracBrowser for help on using the repository browser.