-
-
Notifications
You must be signed in to change notification settings - Fork 305
/
teardown.js
316 lines (280 loc) · 10.4 KB
/
teardown.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
'use strict';
var tape = require('../');
var tap = require('tap');
var concat = require('concat-stream');
var forEach = require('for-each');
var v = require('es-value-fixtures');
var inspect = require('object-inspect');
var flatMap = require('array.prototype.flatmap');
var stripFullStack = require('./common').stripFullStack;
tap.test('teardowns', function (tt) {
tt.plan(1);
var test = tape.createHarness();
test.createStream().pipe(concat(function (body) {
tt.same(stripFullStack(body.toString('utf8')), [].concat(
'TAP version 13',
'# success',
'ok 1 should be truthy',
'# success teardown',
'# success teardown 2',
'# success (async)',
'ok 2 should be truthy',
'# success (async) teardown',
'# success (async) teardown 2',
'# nested teardowns',
'# nested success',
'ok 3 should be truthy',
'# nested teardown (nested success level)',
'# nested teardown (nested success level) 2',
'# nested failure',
'not ok 4 nested failure!',
' ---',
' operator: fail',
' at: Test.<anonymous> ($TEST/teardown.js:$LINE:$COL)',
' stack: |-',
' Error: nested failure!',
' [... stack stripped ...]',
' at Test.<anonymous> ($TEST/teardown.js:$LINE:$COL)',
' [... stack stripped ...]',
' ...',
'# nested teardown (nested fail level)',
'# nested teardown (nested fail level) 2',
'# nested teardown (top level)',
'# nested teardown (top level) 2',
'# fail',
'not ok 5 failure!',
' ---',
' operator: fail',
' at: Test.<anonymous> ($TEST/teardown.js:$LINE:$COL)',
' stack: |-',
' Error: failure!',
' [... stack stripped ...]',
' at Test.<anonymous> ($TEST/teardown.js:$LINE:$COL)',
' [... stack stripped ...]',
' ...',
'# failure teardown',
'# failure teardown 2',
'# teardown errors do not stop the next teardown fn from running',
'ok 6 should be truthy',
'not ok 7 SyntaxError: teardown error!',
' ---',
' operator: fail',
' stack: |-',
' Error: SyntaxError: teardown error!',
' [... stack stripped ...]',
' ...',
'not ok 8 plan != count',
' ---',
' operator: fail',
' expected: 1',
' actual: 2',
' stack: |-',
' Error: plan != count',
' [... stack stripped ...]',
' ...',
'# teardown runs after teardown error',
'# teardown given non-function fails the test',
'ok 9 should be truthy',
flatMap(v.nonFunctions, function (nonFunction, i) {
var offset = 10;
return [].concat(
'not ok ' + (offset + (i > 0 ? i + 1 : i)) + ' teardown: ' + inspect(nonFunction) + ' is not a function',
' ---',
' operator: fail',
' at: <anonymous> ($TEST/teardown.js:$LINE:$COL)',
' stack: |-',
' Error: teardown: ' + inspect(nonFunction) + ' is not a function',
' [... stack stripped ...]',
' at $TEST/teardown.js:$LINE:$COL',
' [... stack stripped ...]',
' at Test.<anonymous> ($TEST/teardown.js:$LINE:$COL)',
' [... stack stripped ...]',
' ...',
i > 0 ? [] : [
'not ok ' + (offset + 1) + ' plan != count',
' ---',
' operator: fail',
' expected: 1',
' actual: 2',
' at: <anonymous> ($TEST/teardown.js:$LINE:$COL)',
' stack: |-',
' Error: plan != count',
' [... stack stripped ...]',
' at $TEST/teardown.js:$LINE:$COL',
' [... stack stripped ...]',
' at Test.<anonymous> ($TEST/teardown.js:$LINE:$COL)',
' [... stack stripped ...]',
' ...'
]
);
}),
typeof Promise === 'function' ? [
'# teardown is only ever called once, even when async',
'ok ' + (11 + v.nonFunctions.length) + ' passes',
'# teardown: once?',
'# success (promise)',
'ok ' + (12 + v.nonFunctions.length) + ' should be truthy',
'# success (promise) teardown: 1',
'# success (promise) teardown: 2',
'# success (promise) teardown: 3'
] : [
'# SKIP teardown is only ever called once, even when async',
'# SKIP success (promise)'
], [
'',
'1..' + ((typeof Promise === 'function' ? 2 : 0) + 10 + v.nonFunctions.length),
'# tests ' + ((typeof Promise === 'function' ? 2 : 0) + 10 + v.nonFunctions.length),
'# pass ' + ((typeof Promise === 'function' ? 2 : 0) + 5),
'# fail ' + (5 + v.nonFunctions.length),
''
]));
}));
test('success', function (t) {
t.plan(1);
t.teardown(function () {
t.comment('success teardown');
});
t.teardown(function () {
t.comment('success teardown 2');
});
t.ok('success!');
});
test('success (async)', function (t) {
t.plan(1);
t.teardown(function () {
t.comment('success (async) teardown');
});
t.teardown(function () {
t.comment('success (async) teardown 2');
});
setTimeout(function () {
t.ok('success!');
}, 10);
});
test('nested teardowns', function (t) {
t.plan(2);
t.teardown(function () {
t.comment('nested teardown (top level)');
});
t.teardown(function () {
t.comment('nested teardown (top level) 2');
});
t.test('nested success', function (st) {
st.teardown(function () {
st.comment('nested teardown (nested success level)');
});
st.teardown(function () {
st.comment('nested teardown (nested success level) 2');
});
st.ok('nested success!');
st.end();
});
t.test('nested failure', function (st) {
st.plan(1);
st.teardown(function () {
st.comment('nested teardown (nested fail level)');
});
st.teardown(function () {
st.comment('nested teardown (nested fail level) 2');
});
st.fail('nested failure!');
});
});
test('fail', function (t) {
t.plan(1);
t.teardown(function () {
t.comment('failure teardown');
});
t.teardown(function () {
t.comment('failure teardown 2');
});
t.fail('failure!');
});
test('teardown errors do not stop the next teardown fn from running', function (t) {
t.plan(1);
t.ok('teardown error test');
t.teardown(function () {
throw new SyntaxError('teardown error!');
});
t.teardown(function () {
t.comment('teardown runs after teardown error');
});
});
test('teardown given non-function fails the test', function (t) {
t.plan(1);
t.ok('non-function test');
forEach(v.nonFunctions, function (nonFunction) {
t.teardown(nonFunction);
});
});
test('teardown is only ever called once, even when async', { skip: typeof Promise !== 'function' }, function (t) {
t.plan(1);
t.teardown(function () {
t.comment('teardown: once?');
});
t.pass('passes');
return Promise.resolve();
});
test('success (promise)', { skip: typeof Promise !== 'function' }, function (t) {
t.plan(1);
t.teardown(function () {
return new Promise(function (resolve) {
t.comment('success (promise) teardown: 1');
setTimeout(resolve, 10);
}).then(function () {
t.comment('success (promise) teardown: 2');
});
});
t.teardown(function () {
t.comment('success (promise) teardown: 3');
});
setTimeout(function () {
t.ok('success!');
}, 10);
});
});
tap.test('teardown with promise', { skip: typeof Promise !== 'function', timeout: 1e3 }, function (tt) {
tt.plan(2);
tape('dummy test', function (t) {
var resolved = false;
t.teardown(function () {
tt.pass('tape teardown');
var p = Promise.resolve();
p.then(function () {
resolved = true;
});
return p;
});
t.on('end', function () {
tt.is(resolved, true);
});
t.end();
});
});
tap.test('teardown only runs once', { skip: typeof Promise !== 'function', timeout: 1e3 }, function (tt) {
tt.plan(1);
var test = tape.createHarness();
test.createStream().pipe(concat(function (body) {
tt.same(stripFullStack(body.toString('utf8')), [].concat(
'TAP version 13',
'# teardown is only called once, even with a plan',
'ok 1 passes',
'# Tearing down!',
'',
'1..1',
'# tests 1',
'# pass 1',
'',
'# ok',
''
));
}));
test('teardown is only called once, even with a plan', function (t) {
t.plan(1);
t.teardown(function () {
t.comment('Tearing down!');
});
t.pass('passes');
return Promise.resolve();
});
});