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