-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.test.js
More file actions
72 lines (63 loc) · 2.3 KB
/
Copy pathscript.test.js
File metadata and controls
72 lines (63 loc) · 2.3 KB
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
// test.js
const assert = require('assert');
const checkLabels = require('./script.js');
// 1. Set up the environment variables
process.env.REQ_LABELS = 'bug, feature, chore';
process.env.SEMVER_REQ_LABELS = 'feature, bug';
process.env.SEMVER_LABELS = 'major, minor, patch';
async function runTests() {
console.log('Starting tests...\n');
let hasFailed = false;
// TEST 1: Throws error if missing required category
try {
const context = { payload: { pull_request: { labels: [{ name: 'documentation' }] } } };
await assert.rejects(
checkLabels({ context }),
Error,
"Expected script to throw when required labels are missing"
);
console.log('✅ Passed: Missing required category throws error');
} catch (err) {
console.error('❌ Failed: Test 1', err.message);
hasFailed = true;
}
// TEST 2: Returns early (succeeds) if category doesn't need semver
try {
const context = { payload: { pull_request: { labels: [{ name: 'chore' }] } } };
await assert.doesNotReject(
checkLabels({ context })
);
console.log('✅ Passed: Category without semver requirement succeeds');
} catch (err) {
console.error('❌ Failed: Test 2', err);
hasFailed = true;
}
// TEST 3: Throws error if semver is required but missing
try {
const context = { payload: { pull_request: { labels: [{ name: 'feature' }] } } };
await assert.rejects(
checkLabels({ context }),
/Pull Request requires a 'Semantic version'-label/,
"Expected script to throw when semver is missing"
);
console.log('✅ Passed: Missing semver throws error');
} catch (err) {
console.error('❌ Failed: Test 3', err.message);
hasFailed = true;
}
// TEST 4: Succeeds when all labels are correct
try {
const context = { payload: { pull_request: { labels: [{ name: 'feature' }, { name: 'minor' }] } } };
await assert.doesNotReject(
checkLabels({ context })
);
console.log('✅ Passed: Valid labels succeed');
} catch (err) {
console.error('❌ Failed: Test 4', err);
hasFailed = true;
}
if (hasFailed) {
process.exit(1);
}
}
runTests();