UNPKG

3.47 kBJavaScriptView Raw
1/*
2 * tests/compat.js: Some of the APIs provided by vasync are intended to be
3 * API-compatible with node-async, so we incorporate the tests from node-async
4 * directly here. These are copied from https://github.com/caolan/async,
5 * available under the MIT license. To make it easy to update this from the
6 * source, this file should remain unchanged from the source except for this
7 * header comment, the change to the "require" line, and deleted lines for
8 * unimplemented functions.
9 *
10 * The following tests are deliberately omitted:
11 *
12 * o "waterfall non-array": Per Joyent's Best Practices for Node.js Error
13 * Handling, we're strict about argument types and throw on these programmer
14 * errors rather than emitting them asynchronously.
15 *
16 * o "waterfall multiple callback calls": We deliberately disallow a waterfall
17 * function to invoke its callback more than once, so we don't test for that
18 * here. The behavior that node-async allows can potentially be used to fork
19 * the waterfall, which may be useful, but it's often used instead as an
20 * excuse to write code sloppily. And the downside is that it makes it really
21 * hard to understand bugs where the waterfall was resumed too early. For
22 * now, we're disallowing it, but if the forking behavior becomes useful, we
23 * can always make our version less strict.
24 */
25var async = require('../lib/vasync');
26
27exports['waterfall'] = function(test){
28 test.expect(6);
29 var call_order = [];
30 async.waterfall([
31 function(callback){
32 call_order.push('fn1');
33 setTimeout(function(){callback(null, 'one', 'two');}, 0);
34 },
35 function(arg1, arg2, callback){
36 call_order.push('fn2');
37 test.equals(arg1, 'one');
38 test.equals(arg2, 'two');
39 setTimeout(function(){callback(null, arg1, arg2, 'three');}, 25);
40 },
41 function(arg1, arg2, arg3, callback){
42 call_order.push('fn3');
43 test.equals(arg1, 'one');
44 test.equals(arg2, 'two');
45 test.equals(arg3, 'three');
46 callback(null, 'four');
47 },
48 function(arg4, callback){
49 call_order.push('fn4');
50 test.same(call_order, ['fn1','fn2','fn3','fn4']);
51 callback(null, 'test');
52 }
53 ], function(err){
54 test.done();
55 });
56};
57
58exports['waterfall empty array'] = function(test){
59 async.waterfall([], function(err){
60 test.done();
61 });
62};
63
64exports['waterfall no callback'] = function(test){
65 async.waterfall([
66 function(callback){callback();},
67 function(callback){callback(); test.done();}
68 ]);
69};
70
71exports['waterfall async'] = function(test){
72 var call_order = [];
73 async.waterfall([
74 function(callback){
75 call_order.push(1);
76 callback();
77 call_order.push(2);
78 },
79 function(callback){
80 call_order.push(3);
81 callback();
82 },
83 function(){
84 test.same(call_order, [1,2,3]);
85 test.done();
86 }
87 ]);
88};
89
90exports['waterfall error'] = function(test){
91 test.expect(1);
92 async.waterfall([
93 function(callback){
94 callback('error');
95 },
96 function(callback){
97 test.ok(false, 'next function should not be called');
98 callback();
99 }
100 ], function(err){
101 test.equals(err, 'error');
102 });
103 setTimeout(test.done, 50);
104};