/
index.ts
381 lines (328 loc) · 10.9 KB
/
index.ts
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
380
381
// Copyright 2014 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/* eslint-disable @typescript-eslint/no-empty-function,prefer-rest-params */
import * as assert from 'assert';
import {describe, it, afterEach, beforeEach} from 'mocha';
import * as sinon from 'sinon';
import * as util from '../src';
const noop = () => {};
const sandbox = sinon.createSandbox();
describe('promisifyAll', () => {
const fakeArgs = [null, 1, 2, 3];
const fakeError = new Error('err.');
let FakeClass: any;
beforeEach(() => {
FakeClass = class {
methodName(callback: Function) {
callback(...fakeArgs);
}
methodSingle(callback: Function) {
callback(null, fakeArgs[1]);
}
methodError(callback: Function) {
callback(fakeError);
}
};
FakeClass.prototype.method_ = noop;
FakeClass.prototype._method = noop;
FakeClass.prototype.methodStream = noop;
FakeClass.prototype.promise = noop;
util.promisifyAll(FakeClass);
const fc = new FakeClass();
});
afterEach(() => {
sandbox.restore();
});
it('should promisify the correct method', () => {
assert(FakeClass.prototype.methodName.promisified_);
assert(FakeClass.prototype.methodSingle.promisified_);
assert(FakeClass.prototype.methodError.promisified_);
assert.strictEqual(FakeClass.prototype.method_, noop);
assert.strictEqual(FakeClass.prototype._method, noop);
assert.strictEqual(FakeClass.prototype.methodStream, noop);
assert.strictEqual(FakeClass.prototype.promise, noop);
});
// The ts compiler will convert a class to the current node version target,
// in this case v4, which means that using the class keyword to create a
// class won't actually test that this method works on ES classes. Using
// eval works around that compilation. The class syntax is a syntax error
// in node v4 which is why the eval call is wrapped in a try catch block.
try {
eval(`
const assert2 = require('assert');
const util = require('../src');
it('should work on ES classes', () => {
class MyESClass {
myMethod(str, callback) {
callback(str.toUpperCase());
}
}
util.promisifyAll(MyESClass);
assert2(MyESClass.prototype.myMethod.promisified_);
});
`);
} catch (error) {
it.skip('should work on ES classes');
}
it('should optionally accept an exclude list', () => {
function FakeClass2() {}
FakeClass2.prototype.methodSync = noop;
FakeClass2.prototype.method = () => {};
util.promisifyAll(FakeClass2, {
exclude: ['methodSync'],
});
assert.strictEqual(FakeClass2.prototype.methodSync, noop);
assert(FakeClass2.prototype.method.promisified_);
});
it('should pass the options object to promisify', done => {
const fakeOptions = {
a: 'a',
} as util.PromisifyAllOptions;
const stub = sandbox
.stub(util, 'promisify')
.callsFake((method, options) => {
assert.strictEqual(method, FakeClass2.prototype.method);
assert.strictEqual(options, fakeOptions);
done();
stub.restore();
});
function FakeClass2() {}
FakeClass2.prototype.method = () => {};
util.promisifyAll(FakeClass2, fakeOptions);
});
it('should not re-promisify methods', () => {
const method = FakeClass.prototype.methodName;
util.promisifyAll(FakeClass);
assert.strictEqual(FakeClass.prototype.methodName, method);
});
});
describe('promisify', () => {
const fakeContext = {};
let func: Function;
let fakeArgs: Array<Error | number | null>;
beforeEach(() => {
fakeArgs = [null, 1, 2, 3];
func = util.promisify(function (this: {}, callback: () => void) {
(callback as any).apply(this, fakeArgs);
});
});
it('should not re-promisify the function', () => {
const original = func;
func = util.promisify(func);
assert.strictEqual(original, func);
});
it('should not return a promise in callback mode', done => {
let returnVal: any;
returnVal = func.call(fakeContext, function (this: {}) {
const args = [...arguments];
assert.deepStrictEqual(args, fakeArgs);
assert.strictEqual(this, fakeContext);
assert(!returnVal);
returnVal = null; // this is to suppress prefer-const.
done();
});
});
it('should return a promise when the callback is omitted', () => {
return func().then((args: Array<{}>) => {
assert.deepStrictEqual(args, fakeArgs.slice(1));
});
});
it('should reject the promise on a failed request', () => {
const error = new Error('err');
fakeArgs = [error];
return func().then(
() => {
throw new Error('Should have gone to failure block');
},
(err: Error) => {
assert.strictEqual(err, error);
}
);
});
it('should allow the Promise object to be overridden', () => {
const FakePromise = class {};
const promise = func.call({Promise: FakePromise});
assert(promise instanceof FakePromise);
});
it('should resolve singular arguments', () => {
const fakeArg = 'hi';
func = util.promisify(
(callback: () => void) => {
(callback as any).apply(func, [null, fakeArg]);
},
{
singular: true,
}
);
return func().then((arg: {}) => {
assert.strictEqual(arg, fakeArg);
});
});
it('should ignore singular when multiple args are present', () => {
const fakeArgs: any[] = ['a', 'b'];
func = util.promisify(
(callback: Function) => {
callback.apply(func, [null].concat(fakeArgs));
},
{
singular: true,
}
);
return func().then((args: Array<{}>) => {
assert.deepStrictEqual(args, fakeArgs);
});
});
describe('trailing undefined arguments', () => {
it('should not return a promise in callback mode', () => {
const func = util.promisify((optional: Function) => {
assert.strictEqual(typeof optional, 'function');
optional(null);
});
const returnVal = func(() => {});
assert.strictEqual(returnVal, undefined);
});
it('should return a promise when callback omitted', done => {
const func = util.promisify((optional: Function, ...args: Array<{}>) => {
assert.strictEqual(args.length, 0);
assert.strictEqual(typeof optional, 'function');
optional(null);
});
func(undefined, undefined).then(() => {
done();
});
});
it('should not mistake non-function args for callbacks', done => {
const func = util.promisify(
(foo: {}, optional: Function, ...args: Array<{}>) => {
assert.strictEqual(args.length, 0);
assert.strictEqual(typeof optional, 'function');
optional(null);
}
);
func('foo').then(() => {
done();
});
});
});
});
describe('callbackifyAll', () => {
const fakeArgs = [1, 2, 3];
const fakeError = new Error('err.');
let FakeClass: any;
beforeEach(() => {
FakeClass = class {
async methodName() {
return fakeArgs;
}
async methodError() {
throw fakeError;
}
};
FakeClass.prototype.method_ = noop;
FakeClass.prototype._method = noop;
FakeClass.prototype.methodStream = noop;
util.callbackifyAll(FakeClass);
});
it('should callbackify the correct method', () => {
assert(FakeClass.prototype.methodName.callbackified_);
assert(FakeClass.prototype.methodError.callbackified_);
assert.strictEqual(FakeClass.prototype.method_, noop);
assert.strictEqual(FakeClass.prototype._method, noop);
assert.strictEqual(FakeClass.prototype.methodStream, noop);
});
it('should optionally accept an exclude list', () => {
function FakeClass2() {}
FakeClass2.prototype.methodSync = noop;
FakeClass2.prototype.method = () => {};
util.callbackifyAll(FakeClass2, {
exclude: ['methodSync'],
});
assert.strictEqual(FakeClass2.prototype.methodSync, noop);
assert(FakeClass2.prototype.method.callbackified_);
assert.strictEqual(FakeClass2.prototype.methodSync, noop);
});
it('should not re-callbackify method', () => {
const method = FakeClass.prototype.methodName;
util.callbackifyAll(FakeClass);
assert.strictEqual(FakeClass.prototype.methodName, method);
});
});
describe('callbackify', () => {
let func: Function;
let fakeArgs: number[];
beforeEach(() => {
fakeArgs = [1, 2, 3];
func = util.callbackify(async (_this: {}) => {
return fakeArgs;
});
});
it('should not re-callbackify the function', () => {
const original = func;
func = util.callbackify(func);
assert.strictEqual(original, func);
});
it('should return a promise when callback is not provided', () => {
func().then((args: []) => {
assert.deepStrictEqual(args, fakeArgs);
});
});
it('should call the callback if it is provided', done => {
func(function (this: {}) {
const args = [].slice.call(arguments);
assert.deepStrictEqual(args, [null, ...fakeArgs]);
done();
});
});
it('should call the provided callback with undefined', done => {
func = util.callbackify(async (_this: {}) => {});
func((err: Error, resp: {}) => {
assert.strictEqual(err, null);
assert.strictEqual(resp, undefined);
done();
});
});
it('should call the provided callback with null', done => {
func = util.callbackify(async (_this: {}) => {
return null;
});
func(function (this: {}) {
const args = [].slice.call(arguments);
assert.deepStrictEqual(args, [null, null]);
done();
});
});
it('should call the callback with error when promise rejects', () => {
const error = new Error('err');
func = util.callbackify(async () => {
throw error;
});
func((err: Error) => assert.strictEqual(err, error));
});
it('should call the callback only a single time when the promise resolves but callback throws an error', () => {
const error = new Error('err');
const callback = sinon.stub().throws(error);
const originalRejection = process.listeners('unhandledRejection').pop();
if (originalRejection) {
process.removeListener('unhandledRejection', originalRejection!);
}
process.once('unhandledRejection', err => {
assert.strictEqual(error, err);
assert.ok(callback.calledOnce);
if (originalRejection) {
process.listeners('unhandledRejection').push(originalRejection!);
}
});
func(callback);
});
});