test.js
2.54 KB
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
var t = require('tap')
var loop = require('./')
var obj = {}
t.test('basic passing operation', function (t) {
var i = 0
loop(obj, [
function (cb) {
t.equal(this, obj, 'this is correct 1')
t.equal(i, 0, '0')
cb()
i++
},
function () {
t.equal(this, obj, 'this is correct 2')
t.equal(i++, 1, '1')
return Promise.resolve(true)
},
function (cb) {
t.equal(this, obj, 'this is correct 3')
t.equal(i++, 2, '2')
setTimeout(cb)
},
function (cb) {
t.equal(this, obj, 'this is correct 4')
t.equal(i++, 3, '3')
process.nextTick(cb)
}
], function () {
t.equal(this, obj, 'this is correct 5')
t.equal(i++, 4, '4')
t.end()
}, function (er) {
throw er
})
t.equal(i, 2, '2, after loop() call')
})
t.test('throws', function (t) {
loop(obj, [
function (cb) {
t.equal(this, obj, 'this is correct')
throw new Error('foo')
},
function () {
t.fail('should not get here')
}
], function () {
t.fail('should not get here')
}, function (er) {
t.match(er, { message: 'foo' })
t.end()
})
})
t.test('all sync', function (t) {
var i = 0
loop(obj, [
function (cb) { t.equal(i++, 0); cb() },
function (cb) { t.equal(i++, 1); cb() },
function (cb) { t.equal(i++, 2); cb() },
function (cb) { t.equal(i++, 3); cb() },
function (cb) { t.equal(i++, 4); cb() }
], function () {
t.equal(i++, 5)
}, function (er) {
throw er
})
t.equal(i, 6)
t.end()
})
t.test('broken promise', function (t) {
loop(obj, [
function (cb) {
t.equal(this, obj, 'this is correct')
return Promise.reject(new Error('foo'))
},
function () {
t.fail('should not get here')
}
], function () {
t.fail('should not get here')
}, function (er) {
t.equal(this, obj, 'this is correct')
t.match(er, { message: 'foo' })
t.end()
})
})
t.test('cb err', function (t) {
loop(obj, [
function (cb) {
t.equal(this, obj, 'this is correct')
cb(new Error('foo'))
},
function () {
t.fail('should not get here')
}
], function () {
t.fail('should not get here')
}, function (er) {
t.equal(this, obj, 'this is correct')
t.match(er, { message: 'foo' })
t.end()
})
})
t.test('cb and also promise resolve', function (t) {
t.plan(1)
loop(obj, [
function (cb) {
return Promise.resolve('foo').then(function () {
cb()
})
}
], function () {
t.pass('finished one time')
}, function (er) {
throw er
})
})