1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00
denoland-deno/tests/node_compat/test/parallel/test-util-promisify.js
Matt Mastracci f5e46c9bf2
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests ->
tests, and updates of relative paths for files.

This is the first step towards aggregate all of the integration test
files under tests/, which will lead to a set of integration tests that
can run without the CLI binary being built.

While we could leave these tests under `cli`, it would require us to
keep a more complex directory structure for the various test runners. In
addition, we have a lot of complexity to ignore various test files in
the `cli` project itself (cargo publish exclusion rules, autotests =
false, etc).

And finally, the `tests/` folder will eventually house the `test_ffi`,
`test_napi` and other testing code, reducing the size of the root repo
directory.

For easier review, the extremely large and noisy "move" is in the first
commit (with no changes -- just a move), while the remainder of the
changes to actual files is in the second commit.
2024-02-10 20:22:13 +00:00

218 lines
4.8 KiB
JavaScript

// 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-internals
const 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 });
// }));
// }
{
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)
});
});