-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
182 lines (148 loc) · 4.37 KB
/
Copy pathindex.js
File metadata and controls
182 lines (148 loc) · 4.37 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
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
const EventEmitter = require('events');
const indent = require('indent-string');
const emitter = new EventEmitter();
function envToBool(value) {
return ['1', 'true', 'True', 'TRUE', true].includes(value);
}
const trials = [];
function testOnly(name, promise) {
const index = trials.push({name, succeeded: null}) - 1;
function succeed() {
trials[index].succeeded = true;
emitter.emit('success', name);
return trials[index];
}
function fail(reason) {
trials[index].succeeded = false;
trials[index].reason = reason;
emitter.emit('failure', name);
if (envToBool(process.env.CUP_BAIL)) {
process.exit();
}
return trials[index];
}
let result;
try {
emitter.emit('start', name);
if (promise instanceof Function) {
promise = Promise.resolve(promise());
}
result = promise.then(succeed).catch(fail);
} catch (error) {
result = Promise.resolve(fail(error));
}
return result.then((trial) => {
emitter.emit('end', name);
if (trial.succeeded) {
return trial;
}
return Promise.reject(trial);
});
}
function test(...args) {
if (envToBool(process.env.CUP_ONLY)) {
return Promise.resolve();
}
return testOnly(...args);
}
const serialTrials = {};
function makeSerial(testFn) {
return (name, promise, key = 'default') => {
const serialTrial = serialTrials[key] || Promise.resolve();
serialTrials[key] = serialTrial.catch(() => null)
.then(() => testFn(name, promise));
return serialTrials[key];
};
}
const serial = makeSerial(test);
const serialOnly = makeSerial(testOnly);
function shouldThrow(fn, regex) {
return () => {
let threw = true;
try {
fn();
threw = false;
} catch (error) {
if (regex) {
console.assert(
regex.test(error.message),
`Error message did not match regex: ${error.message}`,
);
}
}
console.assert(threw, 'Test did not throw');
};
}
function shouldReject(promise, regex) {
if (promise instanceof Function) {
promise = promise();
}
return new Promise((resolve, reject) => (
promise.then(() => reject(new Error('Promise did not reject.')))
.catch((error) => {
if (!error) {
resolve();
return;
}
if (!(error instanceof Error)) {
reject(Error(
`shouldReject expects the rejection reason to either be falsey, or an "Error", instead got: ${error}`,
));
return;
}
if (regex && !regex.test(error.message)) {
reject(Error(
`Error message did not match regex: ${error.message}`,
));
return;
}
resolve();
})
));
}
function finish() {
console.log('TAP version 13');
console.log(`1..${trials.length}`);
trials.forEach((trial, index) => {
const number = index + 1;
if (trial.succeeded) {
console.log(`ok ${number} ${trial.name}`);
return;
}
console.log(`not ok ${number} ${trial.name}`);
console.log(' ---');
console.log(' reason: |-');
if (trial.succeeded === null) {
console.log(indent('never finished', 4));
} else if (trial.reason && trial.reason.stack) {
console.log(indent(trial.reason.stack, 4));
} else {
console.log(indent(String(trial.reason), 4));
}
console.log(' ...');
});
const passes = trials.reduce((total, trial) => {
if (trial.succeeded) {
return total + 1;
}
return total;
}, 0);
const failures = trials.length - passes;
console.log(`# tests ${trials.length}`);
console.log(`# pass ${passes}`);
console.log(`# fail ${failures}`);
if (failures) {
process.exitCode = 1;
}
}
process.on('exit', finish);
process.on('SIGINT', process.exit);
module.exports = {
test,
testOnly,
serial,
serialOnly,
shouldThrow,
shouldReject,
emitter,
};