| 1 | # Suggest using `expect.assertions()` OR `expect.hasAssertions()` (`prefer-expect-assertions`)
|
|---|
| 2 |
|
|---|
| 3 | Ensure every test to have either `expect.assertions(<number of assertions>)` OR
|
|---|
| 4 | `expect.hasAssertions()` as its first expression.
|
|---|
| 5 |
|
|---|
| 6 | ## Rule details
|
|---|
| 7 |
|
|---|
| 8 | This rule triggers a warning if,
|
|---|
| 9 |
|
|---|
| 10 | - `expect.assertions(<number of assertions>)` OR `expect.hasAssertions()` is not
|
|---|
| 11 | present as first statement in a test, e.g.:
|
|---|
| 12 |
|
|---|
| 13 | ```js
|
|---|
| 14 | test('my test', () => {
|
|---|
| 15 | expect(someThing()).toEqual('foo');
|
|---|
| 16 | });
|
|---|
| 17 | ```
|
|---|
| 18 |
|
|---|
| 19 | - `expect.assertions(<number of assertions>)` is the first statement in a test
|
|---|
| 20 | where argument passed to `expect.assertions(<number of assertions>)` is not a
|
|---|
| 21 | valid number, e.g.:
|
|---|
| 22 |
|
|---|
| 23 | ```js
|
|---|
| 24 | test('my test', () => {
|
|---|
| 25 | expect.assertions('1');
|
|---|
| 26 | expect(someThing()).toEqual('foo');
|
|---|
| 27 | });
|
|---|
| 28 | ```
|
|---|
| 29 |
|
|---|
| 30 | ### Default configuration
|
|---|
| 31 |
|
|---|
| 32 | The following patterns are considered warnings:
|
|---|
| 33 |
|
|---|
| 34 | ```js
|
|---|
| 35 | test('my test', () => {
|
|---|
| 36 | expect.assertions('1');
|
|---|
| 37 | expect(someThing()).toEqual('foo');
|
|---|
| 38 | });
|
|---|
| 39 |
|
|---|
| 40 | test('my test', () => {
|
|---|
| 41 | expect(someThing()).toEqual('foo');
|
|---|
| 42 | });
|
|---|
| 43 | ```
|
|---|
| 44 |
|
|---|
| 45 | The following patterns would not be considered warnings:
|
|---|
| 46 |
|
|---|
| 47 | ```js
|
|---|
| 48 | test('my test', () => {
|
|---|
| 49 | expect.assertions(1);
|
|---|
| 50 | expect(someThing()).toEqual('foo');
|
|---|
| 51 | });
|
|---|
| 52 |
|
|---|
| 53 | test('my test', () => {
|
|---|
| 54 | expect.hasAssertions();
|
|---|
| 55 | expect(someThing()).toEqual('foo');
|
|---|
| 56 | });
|
|---|
| 57 | ```
|
|---|
| 58 |
|
|---|
| 59 | ## Options
|
|---|
| 60 |
|
|---|
| 61 | This rule can be configured to only check tests that match certain patterns that
|
|---|
| 62 | typically look like `expect` calls might be missed, such as in promises or
|
|---|
| 63 | loops.
|
|---|
| 64 |
|
|---|
| 65 | By default, none of these options are enabled meaning the rule checks _every_
|
|---|
| 66 | test for a call to either `expect.hasAssertions` or `expect.assertions`. If any
|
|---|
| 67 | of the options are enabled the rule checks any test that matches _at least one_
|
|---|
| 68 | of the patterns represented by the enabled options (think "OR" rather than
|
|---|
| 69 | "AND").
|
|---|
| 70 |
|
|---|
| 71 | #### `onlyFunctionsWithAsyncKeyword`
|
|---|
| 72 |
|
|---|
| 73 | When `true`, this rule will only warn for tests that use the `async` keyword.
|
|---|
| 74 |
|
|---|
| 75 | ```json
|
|---|
| 76 | {
|
|---|
| 77 | "rules": {
|
|---|
| 78 | "jest/prefer-expect-assertions": [
|
|---|
| 79 | "warn",
|
|---|
| 80 | { "onlyFunctionsWithAsyncKeyword": true }
|
|---|
| 81 | ]
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 | ```
|
|---|
| 85 |
|
|---|
| 86 | When `onlyFunctionsWithAsyncKeyword` option is set to `true`, the following
|
|---|
| 87 | pattern would be a warning:
|
|---|
| 88 |
|
|---|
| 89 | ```js
|
|---|
| 90 | test('my test', async () => {
|
|---|
| 91 | const result = await someAsyncFunc();
|
|---|
| 92 | expect(result).toBe('foo');
|
|---|
| 93 | });
|
|---|
| 94 | ```
|
|---|
| 95 |
|
|---|
| 96 | While the following patterns would not be considered warnings:
|
|---|
| 97 |
|
|---|
| 98 | ```js
|
|---|
| 99 | test('my test', () => {
|
|---|
| 100 | const result = someFunction();
|
|---|
| 101 | expect(result).toBe('foo');
|
|---|
| 102 | });
|
|---|
| 103 |
|
|---|
| 104 | test('my test', async () => {
|
|---|
| 105 | expect.assertions(1);
|
|---|
| 106 | const result = await someAsyncFunc();
|
|---|
| 107 | expect(result).toBe('foo');
|
|---|
| 108 | });
|
|---|
| 109 | ```
|
|---|
| 110 |
|
|---|
| 111 | #### `onlyFunctionsWithExpectInLoop`
|
|---|
| 112 |
|
|---|
| 113 | When `true`, this rule will only warn for tests that have `expect` calls within
|
|---|
| 114 | a native loop.
|
|---|
| 115 |
|
|---|
| 116 | ```json
|
|---|
| 117 | {
|
|---|
| 118 | "rules": {
|
|---|
| 119 | "jest/prefer-expect-assertions": [
|
|---|
| 120 | "warn",
|
|---|
| 121 | { "onlyFunctionsWithAsyncKeyword": true }
|
|---|
| 122 | ]
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 | ```
|
|---|
| 126 |
|
|---|
| 127 | Examples of **incorrect** code when `'onlyFunctionsWithExpectInLoop'` is `true`:
|
|---|
| 128 |
|
|---|
| 129 | ```js
|
|---|
| 130 | describe('getNumbers', () => {
|
|---|
| 131 | it('only returns numbers that are greater than zero', () => {
|
|---|
| 132 | const numbers = getNumbers();
|
|---|
| 133 |
|
|---|
| 134 | for (const number in numbers) {
|
|---|
| 135 | expect(number).toBeGreaterThan(0);
|
|---|
| 136 | }
|
|---|
| 137 | });
|
|---|
| 138 | });
|
|---|
| 139 | ```
|
|---|
| 140 |
|
|---|
| 141 | Examples of **correct** code when `'onlyFunctionsWithExpectInLoop'` is `true`:
|
|---|
| 142 |
|
|---|
| 143 | ```js
|
|---|
| 144 | describe('getNumbers', () => {
|
|---|
| 145 | it('only returns numbers that are greater than zero', () => {
|
|---|
| 146 | expect.hasAssertions();
|
|---|
| 147 |
|
|---|
| 148 | const numbers = getNumbers();
|
|---|
| 149 |
|
|---|
| 150 | for (const number in numbers) {
|
|---|
| 151 | expect(number).toBeGreaterThan(0);
|
|---|
| 152 | }
|
|---|
| 153 | });
|
|---|
| 154 |
|
|---|
| 155 | it('returns more than one number', () => {
|
|---|
| 156 | expect(getNumbers().length).toBeGreaterThan(1);
|
|---|
| 157 | });
|
|---|
| 158 | });
|
|---|
| 159 | ```
|
|---|
| 160 |
|
|---|
| 161 | #### `onlyFunctionsWithExpectInCallback`
|
|---|
| 162 |
|
|---|
| 163 | When `true`, this rule will only warn for tests that have `expect` calls within
|
|---|
| 164 | a callback.
|
|---|
| 165 |
|
|---|
| 166 | ```json
|
|---|
| 167 | {
|
|---|
| 168 | "rules": {
|
|---|
| 169 | "jest/prefer-expect-assertions": [
|
|---|
| 170 | "warn",
|
|---|
| 171 | { "onlyFunctionsWithExpectInCallback": true }
|
|---|
| 172 | ]
|
|---|
| 173 | }
|
|---|
| 174 | }
|
|---|
| 175 | ```
|
|---|
| 176 |
|
|---|
| 177 | Examples of **incorrect** code when `'onlyFunctionsWithExpectInCallback'` is
|
|---|
| 178 | `true`:
|
|---|
| 179 |
|
|---|
| 180 | ```js
|
|---|
| 181 | describe('getNumbers', () => {
|
|---|
| 182 | it('only returns numbers that are greater than zero', () => {
|
|---|
| 183 | const numbers = getNumbers();
|
|---|
| 184 |
|
|---|
| 185 | getNumbers().forEach(number => {
|
|---|
| 186 | expect(number).toBeGreaterThan(0);
|
|---|
| 187 | });
|
|---|
| 188 | });
|
|---|
| 189 | });
|
|---|
| 190 |
|
|---|
| 191 | describe('/users', () => {
|
|---|
| 192 | it.each([1, 2, 3])('returns ok', id => {
|
|---|
| 193 | client.get(`/users/${id}`, response => {
|
|---|
| 194 | expect(response.status).toBe(200);
|
|---|
| 195 | });
|
|---|
| 196 | });
|
|---|
| 197 | });
|
|---|
| 198 | ```
|
|---|
| 199 |
|
|---|
| 200 | Examples of **correct** code when `'onlyFunctionsWithExpectInCallback'` is
|
|---|
| 201 | `true`:
|
|---|
| 202 |
|
|---|
| 203 | ```js
|
|---|
| 204 | describe('getNumbers', () => {
|
|---|
| 205 | it('only returns numbers that are greater than zero', () => {
|
|---|
| 206 | expect.hasAssertions();
|
|---|
| 207 |
|
|---|
| 208 | const numbers = getNumbers();
|
|---|
| 209 |
|
|---|
| 210 | getNumbers().forEach(number => {
|
|---|
| 211 | expect(number).toBeGreaterThan(0);
|
|---|
| 212 | });
|
|---|
| 213 | });
|
|---|
| 214 | });
|
|---|
| 215 |
|
|---|
| 216 | describe('/users', () => {
|
|---|
| 217 | it.each([1, 2, 3])('returns ok', id => {
|
|---|
| 218 | expect.assertions(3);
|
|---|
| 219 |
|
|---|
| 220 | client.get(`/users/${id}`, response => {
|
|---|
| 221 | expect(response.status).toBe(200);
|
|---|
| 222 | });
|
|---|
| 223 | });
|
|---|
| 224 | });
|
|---|
| 225 | ```
|
|---|