-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
221 lines (188 loc) · 5.94 KB
/
test.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*!
* redolent <https://github.com/tunnckoCore/redolent>
*
* Copyright (c) Charlike Mike Reagent <@tunnckoCore> (https://i.am.charlike.online)
* Released under the MIT license.
*/
/* jshint asi:true */
'use strict'
var fs = require('fs')
var test = require('mukla')
var semver = require('semver')
var redolent = require('./dist/redolent-testing.js')
var extend = require('extend-shallow')
var Pinkie = require('pinkie')
function noop () {}
function notSkipOne (one, two, cb) {
cb(null, one, two, noop)
}
function notSkipTwo (one, two, cb) {
cb(null, one, two, fs.readFileSync)
}
function multipleArgs (one, two, three, cb) {
cb(null, one, two, three)
}
test('should throw TypeError if not function given', function (done) {
function fixture () {
redolent(false)
}
test.throws(fixture, TypeError)
test.throws(fixture, /expect `fn` to be a function/)
done()
})
function factory (promisify) {
test('should promisify a sync function', function () {
var fn = promisify(function () {
return 'foo bar baz'
})
return fn().then(function (str) {
test.strictEqual(str, 'foo bar baz')
})
})
test('should flatten args', function () {
var func = promisify(function (a, b, c, d, e) {
return [a, b, c, d, e]
}, {
args: ['foo', 'bar']
})
return func(123, [1, 2], { a: 'b' }).then(function (arr) {
test.deepEqual(arr, [ 'foo', 'bar', 123, [ 1, 2 ], { a: 'b' } ])
})
})
test('should promisify with given opts.Promise module (pinkie)', function (done) {
var readFile = promisify(fs.readFile, {
Promise: Pinkie
})
var promise = readFile('package.json', 'utf8')
promise.then(function (res) {
test.strictEqual(typeof res, 'string')
if (semver.lt(process.version, '0.11.13')) {
test.strictEqual(promise.___customPromise, true)
}
done()
}).catch(done)
})
test('should flatten multiple arguments to array by default', function (done) {
promisify(multipleArgs)(11, 22, 33).then(function (res) {
test.strictEqual(Array.isArray(res), true)
test.deepEqual(res, [11, 22, 33])
done()
}).catch(done)
})
test('should not skip if pass callback fn, e.g. fn(err, res) as last argument', function (done) {
function foo (_err, res) { return [_err, res] }
promisify(function (one, fn, cb) {
cb(null, one, fn)
})(123, foo).then(function (res) {
test.strictEqual(Array.isArray(res), true)
test.deepEqual(res, [123, foo])
done()
}).catch(done)
})
test('should promisify `fs.readFileSync` and handle buffer result', function (done) {
var readFile = promisify(fs.readFileSync)
readFile('package.json').then(function (buf) {
test.strictEqual(typeof buf.toString(), 'string')
done()
}).catch(done)
})
test('should catch errors from failing sync function', function (done) {
promisify(fs.readFileSync)('foobar.json', 'utf8')
.catch(function (err) {
test.strictEqual(err.code, 'ENOENT')
test.strictEqual(/no such file or directory/.test(err.message), true)
done()
})
.catch(done)
})
test('should skip last argument only if it is `fn(foo, bar, cb)` (async fn)', function (done) {
promisify(notSkipOne)(111, 222).then(function (res) {
test.strictEqual(Array.isArray(res), true)
test.deepEqual(res, [111, 222, noop])
done()
}).catch(done)
})
test('should not skip last argument and work core api (fs.readFileSync)', function (done) {
promisify(notSkipTwo)(333, 5555).then(function (res) {
test.strictEqual(Array.isArray(res), true)
test.deepEqual(res, [333, 5555, fs.readFileSync])
done()
}).catch(done)
})
test('should call the callback once', function (done) {
var fn = promisify(function (cb) {
cb(null, 1)
cb(null, 2)
})
fn()
.then(function (res) {
test.strictEqual(res, 1)
done()
})
.catch(done)
})
test('should be rejected promise if callback(err)', function (done) {
var fn = promisify(function (cb) {
cb(new Error('foo qux'))
})
fn()
.catch(function (err) {
test.strictEqual(err.name, 'Error')
test.strictEqual(err.message, 'foo qux')
done()
})
.catch(done)
})
test('should work if `done` is present, but return a promise', function (done) {
var fn = promisify(function (xxx, cb) {
return Pinkie.resolve(100 + xxx)
})
return fn(200).then(function (val) {
test.strictEqual(val, 300)
done()
}, done).catch(done)
})
test('should reject if async fn returns non Promise, nor call a callback', function (done) {
var func = promisify(function (cb) {
return 'foo bar'
})
func().catch(function (err) {
test.strictEqual(err instanceof Error, true)
test.strictEqual(/Asynchronous functions can only/.test(err.message), true)
test.strictEqual(/return a Promise or invoke a callback/.test(err.message), true)
done()
}).catch(done)
})
}
if (semver.lt(process.version, '0.12.0')) {
factory(function (fn, opts) {
return redolent(fn, extend({
Promise: Pinkie
}, opts))
})
test('should autoload some custom Promise (installed in some of the devDeps)', function (done) {
var func = redolent(function () { return 123 })
var promise = func()
test.strictEqual(Promise.___customPromise, true)
test.strictEqual(promise.___customPromise, true)
promise.then(function (res) {
test.strictEqual(res, 123)
done()
}, done).catch(done)
})
} else {
factory(function (fn, opts) {
return redolent(fn, extend({}, opts))
})
test('should always load native promise if >= 0.12', function (done) {
var promise = redolent(function () {
return 1
})()
promise.then(function (num) {
test.strictEqual(Promise.___nativePromise, true)
test.strictEqual(promise.___nativePromise, true)
test.strictEqual(num, 1)
done()
}, done).catch(done)
})
}