-
-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathvalidatePrTitle.test.js
348 lines (300 loc) · 12.2 KB
/
validatePrTitle.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
const validatePrTitle = require('./validatePrTitle');
it('allows valid PR titles that use the default types', async () => {
const inputs = [
'fix: Fix bug',
'fix!: Fix bug',
'feat: Add feature',
'feat!: Add feature',
'refactor: Internal cleanup'
];
for (let index = 0; index < inputs.length; index++) {
await validatePrTitle(inputs[index]);
}
});
it('throws for PR titles without a type', async () => {
await expect(validatePrTitle('Fix bug')).rejects.toThrow(
'No release type found in pull request title "Fix bug".'
);
});
it('throws for PR titles with only a type', async () => {
await expect(validatePrTitle('fix:')).rejects.toThrow(
'No release type found in pull request title "fix:".'
);
});
it('throws for PR titles without a subject', async () => {
await expect(validatePrTitle('fix: ')).rejects.toThrow(
'No subject found in pull request title "fix: ".'
);
});
it('throws for PR titles with an unknown type', async () => {
await expect(validatePrTitle('foo: Bar')).rejects.toThrow(
'Unknown release type "foo" found in pull request title "foo: Bar".'
);
});
describe('defined scopes', () => {
it('allows a missing scope by default', async () => {
await validatePrTitle('fix: Bar');
});
it('allows all scopes by default', async () => {
await validatePrTitle('fix(core): Bar');
});
it('allows a missing scope when custom scopes are defined', async () => {
await validatePrTitle('fix: Bar', {scopes: ['foo']});
});
it('allows a matching scope', async () => {
await validatePrTitle('fix(core): Bar', {scopes: ['core']});
});
it('allows a regex matching scope', async () => {
await validatePrTitle('fix(CORE): Bar', {scopes: ['[A-Z]+']});
});
it('allows multiple matching scopes', async () => {
await validatePrTitle('fix(core,e2e): Bar', {
scopes: ['core', 'e2e', 'web']
});
});
it('allows multiple regex matching scopes', async () => {
await validatePrTitle('fix(CORE,WEB): Bar', {
scopes: ['[A-Z]+']
});
});
it('throws when an unknown scope is detected within multiple scopes', async () => {
await expect(
validatePrTitle('fix(core,e2e,foo,bar): Bar', {scopes: ['foo', 'core']})
).rejects.toThrow(
'Unknown scopes "e2e,bar" found in pull request title "fix(core,e2e,foo,bar): Bar". Scope must match one of: foo, core.'
);
});
it('throws when an unknown scope is detected within multiple scopes', async () => {
await expect(
validatePrTitle('fix(CORE,e2e,foo,bar): Bar', {
scopes: ['foo', '[A-Z]+']
})
).rejects.toThrow(
'Unknown scopes "e2e,bar" found in pull request title "fix(CORE,e2e,foo,bar): Bar". Scope must match one of: foo, [A-Z]+.'
);
});
it('throws when an unknown scope is detected', async () => {
await expect(
validatePrTitle('fix(core): Bar', {scopes: ['foo']})
).rejects.toThrow(
'Unknown scope "core" found in pull request title "fix(core): Bar". Scope must match one of: foo.'
);
});
it('throws when an unknown scope is detected for auto-wrapped regex matching', async () => {
await expect(
validatePrTitle('fix(score): Bar', {scopes: ['core']})
).rejects.toThrow(
'Unknown scope "score" found in pull request title "fix(score): Bar". Scope must match one of: core.'
);
});
it('throws when an unknown scope is detected for auto-wrapped regex matching when input is already wrapped', async () => {
await expect(
validatePrTitle('fix(score): Bar', {scopes: ['^[A-Z]+$']})
).rejects.toThrow(
'Unknown scope "score" found in pull request title "fix(score): Bar". Scope must match one of: ^[A-Z]+$.'
);
});
it('throws when an unknown scope is detected for regex matching', async () => {
await expect(
validatePrTitle('fix(core): Bar', {scopes: ['[A-Z]+']})
).rejects.toThrow(
'Unknown scope "core" found in pull request title "fix(core): Bar". Scope must match one of: [A-Z]+.'
);
});
describe('require scope', () => {
describe('scope allowlist defined', () => {
it('passes when a scope is provided', async () => {
await validatePrTitle('fix(core): Bar', {
scopes: ['core'],
requireScope: true
});
});
it('throws when a scope is missing', async () => {
await expect(
validatePrTitle('fix: Bar', {
scopes: ['foo', 'bar'],
requireScope: true
})
).rejects.toThrow(
'No scope found in pull request title "fix: Bar". Scope must match one of: foo, bar.'
);
});
});
describe('disallow scopes', () => {
it('passes when a single scope is provided, but not present in disallowScopes with one item', async () => {
await validatePrTitle('fix(core): Bar', {disallowScopes: ['release']});
});
it('passes when a single scope is provided, but not present in disallowScopes with one regex item', async () => {
await validatePrTitle('fix(core): Bar', {disallowScopes: ['[A-Z]+']});
});
it('passes when multiple scopes are provided, but not present in disallowScopes with one item', async () => {
await validatePrTitle('fix(core,e2e,bar): Bar', {
disallowScopes: ['release']
});
});
it('passes when multiple scopes are provided, but not present in disallowScopes with one regex item', async () => {
await validatePrTitle('fix(core,e2e,bar): Bar', {
disallowScopes: ['[A-Z]+']
});
});
it('passes when a single scope is provided, but not present in disallowScopes with multiple items', async () => {
await validatePrTitle('fix(core): Bar', {
disallowScopes: ['release', 'test', '[A-Z]+']
});
});
it('passes when multiple scopes are provided, but not present in disallowScopes with multiple items', async () => {
await validatePrTitle('fix(core,e2e,bar): Bar', {
disallowScopes: ['release', 'test', '[A-Z]+']
});
});
it('throws when a single scope is provided and it is present in disallowScopes with one item', async () => {
await expect(
validatePrTitle('fix(release): Bar', {disallowScopes: ['release']})
).rejects.toThrow('Disallowed scope was found: release');
});
it('throws when a single scope is provided and it is present in disallowScopes with one regex item', async () => {
await expect(
validatePrTitle('fix(RELEASE): Bar', {disallowScopes: ['[A-Z]+']})
).rejects.toThrow('Disallowed scope was found: RELEASE');
});
it('throws when a single scope is provided and it is present in disallowScopes with multiple item', async () => {
await expect(
validatePrTitle('fix(release): Bar', {
disallowScopes: ['release', 'test']
})
).rejects.toThrow('Disallowed scope was found: release');
});
it('throws when a single scope is provided and it is present in disallowScopes with multiple regex item', async () => {
await expect(
validatePrTitle('fix(RELEASE): Bar', {
disallowScopes: ['[A-Z]+', '^[A-Z].+$']
})
).rejects.toThrow('Disallowed scope was found: RELEASE');
});
it('throws when multiple scopes are provided and one of them is present in disallowScopes with one item ', async () => {
await expect(
validatePrTitle('fix(release,e2e): Bar', {
disallowScopes: ['release']
})
).rejects.toThrow('Disallowed scope was found: release');
});
it('throws when multiple scopes are provided and one of them is present in disallowScopes with one regex item ', async () => {
await expect(
validatePrTitle('fix(RELEASE,e2e): Bar', {
disallowScopes: ['[A-Z]+']
})
).rejects.toThrow('Disallowed scope was found: RELEASE');
});
it('throws when multiple scopes are provided and one of them is present in disallowScopes with multiple items ', async () => {
await expect(
validatePrTitle('fix(release,e2e): Bar', {
disallowScopes: ['release', 'test']
})
).rejects.toThrow('Disallowed scope was found: release');
});
it('throws when multiple scopes are provided and one of them is present in disallowScopes with multiple items ', async () => {
await expect(
validatePrTitle('fix(RELEASE,e2e): Bar', {
disallowScopes: ['[A-Z]+', 'test']
})
).rejects.toThrow('Disallowed scope was found: RELEASE');
});
it('throws when multiple scopes are provided and more than one of them are present in disallowScopes', async () => {
await expect(
validatePrTitle('fix(release,test,CORE): Bar', {
disallowScopes: ['release', 'test', '[A-Z]+']
})
).rejects.toThrow('Disallowed scopes were found: release, test, CORE');
});
});
describe('scope allowlist not defined', () => {
it('passes when a scope is provided', async () => {
await validatePrTitle('fix(core): Bar', {
requireScope: true
});
});
it('throws when a scope is missing', async () => {
await expect(
validatePrTitle('fix: Bar', {
requireScope: true
})
).rejects.toThrow(
// Should make no mention of any available scope
/^No scope found in pull request title "fix: Bar".$/
);
});
});
});
});
describe('custom types', () => {
it('allows PR titles with a supported type', async () => {
const inputs = ['foo: Foobar', 'bar: Foobar', 'baz: Foobar'];
const types = ['foo', 'bar', 'baz'];
for (let index = 0; index < inputs.length; index++) {
await validatePrTitle(inputs[index], {types});
}
});
it('throws for PR titles with an unknown type', async () => {
await expect(
validatePrTitle('fix: Foobar', {types: ['foo', 'bar']})
).rejects.toThrow(
'Unknown release type "fix" found in pull request title "fix: Foobar".'
);
});
});
describe('description validation', () => {
it('does not validate the description by default', async () => {
await validatePrTitle('fix: sK!"§4123');
});
it('can pass the validation when `subjectPatternError` is configured', async () => {
await validatePrTitle('fix: foobar', {
subjectPattern: '^(?![A-Z]).+$',
subjectPatternError:
'The subject found in the pull request title cannot start with an uppercase character.'
});
});
it('uses the `subjectPatternError` if available when the `subjectPattern` does not match', async () => {
const customError =
'The subject found in the pull request title cannot start with an uppercase character.';
await expect(
validatePrTitle('fix: Foobar', {
subjectPattern: '^(?![A-Z]).+$',
subjectPatternError: customError
})
).rejects.toThrow(customError);
});
it('interpolates variables into `subjectPatternError`', async () => {
await expect(
validatePrTitle('fix: Foobar', {
subjectPattern: '^(?![A-Z]).+$',
subjectPatternError:
'The subject "{subject}" found in the pull request title "{title}" cannot start with an uppercase character.'
})
).rejects.toThrow(
'The subject "Foobar" found in the pull request title "fix: Foobar" cannot start with an uppercase character.'
);
});
it('throws for invalid subjects', async () => {
await expect(
validatePrTitle('fix: Foobar', {
subjectPattern: '^(?![A-Z]).+$'
})
).rejects.toThrow(
'The subject "Foobar" found in pull request title "fix: Foobar" doesn\'t match the configured pattern "^(?![A-Z]).+$".'
);
});
it('throws for only partial matches', async () => {
await expect(
validatePrTitle('fix: Foobar', {
subjectPattern: 'Foo'
})
).rejects.toThrow(
'The subject "Foobar" found in pull request title "fix: Foobar" isn\'t an exact match for the configured pattern "Foo". Please provide a subject that matches the whole pattern exactly.'
);
});
it('accepts valid subjects', async () => {
await validatePrTitle('fix: foobar', {
subjectPattern: '^(?![A-Z]).+$'
});
});
});