source: frontend/node_modules/eslint-plugin-jest/docs/rules/expect-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.5 KB
RevLine 
[9af201e]1# Enforce assertion to be made in a test body (`expect-expect`)
2
3Ensure that there is at least one `expect` call made in a test.
4
5## Rule details
6
7This rule triggers when there is no call made to `expect` in a test, to prevent
8users from forgetting to add assertions.
9
10Examples of **incorrect** code for this rule:
11
12```js
13it('should be a test', () => {
14 console.log('no assertion');
15});
16test('should assert something', () => {});
17```
18
19Examples of **correct** code for this rule:
20
21```js
22it('should be a test', () => {
23 expect(true).toBeDefined();
24});
25it('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
46This array option specifies the names of functions that should be considered to
47be asserting functions. Function names can use wildcards i.e `request.*.expect`,
48`request.**.expect`, `request.*.expect*`
49
50Examples of **incorrect** code for the `{ "assertFunctionNames": ["expect"] }`
51option:
52
53```js
54/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect"] }] */
55
56import { expectSaga } from 'redux-saga-test-plan';
57import { addSaga } from '../src/sagas';
58
59test('returns sum', () => {
60 expectSaga(addSaga, 1, 1).returns(2).run();
61});
62```
63
64Examples of **correct** code for the
65`{ "assertFunctionNames": ["expect", "expectSaga"] }` option:
66
67```js
68/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect", "expectSaga"] }] */
69
70import { expectSaga } from 'redux-saga-test-plan';
71import { addSaga } from '../src/sagas';
72
73test('returns sum', () => {
74 expectSaga(addSaga, 1, 1).returns(2).run();
75});
76```
77
78Since the string is compiled into a regular expression, you'll need to escape
79special characters such as `$` with a double backslash:
80
81```js
82/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect\\$"] }] */
83
84it('is money-like', () => {
85 expect$(1.0);
86});
87```
88
89Examples 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"] }] */
95const request = require('supertest');
96const express = require('express');
97
98const app = express();
99
100describe('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
109This array can be used to specify the names of functions that should also be
110treated as test blocks:
111
112```json
113{
114 "rules": {
115 "jest/expect-expect": [
116 "error",
117 { "additionalTestBlockFunctions": ["theoretically"] }
118 ]
119 }
120}
121```
122
123The following is _correct_ when using the above configuration:
124
125```js
126import theoretically from 'jest-theories';
127
128describe('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```
Note: See TracBrowser for help on using the repository browser.