-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
test.js
379 lines (306 loc) · 9.77 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import util from 'node:util';
import fs from 'node:fs';
import stream from 'node:stream';
import test from 'ava';
import pinkiePromise from 'pinkie-promise';
import pify from './index.js';
const fixture = callback => setImmediate(() => {
callback(null, 'unicorn');
});
const fixture1 = callback => setImmediate(() => {
callback('error', 'unicorn', 'rainbow');
});
const fixture2 = (value, callback) => setImmediate(() => {
callback(null, value);
});
const fixture3 = callback => setImmediate(() => {
callback(null, 'unicorn', 'rainbow');
});
const fixture4 = callback => setImmediate(() => {
callback(null, 'unicorn');
return 'rainbow';
});
fixture4.meow = callback => {
setImmediate(() => {
callback(null, 'unicorn');
});
};
const fixture5 = () => 'rainbow';
const fixtureModule = {
method1: fixture,
method2: fixture,
method3: fixture5,
};
function FixtureGrandparent() {}
FixtureGrandparent.prototype.grandparentMethod1 = fixture;
FixtureGrandparent.prototype.overriddenMethod1 = fixture;
function FixtureParent() {}
util.inherits(FixtureParent, FixtureGrandparent);
FixtureParent.prototype.parentMethod1 = fixture;
FixtureParent.prototype.overriddenMethod1 = fixture2;
FixtureParent.prototype.overriddenValue1 = 2;
function FixtureClass() {
this.instanceMethod1 = fixture;
this.instanceValue1 = 72;
}
util.inherits(FixtureClass, FixtureParent);
FixtureClass.prototype.method1 = fixture;
FixtureClass.prototype.method2Cb = function (callback) {
setImmediate(() => {
callback(null, this.instanceValue1);
});
};
FixtureClass.prototype.method2Async = pify(FixtureClass.prototype.method2Cb);
FixtureParent.prototype.overriddenValue1 = 4;
FixtureClass.prototype.value1 = 'neo';
test('main', async t => {
t.is(typeof pify(fixture)().then, 'function');
t.is(await pify(fixture)(), 'unicorn');
});
test('throw error on invalid input', t => {
let error = t.throws(() => pify());
t.is(error.message, 'Expected `input` to be a `Function` or `Object`, got `undefined`');
error = t.throws(() => pify(''));
t.is(error.message, 'Expected `input` to be a `Function` or `Object`, got `string`');
error = t.throws(() => pify(null));
t.is(error.message, 'Expected `input` to be a `Function` or `Object`, got `null`');
});
test('error', async t => {
t.is(await pify(fixture1)().catch(error => error), 'error');
});
test('pass argument', async t => {
t.is(await pify(fixture2)('rainbow'), 'rainbow');
});
test('custom Promise module', async t => {
t.is(await pify(fixture, {promiseModule: pinkiePromise})(), 'unicorn');
});
test('multiArgs option', async t => {
t.deepEqual(await pify(fixture3, {multiArgs: true})(), ['unicorn', 'rainbow']);
});
test('multiArgs option — rejection', async t => {
t.deepEqual(await pify(fixture1, {multiArgs: true})().catch(error => error), ['error', 'unicorn', 'rainbow']);
});
test('wrap core method', async t => {
t.is(JSON.parse(await pify(fs.readFile)('package.json')).name, 'pify');
});
test('module support', async t => {
t.is(JSON.parse(await pify(fs).readFile('package.json')).name, 'pify');
});
test('module support - doesn\'t transform *Sync methods by default', t => {
t.is(JSON.parse(pify(fs).readFileSync('package.json')).name, 'pify');
});
test('module support - doesn\'t transform *Stream methods by default', t => {
t.true(pify(fs).createReadStream('package.json') instanceof stream.Readable);
});
test('module support - preserves non-function members', t => {
const module = {
method() {},
nonMethod: 3,
};
t.deepEqual(Object.keys(module), Object.keys(pify(module)));
});
test('module support - transforms only members in options.include', t => {
const pModule = pify(fixtureModule, {
include: ['method1', 'method2'],
});
t.is(typeof pModule.method1().then, 'function');
t.is(typeof pModule.method2().then, 'function');
t.not(typeof pModule.method3().then, 'function');
});
test('module support - doesn\'t transform members in options.exclude', t => {
const pModule = pify(fixtureModule, {
exclude: ['method3'],
});
t.is(typeof pModule.method1().then, 'function');
t.is(typeof pModule.method2().then, 'function');
t.not(typeof pModule.method3().then, 'function');
});
test('module support - options.include over options.exclude', t => {
const pModule = pify(fixtureModule, {
include: ['method1', 'method2'],
exclude: ['method2', 'method3'],
});
t.is(typeof pModule.method1().then, 'function');
t.is(typeof pModule.method2().then, 'function');
t.not(typeof pModule.method3().then, 'function');
});
test('module support — function modules', t => {
const pModule = pify(fixture4);
t.is(typeof pModule().then, 'function');
t.is(typeof pModule.meow().then, 'function');
});
test('module support — function modules exclusion', t => {
const pModule = pify(fixture4, {
excludeMain: true,
});
t.is(typeof pModule.meow().then, 'function');
t.not(typeof pModule(() => {}).then, 'function');
});
test('`errorFirst` option', async t => {
const fixture = (foo, callback) => {
callback(foo);
};
t.is(await pify(fixture, {errorFirst: false})('🦄'), '🦄');
});
test('`errorFirst` option and `multiArgs`', async t => {
const fixture = (foo, bar, callback) => {
callback(foo, bar);
};
t.deepEqual(await pify(fixture, {
errorFirst: false,
multiArgs: true,
})('🦄', '🌈'), ['🦄', '🌈']);
});
test('class support - does not create a copy', async t => {
const object = {
x: 'foo',
y(callback) {
setImmediate(() => {
callback(null, this.x);
});
},
};
const pified = pify(object);
object.x = 'bar';
t.is(await pified.y(), 'bar');
t.is(pified.x, 'bar');
});
test('class support — transforms inherited methods', t => {
const instance = new FixtureClass();
const pInstance = pify(instance);
t.is(instance.value1, pInstance.value1);
t.is(typeof pInstance.instanceMethod1().then, 'function');
t.is(typeof pInstance.method1().then, 'function');
t.is(typeof pInstance.parentMethod1().then, 'function');
t.is(typeof pInstance.grandparentMethod1().then, 'function');
});
test('class support — preserves prototype', t => {
const instance = new FixtureClass();
const pInstance = pify(instance);
t.true(pInstance instanceof FixtureClass);
});
test('class support — respects inheritance order', async t => {
const instance = new FixtureClass();
const pInstance = pify(instance);
t.is(instance.overriddenValue1, pInstance.overriddenValue1);
t.is(await pInstance.overriddenMethod1('rainbow'), 'rainbow');
});
test('class support - transforms only members in options.include, copies all', t => {
const instance = new FixtureClass();
const pInstance = pify(instance, {
include: ['parentMethod1'],
});
t.is(typeof pInstance.parentMethod1().then, 'function');
t.not(typeof pInstance.method1(() => {}).then, 'function');
t.not(typeof pInstance.grandparentMethod1(() => {}).then, 'function');
});
test('class support - doesn\'t transform members in options.exclude', t => {
const instance = new FixtureClass();
const pInstance = pify(instance, {
exclude: ['grandparentMethod1'],
});
t.not(typeof pInstance.grandparentMethod1(() => {}).then, 'function');
t.is(typeof pInstance.parentMethod1().then, 'function');
});
test('class support - options.include over options.exclude', t => {
const instance = new FixtureClass();
const pInstance = pify(instance, {
include: ['method1', 'parentMethod1'],
exclude: ['parentMethod1', 'grandparentMethod1'],
});
t.is(typeof pInstance.method1().then, 'function');
t.is(typeof pInstance.parentMethod1().then, 'function');
t.not(typeof pInstance.grandparentMethod1(() => {}).then, 'function');
});
test('promisify prototype function', async t => {
const instance = new FixtureClass();
t.is(await instance.method2Async(), 72);
});
test('method mutation', async t => {
const object = {
foo(callback) {
setImmediate(() => {
callback(null, 'original');
});
},
};
const pified = pify(object);
object.foo = callback => setImmediate(() => {
callback(null, 'new');
});
t.is(await pified.foo(), 'new');
});
test('symbol keys', async t => {
await t.notThrowsAsync(async () => {
const symbol = Symbol('symbol');
const object = {
[symbol](callback) {
setImmediate(callback);
},
};
const pified = pify(object);
await pified[symbol]();
});
});
// [[Get]] for proxy objects enforces the following invariants: The value
// reported for a property must be the same as the value of the corresponding
// target object property if the target object property is a non-writable,
// non-configurable own data property.
test('non-writable non-configurable property', t => {
const object = {};
Object.defineProperty(object, 'prop', {
value(callback) {
setImmediate(callback);
},
writable: false,
configurable: false,
});
const pified = pify(object);
t.notThrows(() => {
Reflect.get(pified, 'prop');
});
});
test('do not promisify Function.prototype.bind', async t => {
function fn(callback) {
callback(null, this);
}
const target = {};
t.is(await pify(fn).bind(target)(), target);
});
test('do not break internal callback usage', async t => {
const object = {
foo(callback) {
this.bar(4, callback);
},
bar(...arguments_) {
const callback = arguments_.pop();
callback(null, 42);
},
};
t.is(await pify(object).foo(), 42);
});
test('Function.prototype.call', async t => {
function fn(...arguments_) {
const callback = arguments_.pop();
callback(null, arguments_.length);
}
const pified = pify(fn);
t.is(await pified.call(), 0);
});
test('Function.prototype.apply', async t => {
function fn(...arguments_) {
const callback = arguments_.pop();
callback(null, arguments_.length);
}
const pified = pify(fn);
t.is(await pified.apply(), 0);
});
test('self as member', async t => {
function fn(...arguments_) {
const callback = arguments_.pop();
callback(null, arguments_.length);
}
fn.self = fn;
const pified = pify(fn);
t.is(await pified.self(), 0);
});