• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('./common');
4const assert = require('assert');
5const fs = require('fs');
6const vm = require('vm');
7const promisify = require('..');
8//const customPromisifyArgs = require('..').customPromisifyArgs;
9
10const stat = promisify(fs.stat);
11
12{
13  const promise = stat(__filename);
14  assert(promise instanceof Promise);
15  promise.then(common.mustCall((value) => {
16    assert.deepStrictEqual(value, fs.statSync(__filename));
17  }));
18}
19
20{
21  const promise = stat('/dontexist');
22  promise.catch(common.mustCall((error) => {
23    assert(error.message.includes('ENOENT: no such file or directory, stat'));
24  }));
25}
26
27{
28  function fn() {}
29  function promisifedFn() {}
30  fn[promisify.custom] = promisifedFn;
31  assert.strictEqual(promisify(fn), promisifedFn);
32  assert.strictEqual(promisify(promisify(fn)), promisifedFn);
33}
34
35{
36  function fn() {}
37  fn[promisify.custom] = 42;
38  assert.throws(
39      () => promisify(fn),
40      (err) => err instanceof TypeError &&
41                err.message === 'The [util.promisify.custom] property must ' +
42                                'be a function');
43}
44
45/*{
46  const firstValue = 5;
47  const secondValue = 17;
48
49  function fn(callback) {
50    callback(null, firstValue, secondValue);
51  }
52
53  fn[customPromisifyArgs] = ['first', 'second'];
54
55  promisify(fn)().then(common.mustCall((obj) => {
56    assert.deepStrictEqual(obj, {first: firstValue, second: secondValue});
57  }));
58}*/
59
60{
61  const fn = vm.runInNewContext('(function() {})');
62  assert.notStrictEqual(Object.getPrototypeOf(promisify(fn)),
63                        Function.prototype);
64}
65
66{
67  function fn(callback) {
68    callback(null, 'foo', 'bar');
69  }
70  promisify(fn)().then(common.mustCall((value) => {
71    assert.deepStrictEqual(value, 'foo');
72  }));
73}
74
75{
76  function fn(callback) {
77    callback(null);
78  }
79  promisify(fn)().then(common.mustCall((value) => {
80    assert.strictEqual(value, undefined);
81  }));
82}
83
84{
85  function fn(callback) {
86    callback();
87  }
88  promisify(fn)().then(common.mustCall((value) => {
89    assert.strictEqual(value, undefined);
90  }));
91}
92
93{
94  function fn(err, val, callback) {
95    callback(err, val);
96  }
97  promisify(fn)(null, 42).then(common.mustCall((value) => {
98    assert.strictEqual(value, 42);
99  }));
100}
101
102{
103  function fn(err, val, callback) {
104    callback(err, val);
105  }
106  promisify(fn)(new Error('oops'), null).catch(common.mustCall((err) => {
107    assert.strictEqual(err.message, 'oops');
108  }));
109}
110
111if (Number(process.version[1]) >= 7) eval`
112{
113
114  function fn(err, val, callback) {
115    callback(err, val);
116  }
117
118  (async () => {
119    const value = await promisify(fn)(null, 42);
120    assert.strictEqual(value, 42);
121  })();
122}`
123
124{
125  const o = {};
126  const fn = promisify(function(cb) {
127
128    cb(null, this === o);
129  });
130
131  o.fn = fn;
132
133  o.fn().then(common.mustCall(function(val) {
134    assert(val);
135  }));
136}
137
138if (Number(process.version[1]) >= 7) eval`
139{
140  const err = new Error('Should not have called the callback with the error.');
141  const stack = err.stack;
142
143  const fn = promisify(function(cb) {
144    cb(null);
145    cb(err);
146  });
147
148  (async () => {
149    await fn();
150    await Promise.resolve();
151    return assert.strictEqual(stack, err.stack);
152  })();
153}`
154
155{
156  function c() { }
157  const a = promisify(function() { });
158  const b = promisify(a);
159  assert.notStrictEqual(c, a);
160  assert.strictEqual(a, b);
161}
162
163{
164  let errToThrow;
165  const thrower = promisify(function(a, b, c, cb) {
166    errToThrow = new Error();
167    throw errToThrow;
168  });
169  thrower(1, 2, 3)
170    .then(assert.fail)
171    .then(assert.fail, (e) => assert.strictEqual(e, errToThrow));
172}
173
174{
175  const err = new Error();
176
177  const a = promisify((cb) => cb(err))();
178  const b = promisify(() => { throw err; })();
179
180  Promise.all([
181    a.then(assert.fail, function(e) {
182      assert.strictEqual(err, e);
183    }),
184    b.then(assert.fail, function(e) {
185      assert.strictEqual(err, e);
186    })
187  ]);
188}
189
190if (Number(process.version[1]) >= 8)
191{
192  const coreUtil = require('util');
193  assert.strictEqual(coreUtil.promisify.custom, promisify.custom);
194}
195