| 1 | # Enforce assertion to be made in a test body (`expect-expect`)
|
|---|
| 2 |
|
|---|
| 3 | Ensure that there is at least one `expect` call made in a test.
|
|---|
| 4 |
|
|---|
| 5 | ## Rule details
|
|---|
| 6 |
|
|---|
| 7 | This rule triggers when there is no call made to `expect` in a test, to prevent
|
|---|
| 8 | users from forgetting to add assertions.
|
|---|
| 9 |
|
|---|
| 10 | Examples of **incorrect** code for this rule:
|
|---|
| 11 |
|
|---|
| 12 | ```js
|
|---|
| 13 | it('should be a test', () => {
|
|---|
| 14 | console.log('no assertion');
|
|---|
| 15 | });
|
|---|
| 16 | test('should assert something', () => {});
|
|---|
| 17 | ```
|
|---|
| 18 |
|
|---|
| 19 | Examples of **correct** code for this rule:
|
|---|
| 20 |
|
|---|
| 21 | ```js
|
|---|
| 22 | it('should be a test', () => {
|
|---|
| 23 | expect(true).toBeDefined();
|
|---|
| 24 | });
|
|---|
| 25 | it('should work with callbacks/async', () => {
|
|---|
| 26 | somePromise().then(res => expect(res).toBe('passed'));
|
|---|
| 27 | });
|
|---|
| 28 | ```
|
|---|
| 29 |
|
|---|
| 30 | ## Options
|
|---|
| 31 |
|
|---|
| 32 | ```json
|
|---|
| 33 | {
|
|---|
| 34 | "jest/expect-expect": [
|
|---|
| 35 | "error",
|
|---|
| 36 | {
|
|---|
| 37 | "assertFunctionNames": ["expect"],
|
|---|
| 38 | "additionalTestBlockFunctions": []
|
|---|
| 39 | }
|
|---|
| 40 | ]
|
|---|
| 41 | }
|
|---|
| 42 | ```
|
|---|
| 43 |
|
|---|
| 44 | ### `assertFunctionNames`
|
|---|
| 45 |
|
|---|
| 46 | This array option specifies the names of functions that should be considered to
|
|---|
| 47 | be asserting functions. Function names can use wildcards i.e `request.*.expect`,
|
|---|
| 48 | `request.**.expect`, `request.*.expect*`
|
|---|
| 49 |
|
|---|
| 50 | Examples of **incorrect** code for the `{ "assertFunctionNames": ["expect"] }`
|
|---|
| 51 | option:
|
|---|
| 52 |
|
|---|
| 53 | ```js
|
|---|
| 54 | /* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect"] }] */
|
|---|
| 55 |
|
|---|
| 56 | import { expectSaga } from 'redux-saga-test-plan';
|
|---|
| 57 | import { addSaga } from '../src/sagas';
|
|---|
| 58 |
|
|---|
| 59 | test('returns sum', () => {
|
|---|
| 60 | expectSaga(addSaga, 1, 1).returns(2).run();
|
|---|
| 61 | });
|
|---|
| 62 | ```
|
|---|
| 63 |
|
|---|
| 64 | Examples of **correct** code for the
|
|---|
| 65 | `{ "assertFunctionNames": ["expect", "expectSaga"] }` option:
|
|---|
| 66 |
|
|---|
| 67 | ```js
|
|---|
| 68 | /* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect", "expectSaga"] }] */
|
|---|
| 69 |
|
|---|
| 70 | import { expectSaga } from 'redux-saga-test-plan';
|
|---|
| 71 | import { addSaga } from '../src/sagas';
|
|---|
| 72 |
|
|---|
| 73 | test('returns sum', () => {
|
|---|
| 74 | expectSaga(addSaga, 1, 1).returns(2).run();
|
|---|
| 75 | });
|
|---|
| 76 | ```
|
|---|
| 77 |
|
|---|
| 78 | Since the string is compiled into a regular expression, you'll need to escape
|
|---|
| 79 | special characters such as `$` with a double backslash:
|
|---|
| 80 |
|
|---|
| 81 | ```js
|
|---|
| 82 | /* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect\\$"] }] */
|
|---|
| 83 |
|
|---|
| 84 | it('is money-like', () => {
|
|---|
| 85 | expect$(1.0);
|
|---|
| 86 | });
|
|---|
| 87 | ```
|
|---|
| 88 |
|
|---|
| 89 | Examples of **correct** code for working with the HTTP assertions library
|
|---|
| 90 | [SuperTest](https://www.npmjs.com/package/supertest) with the
|
|---|
| 91 | `{ "assertFunctionNames": ["expect", "request.**.expect"] }` option:
|
|---|
| 92 |
|
|---|
| 93 | ```js
|
|---|
| 94 | /* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect", "request.**.expect"] }] */
|
|---|
| 95 | const request = require('supertest');
|
|---|
| 96 | const express = require('express');
|
|---|
| 97 |
|
|---|
| 98 | const app = express();
|
|---|
| 99 |
|
|---|
| 100 | describe('GET /user', function () {
|
|---|
| 101 | it('responds with json', function (done) {
|
|---|
| 102 | request(app).get('/user').expect('Content-Type', /json/).expect(200, done);
|
|---|
| 103 | });
|
|---|
| 104 | });
|
|---|
| 105 | ```
|
|---|
| 106 |
|
|---|
| 107 | ### `additionalTestBlockFunctions`
|
|---|
| 108 |
|
|---|
| 109 | This array can be used to specify the names of functions that should also be
|
|---|
| 110 | treated as test blocks:
|
|---|
| 111 |
|
|---|
| 112 | ```json
|
|---|
| 113 | {
|
|---|
| 114 | "rules": {
|
|---|
| 115 | "jest/expect-expect": [
|
|---|
| 116 | "error",
|
|---|
| 117 | { "additionalTestBlockFunctions": ["theoretically"] }
|
|---|
| 118 | ]
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|
| 121 | ```
|
|---|
| 122 |
|
|---|
| 123 | The following is _correct_ when using the above configuration:
|
|---|
| 124 |
|
|---|
| 125 | ```js
|
|---|
| 126 | import theoretically from 'jest-theories';
|
|---|
| 127 |
|
|---|
| 128 | describe('NumberToLongString', () => {
|
|---|
| 129 | const theories = [
|
|---|
| 130 | { input: 100, expected: 'One hundred' },
|
|---|
| 131 | { input: 1000, expected: 'One thousand' },
|
|---|
| 132 | { input: 10000, expected: 'Ten thousand' },
|
|---|
| 133 | { input: 100000, expected: 'One hundred thousand' },
|
|---|
| 134 | ];
|
|---|
| 135 |
|
|---|
| 136 | theoretically(
|
|---|
| 137 | 'the number {input} is correctly translated to string',
|
|---|
| 138 | theories,
|
|---|
| 139 | theory => {
|
|---|
| 140 | const output = NumberToLongString(theory.input);
|
|---|
| 141 | expect(output).toBe(theory.expected);
|
|---|
| 142 | },
|
|---|
| 143 | );
|
|---|
| 144 | });
|
|---|
| 145 | ```
|
|---|