1 | 'use strict';
|
2 |
|
3 | var tape = require('../');
|
4 | var tap = require('tap');
|
5 | var concat = require('concat-stream');
|
6 | var forEach = require('for-each');
|
7 | var v = require('es-value-fixtures');
|
8 | var inspect = require('object-inspect');
|
9 | var flatMap = require('array.prototype.flatmap');
|
10 |
|
11 | var stripFullStack = require('./common').stripFullStack;
|
12 |
|
13 | tap.test('teardowns', function (tt) {
|
14 | tt.plan(1);
|
15 |
|
16 | var test = tape.createHarness();
|
17 | test.createStream().pipe(concat(function (body) {
|
18 | tt.same(stripFullStack(body.toString('utf8')), [].concat(
|
19 | 'TAP version 13',
|
20 | '# success',
|
21 | 'ok 1 should be truthy',
|
22 | '# success teardown',
|
23 | '# success teardown 2',
|
24 | '# success (async)',
|
25 | 'ok 2 should be truthy',
|
26 | '# success (async) teardown',
|
27 | '# success (async) teardown 2',
|
28 | '# nested teardowns',
|
29 | '# nested success',
|
30 | 'ok 3 should be truthy',
|
31 | '# nested teardown (nested success level)',
|
32 | '# nested teardown (nested success level) 2',
|
33 | '# nested failure',
|
34 | 'not ok 4 nested failure!',
|
35 | ' ---',
|
36 | ' operator: fail',
|
37 | ' at: Test.<anonymous> ($TEST/teardown.js:$LINE:$COL)',
|
38 | ' stack: |-',
|
39 | ' Error: nested failure!',
|
40 | ' [... stack stripped ...]',
|
41 | ' at Test.<anonymous> ($TEST/teardown.js:$LINE:$COL)',
|
42 | ' [... stack stripped ...]',
|
43 | ' ...',
|
44 | '# nested teardown (nested fail level)',
|
45 | '# nested teardown (nested fail level) 2',
|
46 | '# nested teardown (top level)',
|
47 | '# nested teardown (top level) 2',
|
48 | '# fail',
|
49 | 'not ok 5 failure!',
|
50 | ' ---',
|
51 | ' operator: fail',
|
52 | ' at: Test.<anonymous> ($TEST/teardown.js:$LINE:$COL)',
|
53 | ' stack: |-',
|
54 | ' Error: failure!',
|
55 | ' [... stack stripped ...]',
|
56 | ' at Test.<anonymous> ($TEST/teardown.js:$LINE:$COL)',
|
57 | ' [... stack stripped ...]',
|
58 | ' ...',
|
59 | '# failure teardown',
|
60 | '# failure teardown 2',
|
61 | '# teardown errors do not stop the next teardown fn from running',
|
62 | 'ok 6 should be truthy',
|
63 | 'not ok 7 SyntaxError: teardown error!',
|
64 | ' ---',
|
65 | ' operator: fail',
|
66 | ' stack: |-',
|
67 | ' Error: SyntaxError: teardown error!',
|
68 | ' [... stack stripped ...]',
|
69 | ' ...',
|
70 | 'not ok 8 plan != count',
|
71 | ' ---',
|
72 | ' operator: fail',
|
73 | ' expected: 1',
|
74 | ' actual: 2',
|
75 | ' stack: |-',
|
76 | ' Error: plan != count',
|
77 | ' [... stack stripped ...]',
|
78 | ' ...',
|
79 | '# teardown runs after teardown error',
|
80 | '# teardown given non-function fails the test',
|
81 | 'ok 9 should be truthy',
|
82 | flatMap(v.nonFunctions, function (nonFunction, i) {
|
83 | var offset = 10;
|
84 | return [].concat(
|
85 | 'not ok ' + (offset + (i > 0 ? i + 1 : i)) + ' teardown: ' + inspect(nonFunction) + ' is not a function',
|
86 | ' ---',
|
87 | ' operator: fail',
|
88 | ' at: <anonymous> ($TEST/teardown.js:$LINE:$COL)',
|
89 | ' stack: |-',
|
90 | ' Error: teardown: ' + inspect(nonFunction) + ' is not a function',
|
91 | ' [... stack stripped ...]',
|
92 | ' at $TEST/teardown.js:$LINE:$COL',
|
93 | ' [... stack stripped ...]',
|
94 | ' at Test.<anonymous> ($TEST/teardown.js:$LINE:$COL)',
|
95 | ' [... stack stripped ...]',
|
96 | ' ...',
|
97 | i > 0 ? [] : [
|
98 | 'not ok ' + (offset + 1) + ' plan != count',
|
99 | ' ---',
|
100 | ' operator: fail',
|
101 | ' expected: 1',
|
102 | ' actual: 2',
|
103 | ' at: <anonymous> ($TEST/teardown.js:$LINE:$COL)',
|
104 | ' stack: |-',
|
105 | ' Error: plan != count',
|
106 | ' [... stack stripped ...]',
|
107 | ' at $TEST/teardown.js:$LINE:$COL',
|
108 | ' [... stack stripped ...]',
|
109 | ' at Test.<anonymous> ($TEST/teardown.js:$LINE:$COL)',
|
110 | ' [... stack stripped ...]',
|
111 | ' ...'
|
112 | ]
|
113 | );
|
114 | }),
|
115 | typeof Promise === 'function' ? [
|
116 | '# teardown is only ever called once, even when async',
|
117 | 'ok ' + (11 + v.nonFunctions.length) + ' passes',
|
118 | '# teardown: once?',
|
119 | '# success (promise)',
|
120 | 'ok ' + (12 + v.nonFunctions.length) + ' should be truthy',
|
121 | '# success (promise) teardown: 1',
|
122 | '# success (promise) teardown: 2',
|
123 | '# success (promise) teardown: 3'
|
124 | ] : [
|
125 | '# SKIP teardown is only ever called once, even when async',
|
126 | '# SKIP success (promise)'
|
127 | ],
|
128 | [
|
129 | '',
|
130 | '1..' + ((typeof Promise === 'function' ? 2 : 0) + 10 + v.nonFunctions.length),
|
131 | '# tests ' + ((typeof Promise === 'function' ? 2 : 0) + 10 + v.nonFunctions.length),
|
132 | '# pass ' + ((typeof Promise === 'function' ? 2 : 0) + 5),
|
133 | '# fail ' + (5 + v.nonFunctions.length),
|
134 | ''
|
135 | ]
|
136 | ));
|
137 | }));
|
138 |
|
139 | test('success', function (t) {
|
140 | t.plan(1);
|
141 | t.teardown(function () {
|
142 | t.comment('success teardown');
|
143 | });
|
144 | t.teardown(function () {
|
145 | t.comment('success teardown 2');
|
146 | });
|
147 | t.ok('success!');
|
148 | });
|
149 |
|
150 | test('success (async)', function (t) {
|
151 | t.plan(1);
|
152 | t.teardown(function () {
|
153 | t.comment('success (async) teardown');
|
154 | });
|
155 | t.teardown(function () {
|
156 | t.comment('success (async) teardown 2');
|
157 | });
|
158 | setTimeout(function () {
|
159 | t.ok('success!');
|
160 | }, 10);
|
161 | });
|
162 |
|
163 | test('nested teardowns', function (t) {
|
164 | t.plan(2);
|
165 |
|
166 | t.teardown(function () {
|
167 | t.comment('nested teardown (top level)');
|
168 | });
|
169 | t.teardown(function () {
|
170 | t.comment('nested teardown (top level) 2');
|
171 | });
|
172 |
|
173 | t.test('nested success', function (st) {
|
174 | st.teardown(function () {
|
175 | st.comment('nested teardown (nested success level)');
|
176 | });
|
177 | st.teardown(function () {
|
178 | st.comment('nested teardown (nested success level) 2');
|
179 | });
|
180 |
|
181 | st.ok('nested success!');
|
182 | st.end();
|
183 | });
|
184 |
|
185 | t.test('nested failure', function (st) {
|
186 | st.plan(1);
|
187 |
|
188 | st.teardown(function () {
|
189 | st.comment('nested teardown (nested fail level)');
|
190 | });
|
191 | st.teardown(function () {
|
192 | st.comment('nested teardown (nested fail level) 2');
|
193 | });
|
194 |
|
195 | st.fail('nested failure!');
|
196 | });
|
197 | });
|
198 |
|
199 | test('fail', function (t) {
|
200 | t.plan(1);
|
201 |
|
202 | t.teardown(function () {
|
203 | t.comment('failure teardown');
|
204 | });
|
205 | t.teardown(function () {
|
206 | t.comment('failure teardown 2');
|
207 | });
|
208 |
|
209 | t.fail('failure!');
|
210 | });
|
211 |
|
212 | test('teardown errors do not stop the next teardown fn from running', function (t) {
|
213 | t.plan(1);
|
214 |
|
215 | t.ok('teardown error test');
|
216 |
|
217 | t.teardown(function () {
|
218 | throw new SyntaxError('teardown error!');
|
219 | });
|
220 | t.teardown(function () {
|
221 | t.comment('teardown runs after teardown error');
|
222 | });
|
223 | });
|
224 |
|
225 | test('teardown given non-function fails the test', function (t) {
|
226 | t.plan(1);
|
227 |
|
228 | t.ok('non-function test');
|
229 |
|
230 | forEach(v.nonFunctions, function (nonFunction) {
|
231 | t.teardown(nonFunction);
|
232 | });
|
233 | });
|
234 |
|
235 | test('teardown is only ever called once, even when async', { skip: typeof Promise !== 'function' }, function (t) {
|
236 | t.plan(1);
|
237 |
|
238 | t.teardown(function () {
|
239 | t.comment('teardown: once?');
|
240 | });
|
241 |
|
242 | t.pass('passes');
|
243 |
|
244 | return Promise.resolve();
|
245 | });
|
246 |
|
247 | test('success (promise)', { skip: typeof Promise !== 'function' }, function (t) {
|
248 | t.plan(1);
|
249 |
|
250 | t.teardown(function () {
|
251 | return new Promise(function (resolve) {
|
252 | t.comment('success (promise) teardown: 1');
|
253 | setTimeout(resolve, 10);
|
254 | }).then(function () {
|
255 | t.comment('success (promise) teardown: 2');
|
256 | });
|
257 | });
|
258 | t.teardown(function () {
|
259 | t.comment('success (promise) teardown: 3');
|
260 | });
|
261 |
|
262 | setTimeout(function () {
|
263 | t.ok('success!');
|
264 | }, 10);
|
265 | });
|
266 | });
|
267 |
|
268 | tap.test('teardown with promise', { skip: typeof Promise !== 'function', timeout: 1e3 }, function (tt) {
|
269 | tt.plan(2);
|
270 | tape('dummy test', function (t) {
|
271 | var resolved = false;
|
272 | t.teardown(function () {
|
273 | tt.pass('tape teardown');
|
274 | var p = Promise.resolve();
|
275 | p.then(function () {
|
276 | resolved = true;
|
277 | });
|
278 | return p;
|
279 | });
|
280 | t.on('end', function () {
|
281 | tt.is(resolved, true);
|
282 | });
|
283 | t.end();
|
284 | });
|
285 | });
|
286 |
|
287 | tap.test('teardown only runs once', { skip: typeof Promise !== 'function', timeout: 1e3 }, function (tt) {
|
288 | tt.plan(1);
|
289 |
|
290 | var test = tape.createHarness();
|
291 | test.createStream().pipe(concat(function (body) {
|
292 | tt.same(stripFullStack(body.toString('utf8')), [].concat(
|
293 | 'TAP version 13',
|
294 | '# teardown is only called once, even with a plan',
|
295 | 'ok 1 passes',
|
296 | '# Tearing down!',
|
297 | '',
|
298 | '1..1',
|
299 | '# tests 1',
|
300 | '# pass 1',
|
301 | '',
|
302 | '# ok',
|
303 | ''
|
304 | ));
|
305 | }));
|
306 |
|
307 | test('teardown is only called once, even with a plan', function (t) {
|
308 | t.plan(1);
|
309 |
|
310 | t.teardown(function () {
|
311 | t.comment('Tearing down!');
|
312 | });
|
313 |
|
314 | t.pass('passes');
|
315 |
|
316 | return Promise.resolve();
|
317 | });
|
318 | });
|