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