-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
200 lines (169 loc) · 6.01 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
/*!
* relike <https://github.com/hybridables/relike>
*
* Copyright (c) 2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
'use strict'
var fs = require('fs')
var test = require('mukla')
var isArray = require('isarray')
var isBuffer = require('is-buffer')
var semver = require('semver')
var relike = require('./index')
function create () {
return require('./index') // eslint-disable-line
}
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 .catch TypeError if not function given', function () {
return relike(123).catch(function (err) {
test.strictEqual(err.name, 'TypeError')
test.ok(/expect `fn` be function/.test(err.message))
})
})
test('should promisify with native Promise or Bluebird', function () {
var promise = relike(fs.readFile, './package.json', 'utf-8')
return promise.then(function (res) {
test.ok(res.indexOf('"license": "MIT"') !== -1)
if (semver.lt(process.version, '0.11.13')) {
test.ok(promise.Promise.___bluebirdPromise === true)
test.ok(promise.___bluebirdPromise, true)
}
})
})
test('should flatten multiple arguments to array by default', function () {
return relike(multipleArgs, 11, 22, 33).then(function (res) {
test.strictEqual(isArray(res), true)
test.deepEqual(res, [11, 22, 33])
})
})
test('should skip last argument only if it is `fn(foo, bar, cb)` (async fn)', function () {
return relike(notSkipOne, 111, 222).then(function (res) {
test.strictEqual(isArray(res), true)
test.deepEqual(res, [111, 222, noop])
})
})
test('should not skip last argument and work core api (fs.readFileSync)', function () {
return relike(notSkipTwo, 333, 5555).then(function (res) {
test.strictEqual(isArray(res), true)
test.deepEqual(res, [333, 5555, fs.readFileSync])
})
})
test('should not skip if pass callback fn, e.g. fn(err, res) as last argument', function () {
function foo (_err, res) {}
return relike(function (one, fn, cb) {
cb(null, one, fn)
}, 123, foo).then(function (res) {
test.strictEqual(isArray(res), true)
test.deepEqual(res, [123, foo])
})
})
test('should promisify sync function `fs.readFileSync` and handle utf8 result', function () {
var promise = relike(fs.readFileSync, 'package.json', 'utf8')
return promise
.then(JSON.parse)
.then(function (res) {
test.strictEqual(res.name, 'relike')
})
})
test('should promisify `fs.readFileSync` and handle buffer result', function () {
return relike(fs.readFileSync, 'package.json').then(function (buf) {
test.strictEqual(isBuffer(buf), true)
})
})
test('should catch errors from failing sync function', function () {
var promise = relike(fs.readFileSync, 'foobar.json', 'utf8')
return promise.catch(function (err) {
test.strictEqual(err.code, 'ENOENT')
test.strictEqual(/no such file or directory/.test(err.message), true)
})
})
test('should mute all errors and `.catch` them (should not crash)', function (done) {
return relike(function () {
foobar // eslint-disable-line
/* istanbul ignore next */
return 123
}).catch(function (err) {
test.ifError(!err)
test.ok(err instanceof Error)
test.equal(err.name, 'ReferenceError')
})
})
test('should `.promisify` method wrap a function and return a function', function () {
var readFile = relike.promisify(fs.readFile)
return readFile('package.json', 'utf8')
.then(JSON.parse)
.then(function (data) {
test.strictEqual(data.name, 'relike')
})
})
test('should emit `unhandledRejection` event', function (done) {
relike(JSON.parse, '{boo:"bar}')
process.once('unhandledRejection', function (err) {
test.ifError(!err)
test.strictEqual(err.name, 'SyntaxError')
test.ok(/Unexpected token/.test(err.message))
done()
})
})
test('should `.promisify` method allow passing `.Promise` to promisified function', function () {
var relike = create()
var readFile = relike.promisify(fs.readFile)
readFile.Promise = require('pinkie') // eslint-disable-line
var promise = readFile('package.json', 'utf8')
return promise
.then(function (res) {
test.strictEqual(typeof res, 'string')
test.strictEqual(isBuffer(res), false)
if (semver.lt(process.version, '0.11.13')) {
test.strictEqual(promise.___customPromise, true)
test.strictEqual(promise.Promise.___customPromise, true)
}
})
})
test('should use promise passed to `relike.promisify.Promise` and promisifing with `.promisify`', function () {
var relike = create()
relike.promisify.Promise = require('pinkie') // eslint-disable-line
var statFile = relike.promisify(fs.stat)
var promise = statFile('index.js')
return promise.then(function (res) {
if (semver.lt(process.version, '0.11.13')) {
test.ok(promise.___customPromise === true)
}
test.strictEqual(typeof res, 'object')
test.ok(res.mtime)
})
})
test('should not use custom promise constructor passed to `relike.Promise` when use `.promisify`', function () {
var relike = create()
relike.Promise = require('pinkie') // eslint-disable-line
var readSync = relike.promisify(fs.readFileSync)
var promise = readSync('README.md', 'utf8')
return promise.then(function resolve (result) {
test.ok(typeof result === 'string')
if (semver.lt(process.version, '0.11.13')) {
test.strictEqual(promise.___customPromise, true)
}
})
})
test('should promisify with promise module (pinkie) given in `relike.Promise`', function () {
var relike = create()
relike.Promise = require('pinkie') // eslint-disable-line
var promise = relike(fs.readFile, 'package.json')
return promise.then(function (res) {
test.strictEqual(isBuffer(res), true)
if (semver.lt(process.version, '0.11.13')) {
test.ok(promise.Promise.___customPromise === true)
test.strictEqual(promise.___customPromise === true, true)
}
})
})