| 1 | # Enforce valid `expect()` usage (`valid-expect`)
|
|---|
| 2 |
|
|---|
| 3 | Ensure `expect()` is called with a single argument and there is an actual
|
|---|
| 4 | expectation made.
|
|---|
| 5 |
|
|---|
| 6 | ## Rule details
|
|---|
| 7 |
|
|---|
| 8 | This rule triggers a warning if `expect()` is called with more than one argument
|
|---|
| 9 | or without arguments. It would also issue a warning if there is nothing called
|
|---|
| 10 | on `expect()`, e.g.:
|
|---|
| 11 |
|
|---|
| 12 | ```js
|
|---|
| 13 | expect();
|
|---|
| 14 | expect('something');
|
|---|
| 15 | ```
|
|---|
| 16 |
|
|---|
| 17 | or when a matcher function was not called, e.g.:
|
|---|
| 18 |
|
|---|
| 19 | ```js
|
|---|
| 20 | expect(true).toBeDefined;
|
|---|
| 21 | ```
|
|---|
| 22 |
|
|---|
| 23 | or when an async assertion was not `await`ed or returned, e.g.:
|
|---|
| 24 |
|
|---|
| 25 | ```js
|
|---|
| 26 | expect(Promise.resolve('Hi!')).resolves.toBe('Hi!');
|
|---|
| 27 | ```
|
|---|
| 28 |
|
|---|
| 29 | This 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 |
|
|---|
| 61 | Enforces to use `await` inside block statements. Using `return` will trigger a
|
|---|
| 62 | warning. Returning one line statements with arrow functions is _always allowed_.
|
|---|
| 63 |
|
|---|
| 64 | Examples of **incorrect** code for the { "alwaysAwait": **true** } option:
|
|---|
| 65 |
|
|---|
| 66 | ```js
|
|---|
| 67 | // alwaysAwait: true
|
|---|
| 68 | test('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 |
|
|---|
| 74 | Examples of **correct** code for the { "alwaysAwait": **true** } option:
|
|---|
| 75 |
|
|---|
| 76 | ```js
|
|---|
| 77 | // alwaysAwait: true
|
|---|
| 78 | test('test1', async () => {
|
|---|
| 79 | await expect(Promise.resolve(2)).resolves.toBeDefined();
|
|---|
| 80 | await expect(Promise.resolve(1)).resolves.toBe(1);
|
|---|
| 81 | });
|
|---|
| 82 |
|
|---|
| 83 | test('test2', () => expect(Promise.resolve(2)).resolves.toBe(2));
|
|---|
| 84 | ```
|
|---|
| 85 |
|
|---|
| 86 | ### `asyncMatchers`
|
|---|
| 87 |
|
|---|
| 88 | Allows specifying which matchers return promises, and so should be considered
|
|---|
| 89 | async when checking if an `expect` should be returned or awaited.
|
|---|
| 90 |
|
|---|
| 91 | By default, this has a list of all the async matchers provided by
|
|---|
| 92 | `jest-extended` (namely, `toResolve` and `toReject`).
|
|---|
| 93 |
|
|---|
| 94 | ### `minArgs` & `maxArgs`
|
|---|
| 95 |
|
|---|
| 96 | Enforces the minimum and maximum number of arguments that `expect` can take, and
|
|---|
| 97 | is required to take.
|
|---|
| 98 |
|
|---|
| 99 | Both of these properties have a default value of `1`, which is the number of
|
|---|
| 100 | arguments supported by vanilla `expect`.
|
|---|
| 101 |
|
|---|
| 102 | This is useful when you're using libraries that increase the number of arguments
|
|---|
| 103 | supported by `expect`, such as
|
|---|
| 104 | [`jest-expect-message`](https://www.npmjs.com/package/jest-expect-message).
|
|---|
| 105 |
|
|---|
| 106 | ### Default configuration
|
|---|
| 107 |
|
|---|
| 108 | The following patterns are considered warnings:
|
|---|
| 109 |
|
|---|
| 110 | ```js
|
|---|
| 111 | test('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 |
|
|---|
| 128 | The following patterns are not warnings:
|
|---|
| 129 |
|
|---|
| 130 | ```js
|
|---|
| 131 | test('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 | ```
|
|---|