UNPKG

11.8 kBJavaScriptView Raw
1'use strict'
2
3var assert = require('assert')
4var Promise = require('promise')
5var result = require('test-result');
6var test = require('../')
7var Suite = require('../lib/suite')
8
9// we are going to tamper with the `test-result` module
10// so we wire things up here to make sure the result is
11// definitely recorded
12var originalResult = {
13 pass: result.pass,
14 fail: result.fail
15};
16test.on('suite-fail', function () {
17 originalResult.fail('fallback test failer');
18});
19
20function captureLogs() {
21 var logs = []
22 var log = console.log
23 var now = Suite.now
24 var pass = result.pass
25 var fail = result.fail
26 console.log = function (str) {
27 assert(arguments.length === 1)
28 str = str.split('\n')[0];
29 logs.push(str)
30 }
31 Suite.now = function () {
32 return 0
33 }
34 result.pass = function () {
35 logs.push('<pass>')
36 }
37 result.fail = function () {
38 logs.push('<fail>')
39 }
40 return function () {
41 console.log = log
42 Suite.now = now
43 result.pass = pass
44 result.fail = fail
45 return logs
46 }
47}
48
49test('synchronous', function () {
50 test('passing tests', function () {
51 var logs = captureLogs()
52 var suite = new Suite()
53 suite.disableColors()
54 suite.addLogging()
55 suite.addExit()
56 suite.addTest('passes tests that do not fail', function () {
57 assert(true)
58 })
59 return suite.run().then(function () {
60 assert.deepEqual(logs(), [
61 ' ✓ passes tests that do not fail (0ms)',
62 '',
63 'Total duration 0ms',
64 '<pass>'
65 ])
66 })
67 })
68 test('nested passing tests', function () {
69 var logs = captureLogs()
70 var suite = new Suite()
71 suite.disableColors()
72 suite.addLogging()
73 suite.addExit()
74 suite.addTest('passes tests that do not fail', function () {
75 suite.addTest('even when they are nested', function () {
76 assert(true)
77 })
78 })
79 return suite.run().then(function () {
80 assert.deepEqual(logs(), [
81 ' • passes tests that do not fail',
82 ' ✓ even when they are nested (0ms)',
83 '',
84 'Total duration 0ms',
85 '<pass>'
86 ])
87 })
88 })
89 test('failing tests', function () {
90 var logs = captureLogs()
91 var suite = new Suite()
92 suite.disableColors()
93 suite.addLogging()
94 suite.addExit()
95 suite.addTest('fails tests that fail', function () {
96 assert(false)
97 })
98 return suite.run().then(function () {
99 throw new Error('expected failure');
100 }, function (err) {
101 assert.deepEqual(logs(), [
102 ' ✗ fails tests that fail (0ms)',
103 '',
104 ' AssertionError: false == true',
105 '',
106 'Total duration 0ms',
107 '<fail>'
108 ])
109 })
110 })
111 test('nested failing tests', function () {
112 var logs = captureLogs()
113 var suite = new Suite()
114 suite.disableColors()
115 suite.addLogging()
116 suite.addExit()
117 suite.addTest('fails tests that fail', function () {
118 suite.addTest('even when they are nested', function () {
119 assert(false)
120 })
121 })
122 return suite.run().then(function () {
123 throw new Error('expected failure');
124 }, function (err) {
125 assert.deepEqual(logs(), [
126 ' • fails tests that fail',
127 ' ✗ even when they are nested (0ms)',
128 '',
129 ' AssertionError: false == true',
130 '',
131 'Total duration 0ms',
132 '<fail>'
133 ])
134 })
135 })
136})
137
138test('asynchronous', function () {
139 test('promises', function () {
140 test('passing tests', function () {
141 var logs = captureLogs()
142 var suite = new Suite()
143 suite.disableColors()
144 suite.addLogging()
145 suite.addExit()
146 suite.addTest('passes tests that do not fail', function () {
147 return Promise.resolve(null).then(function () {
148 assert(true)
149 })
150 })
151 return suite.run().then(function () {
152 assert.deepEqual(logs(), [
153 ' ✓ passes tests that do not fail (0ms)',
154 '',
155 'Total duration 0ms',
156 '<pass>'
157 ])
158 })
159 })
160 test('nested passing tests', function () {
161 var logs = captureLogs()
162 var suite = new Suite()
163 suite.disableColors()
164 suite.addLogging()
165 suite.addExit()
166 suite.addTest('passes tests that do not fail', function () {
167 suite.addTest('even when they are nested', function () {
168 return Promise.resolve(null).then(function () {
169 assert(true)
170 })
171 })
172 })
173 return suite.run().then(function () {
174 assert.deepEqual(logs(), [
175 ' • passes tests that do not fail',
176 ' ✓ even when they are nested (0ms)',
177 '',
178 'Total duration 0ms',
179 '<pass>'
180 ])
181 })
182 })
183 test('failing tests', function () {
184 var logs = captureLogs()
185 var suite = new Suite()
186 suite.disableColors()
187 suite.addLogging()
188 suite.addExit()
189 suite.addTest('fails tests that fail', function () {
190 return Promise.resolve(null).then(function () {
191 assert(false)
192 })
193 })
194 return suite.run().then(function () {
195 throw new Error('expected failure');
196 }, function (err) {
197 assert.deepEqual(logs(), [
198 ' ✗ fails tests that fail (0ms)',
199 '',
200 ' AssertionError: false == true',
201 '',
202 'Total duration 0ms',
203 '<fail>'
204 ])
205 })
206 })
207 test('nested failing tests', function () {
208 var logs = captureLogs()
209 var suite = new Suite()
210 suite.disableColors()
211 suite.addLogging()
212 suite.addExit()
213 suite.addTest('fails tests that fail', function () {
214 suite.addTest('even when they are nested', function () {
215 return Promise.resolve(null).then(function () {
216 assert(false)
217 })
218 })
219 })
220 return suite.run().then(function () {
221 throw new Error('expected failure');
222 }, function (err) {
223 assert.deepEqual(logs(), [
224 ' • fails tests that fail',
225 ' ✗ even when they are nested (0ms)',
226 '',
227 ' AssertionError: false == true',
228 '',
229 'Total duration 0ms',
230 '<fail>'
231 ])
232 })
233 })
234 test('failing because of timeout', function () {
235 var logs = captureLogs()
236 var suite = new Suite()
237 suite.disableColors()
238 suite.addLogging()
239 suite.addExit()
240 suite.addTest('eventually times out', function () {
241 return new Promise(function () {});
242 }, {timeout: '10ms'})
243 return suite.run().then(function () {
244 throw new Error('expected failure');
245 }, function (err) {
246 assert.deepEqual(logs(), [
247 ' ✗ eventually times out (0ms)',
248 '',
249 ' Error: Operation timed out',
250 '',
251 'Total duration 0ms',
252 '<fail>'
253 ])
254 })
255 })
256 })
257 test('callbacks', function () {
258 test('passing tests', function () {
259 var logs = captureLogs()
260 var suite = new Suite()
261 suite.disableColors()
262 suite.addLogging()
263 suite.addExit()
264 suite.addTest('passes tests that do not fail', function (cb) {
265 Promise.resolve(null).then(function () {
266 assert(true)
267 }).nodeify(cb)
268 })
269 return suite.run().then(function () {
270 assert.deepEqual(logs(), [
271 ' ✓ passes tests that do not fail (0ms)',
272 '',
273 'Total duration 0ms',
274 '<pass>'
275 ])
276 })
277 })
278 test('nested passing tests', function () {
279 var logs = captureLogs()
280 var suite = new Suite()
281 suite.disableColors()
282 suite.addLogging()
283 suite.addExit()
284 suite.addTest('passes tests that do not fail', function () {
285 suite.addTest('even when they are nested', function (cb) {
286 Promise.resolve(null).then(function () {
287 assert(true)
288 }).nodeify(cb)
289 })
290 })
291 return suite.run().then(function () {
292 assert.deepEqual(logs(), [
293 ' • passes tests that do not fail',
294 ' ✓ even when they are nested (0ms)',
295 '',
296 'Total duration 0ms',
297 '<pass>'
298 ])
299 })
300 })
301 test('failing tests', function () {
302 var logs = captureLogs()
303 var suite = new Suite()
304 suite.disableColors()
305 suite.addLogging()
306 suite.addExit()
307 suite.addTest('fails tests that fail', function (cb) {
308 Promise.resolve(null).then(function () {
309 assert(false)
310 }).nodeify(cb)
311 })
312 return suite.run().then(function () {
313 throw new Error('expected failure');
314 }, function (err) {
315 assert.deepEqual(logs(), [
316 ' ✗ fails tests that fail (0ms)',
317 '',
318 ' AssertionError: false == true',
319 '',
320 'Total duration 0ms',
321 '<fail>'
322 ])
323 })
324 })
325 test('nested failing tests', function () {
326 var logs = captureLogs()
327 var suite = new Suite()
328 suite.disableColors()
329 suite.addLogging()
330 suite.addExit()
331 suite.addTest('fails tests that fail', function () {
332 suite.addTest('even when they are nested', function (cb) {
333 Promise.resolve(null).then(function () {
334 assert(false)
335 }).nodeify(cb)
336 })
337 })
338 return suite.run().then(function () {
339 throw new Error('expected failure');
340 }, function (err) {
341 assert.deepEqual(logs(), [
342 ' • fails tests that fail',
343 ' ✗ even when they are nested (0ms)',
344 '',
345 ' AssertionError: false == true',
346 '',
347 'Total duration 0ms',
348 '<fail>'
349 ])
350 })
351 })
352 test('failing because of timeout', function () {
353 var logs = captureLogs()
354 var suite = new Suite()
355 suite.disableColors()
356 suite.addLogging()
357 suite.addExit()
358 suite.addTest('eventually times out', function (cb) {
359 }, {timeout: '10ms'})
360 return suite.run().then(function () {
361 throw new Error('expected failure');
362 }, function (err) {
363 assert.deepEqual(logs(), [
364 ' ✗ eventually times out (0ms)',
365 '',
366 ' Error: Operation timed out',
367 '',
368 'Total duration 0ms',
369 '<fail>'
370 ])
371 })
372 })
373 })
374})
375
376test('run', function () {
377 test.run(function () {
378 console.log('using test.run to run some code in between tests');
379 });
380 test('run success', function () {
381 var logs = captureLogs()
382 var suite = new Suite()
383 suite.disableColors()
384 suite.addLogging()
385 suite.addExit()
386 suite.addCode(function () {
387 assert(true)
388 })
389 return suite.run().then(function () {
390 assert.deepEqual(logs(), [
391 '',
392 'Total duration 0ms',
393 '<pass>'
394 ])
395 })
396 })
397 test('run fail', function () {
398 var logs = captureLogs()
399 var suite = new Suite()
400 suite.disableColors()
401 suite.addLogging()
402 suite.addExit()
403 suite.addCode(function () {
404 assert(false)
405 })
406 return suite.run().then(function () {
407 throw new Error('expected failure');
408 }, function () {
409 assert.deepEqual(logs(), [
410 ' ✗ run (0ms)',
411 '',
412 ' AssertionError: false == true',
413 '',
414 'Total duration 0ms',
415 '<fail>'
416 ])
417 })
418 })
419})
420
421test('infinite timeout', function () {
422 var logs = captureLogs()
423 var suite = new Suite()
424 suite.disableColors()
425 suite.addLogging()
426 suite.addExit()
427 suite.addTest('passes tests that do not fail', function () {
428 assert(true)
429 }, {timeout: Infinity})
430 return suite.run().then(function () {
431 assert.deepEqual(logs(), [
432 ' ✓ passes tests that do not fail (0ms)',
433 '',
434 'Total duration 0ms',
435 '<pass>'
436 ])
437 })
438});