UNPKG

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