source: frontend/node_modules/eslint-plugin-jest/docs/rules/valid-title.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: 5.7 KB
Line 
1# Enforce valid titles (`valid-title`)
2
3Checks that the title of Jest blocks are valid by ensuring that titles are:
4
5- not empty,
6- is a string,
7- not prefixed with their block name,
8- have no leading or trailing spaces
9
10## Rule Details
11
12**emptyTitle**
13
14An empty title is not informative, and serves little purpose.
15
16Examples of **incorrect** code for this rule:
17
18```js
19describe('', () => {});
20describe('foo', () => {
21 it('', () => {});
22});
23it('', () => {});
24test('', () => {});
25xdescribe('', () => {});
26xit('', () => {});
27xtest('', () => {});
28```
29
30Examples of **correct** code for this rule:
31
32```js
33describe('foo', () => {});
34describe('foo', () => {
35 it('bar', () => {});
36});
37test('foo', () => {});
38it('foo', () => {});
39xdescribe('foo', () => {});
40xit('foo', () => {});
41xtest('foo', () => {});
42```
43
44**titleMustBeString**
45
46Titles for test blocks should always be a string.
47
48This is also applied to `describe` blocks by default, but can be turned off via
49the `ignoreTypeOfDescribeName` option:
50
51Examples of **incorrect** code for this rule:
52
53```js
54it(123, () => {});
55describe(String(/.+/), () => {});
56describe(myFunction, () => {});
57xdescribe(myFunction, () => {});
58describe(6, function () {});
59```
60
61Examples of **correct** code for this rule:
62
63```js
64it('is a string', () => {});
65test('is a string', () => {});
66xtest('is a string', () => {});
67describe('is a string', () => {});
68describe.skip('is a string', () => {});
69fdescribe('is a string', () => {});
70```
71
72Examples of **correct** code when `ignoreTypeOfDescribeName` is `true`:
73
74```js
75it('is a string', () => {});
76test('is a string', () => {});
77xtest('is a string', () => {});
78describe('is a string', () => {});
79describe.skip('is a string', () => {});
80fdescribe('is a string', () => {});
81
82describe(String(/.+/), () => {});
83describe(myFunction, () => {});
84xdescribe(myFunction, () => {});
85describe(6, function () {});
86```
87
88**duplicatePrefix**
89
90A `describe` / `test` block should not start with `duplicatePrefix`
91
92Examples of **incorrect** code for this rule
93
94```js
95test('test foo', () => {});
96it('it foo', () => {});
97
98describe('foo', () => {
99 test('test bar', () => {});
100});
101
102describe('describe foo', () => {
103 test('bar', () => {});
104});
105```
106
107Examples of **correct** code for this rule
108
109```js
110test('foo', () => {});
111it('foo', () => {});
112
113describe('foo', () => {
114 test('bar', () => {});
115});
116```
117
118**accidentalSpace**
119
120A `describe` / `test` block should not contain accidentalSpace
121
122Examples of **incorrect** code for this rule
123
124```js
125test(' foo', () => {});
126it(' foo', () => {});
127
128describe('foo', () => {
129 test(' bar', () => {});
130});
131
132describe(' foo', () => {
133 test('bar', () => {});
134});
135
136describe('foo ', () => {
137 test('bar', () => {});
138});
139```
140
141Examples of **correct** code for this rule
142
143```js
144test('foo', () => {});
145it('foo', () => {});
146
147describe('foo', () => {
148 test('bar', () => {});
149});
150```
151
152## Options
153
154```ts
155interface Options {
156 ignoreTypeOfDescribeName?: boolean;
157 disallowedWords?: string[];
158 mustNotMatch?: Partial<Record<'describe' | 'test' | 'it', string>> | string;
159 mustMatch?: Partial<Record<'describe' | 'test' | 'it', string>> | string;
160}
161```
162
163#### `ignoreTypeOfDescribeName`
164
165Default: `false`
166
167When enabled, the type of the first argument to `describe` blocks won't be
168checked.
169
170#### `disallowedWords`
171
172Default: `[]`
173
174A string array of words that are not allowed to be used in test titles. Matching
175is not case-sensitive, and looks for complete words:
176
177Examples of **incorrect** code when using `disallowedWords`:
178
179```js
180// with disallowedWords: ['correct', 'all', 'every', 'properly']
181describe('the correct way to do things', () => {});
182it('has ALL the things', () => {});
183xdescribe('every single one of them', () => {});
184test(`that the value is set properly`, () => {});
185```
186
187Examples of **correct** code when using `disallowedWords`:
188
189```js
190// with disallowedWords: ['correct', 'all', 'every', 'properly']
191it('correctly sets the value', () => {});
192test('that everything is as it should be', () => {});
193describe('the proper way to handle things', () => {});
194```
195
196#### `mustMatch` & `mustNotMatch`
197
198Defaults: `{}`
199
200Allows enforcing that titles must match or must not match a given Regular
201Expression, with an optional message. An object can be provided to apply
202different Regular Expressions (with optional messages) to specific Jest test
203function groups (`describe`, `test`, and `it`).
204
205Examples of **incorrect** code when using `mustMatch`:
206
207```js
208// with mustMatch: '^that'
209describe('the correct way to do things', () => {});
210fit('this there!', () => {});
211
212// with mustMatch: { test: '^that' }
213describe('the tests that will be run', () => {});
214test('the stuff works', () => {});
215xtest('errors that are thrown have messages', () => {});
216```
217
218Examples of **correct** code when using `mustMatch`:
219
220```js
221// with mustMatch: '^that'
222describe('that thing that needs to be done', () => {});
223fit('that this there!', () => {});
224
225// with mustMatch: { test: '^that' }
226describe('the tests that will be run', () => {});
227test('that the stuff works', () => {});
228xtest('that errors that thrown have messages', () => {});
229```
230
231Optionally you can provide a custom message to show for a particular matcher by
232using a tuple at any level where you can provide a matcher:
233
234```js
235const prefixes = ['when', 'with', 'without', 'if', 'unless', 'for'];
236const prefixesList = prefixes.join(' - \n');
237
238module.exports = {
239 rules: {
240 'jest/valid-title': [
241 'error',
242 {
243 mustNotMatch: ['\\.$', 'Titles should not end with a full-stop'],
244 mustMatch: {
245 describe: [
246 new RegExp(`^(?:[A-Z]|\\b(${prefixes.join('|')})\\b`, 'u').source,
247 `Describe titles should either start with a capital letter or one of the following prefixes: ${prefixesList}`,
248 ],
249 test: [/[^A-Z]/u.source],
250 it: /[^A-Z]/u.source,
251 },
252 },
253 ],
254 },
255};
256```
Note: See TracBrowser for help on using the repository browser.