deno.land / std@0.166.0 / node / _tools / test / parallel / test-util-promisify.js

test-util-promisify.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
// deno-fmt-ignore-file// deno-lint-ignore-file
// Copyright Joyent and Node contributors. All rights reserved. MIT license.// Taken from Node 16.13.0// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually
'use strict';// Flags: --expose-internalsconst common = require('../common');const assert = require('assert');const fs = require('fs');const vm = require('vm');const { promisify } = require('util');const { customPromisifyArgs } = require('internal/util');
const stat = promisify(fs.stat);
// TODO(wafuwafu13): Fix// {// const promise = stat(__filename);// assert(promise instanceof Promise);// promise.then(common.mustCall((value) => {// assert.deepStrictEqual(value, fs.statSync(__filename));// }));// }
{ const promise = stat('/dontexist'); promise.catch(common.mustCall((error) => { assert(error.message.includes('ENOENT: no such file or directory, stat')); }));}
{ function fn() {}
function promisifedFn() {} fn[promisify.custom] = promisifedFn; assert.strictEqual(promisify(fn), promisifedFn); assert.strictEqual(promisify(promisify(fn)), promisifedFn);}
{ function fn() {}
function promisifiedFn() {}
// util.promisify.custom is a shared symbol which can be accessed // as `Symbol.for("nodejs.util.promisify.custom")`. const kCustomPromisifiedSymbol = Symbol.for('nodejs.util.promisify.custom'); fn[kCustomPromisifiedSymbol] = promisifiedFn;
assert.strictEqual(kCustomPromisifiedSymbol, promisify.custom); assert.strictEqual(promisify(fn), promisifiedFn); assert.strictEqual(promisify(promisify(fn)), promisifiedFn);}
{ function fn() {} fn[promisify.custom] = 42; assert.throws( () => promisify(fn), { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError' } );}
// TODO(wafuwafu13): Fix// {// const firstValue = 5;// const secondValue = 17;
// function fn(callback) {// callback(null, firstValue, secondValue);// }
// fn[customPromisifyArgs] = ['first', 'second'];
// promisify(fn)().then(common.mustCall((obj) => {// assert.deepStrictEqual(obj, { first: firstValue, second: secondValue });// }));// }
// TODO(wafuwafu13): Implement "vm.runInNewContext"// {// const fn = vm.runInNewContext('(function() {})');// assert.notStrictEqual(Object.getPrototypeOf(promisify(fn)),// Function.prototype);// }
{ function fn(callback) { callback(null, 'foo', 'bar'); } promisify(fn)().then(common.mustCall((value) => { assert.deepStrictEqual(value, 'foo'); }));}
{ function fn(callback) { callback(null); } promisify(fn)().then(common.mustCall((value) => { assert.strictEqual(value, undefined); }));}
{ function fn(callback) { callback(); } promisify(fn)().then(common.mustCall((value) => { assert.strictEqual(value, undefined); }));}
{ function fn(err, val, callback) { callback(err, val); } promisify(fn)(null, 42).then(common.mustCall((value) => { assert.strictEqual(value, 42); }));}
{ function fn(err, val, callback) { callback(err, val); } promisify(fn)(new Error('oops'), null).catch(common.mustCall((err) => { assert.strictEqual(err.message, 'oops'); }));}
{ function fn(err, val, callback) { callback(err, val); }
(async () => { const value = await promisify(fn)(null, 42); assert.strictEqual(value, 42); })().then(common.mustCall());}
{ const o = {}; const fn = promisify(function(cb) {
cb(null, this === o); });
o.fn = fn;
o.fn().then(common.mustCall((val) => assert(val)));}
{ const err = new Error('Should not have called the callback with the error.'); const stack = err.stack;
const fn = promisify(function(cb) { cb(null); cb(err); });
(async () => { await fn(); await Promise.resolve(); return assert.strictEqual(stack, err.stack); })().then(common.mustCall());}
{ function c() { } const a = promisify(function() { }); const b = promisify(a); assert.notStrictEqual(c, a); assert.strictEqual(a, b);}
{ let errToThrow; const thrower = promisify(function(a, b, c, cb) { errToThrow = new Error(); throw errToThrow; }); thrower(1, 2, 3) .then(assert.fail) .then(assert.fail, (e) => assert.strictEqual(e, errToThrow));}
{ const err = new Error();
const a = promisify((cb) => cb(err))(); const b = promisify(() => { throw err; })();
Promise.all([ a.then(assert.fail, function(e) { assert.strictEqual(err, e); }), b.then(assert.fail, function(e) { assert.strictEqual(err, e); }), ]);}
[undefined, null, true, 0, 'str', {}, [], Symbol()].forEach((input) => { assert.throws( () => promisify(input), { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError', message: 'The "original" argument must be of type function.' + common.invalidArgTypeHelper(input) });});
std

Version Info

Tagged at
a year ago