Files
bun.sh/test/bun.js/node-test-helpers.js
Derrick Farris b8586b33da feat(process): add process.{stdin, stdout, stderr} support (#1495)
* fix(stream): get Duplex working

* feat(process): add stdin,stdout,stderr in a semi-broken state (pipes??)

* test(NodeTestHelpers): fix test names

* test(NodeTestHelpers): add test for createDoneDotAll done called w error

* test(NodeTestHelpers): remove stray console.log

* fix(stream): fix bug in Duplex, Readable

* test(process.stdio): rename test

* fix(process.stdio): change onData listener to onReadable

* refactor(streams): add file-wide debug fn, destructure opts

* fix(child_process): check isCallable on promise

* fix: get stdio streams mostly working (mostly)

* fix(child_process): wait until stream is drained before calling end?

* fix(child_process): change to result?.then

* debug(child_process,streams): add EE id tracking, add shim for stdio after handle is dead

* test(child_process): fix double pipe test, temp fix for ChildProcess.kill() return val

* fix(child_process): remove immediate emit of exit on kill

* debug(streams): add more debug log

* debug(streams): add more debug logs part 2

* feat(streams,fs): add NativeWritable, adapt fs.WriteStream and fs.ReadStream to native
2022-12-02 08:25:13 -08:00

157 lines
3.5 KiB
JavaScript

import { expect as expect_ } from "bun:test";
import { gcTick } from "gc";
import assertNode from "node:assert";
const expect = (actual) => {
gcTick();
const ret = expect_(actual);
gcTick();
return ret;
};
export const strictEqual = (...args) => {
let error = null;
try {
assertNode.strictEqual(...args);
} catch (err) {
error = err;
}
expect(error).toBe(null);
};
export const throws = (...args) => {
let error = null;
try {
assertNode.throws(...args);
} catch (err) {
error = err;
}
expect(error).toBe(null);
};
export const assert = (...args) => {
let error = null;
try {
assertNode(...args);
} catch (err) {
error = err;
}
expect(error).toBe(null);
};
export const assertOk = (...args) => {
let error = null;
try {
assertNode.ok(...args);
} catch (err) {
error = err;
}
expect(error).toBe(null);
};
export const createCallCheckCtx = (done, timeout = 1500) => {
const createDone = createDoneDotAll(done);
// const mustCallChecks = [];
// failed.forEach(function (context) {
// console.log(
// "Mismatched %s function calls. Expected %s, actual %d.",
// context.name,
// context.messageSegment,
// context.actual
// );
// console.log(context.stack.split("\n").slice(2).join("\n"));
// });
// TODO: Implement this to be exact only
function mustCall(fn, exact) {
return mustCallAtLeast(fn, exact);
}
function mustSucceed(fn, exact) {
return mustCall(function (err, ...args) {
assert.ifError(err);
if (typeof fn === "function") return fn.apply(this, args);
}, exact);
}
function mustCallAtLeast(fn, minimum) {
return _mustCallInner(fn, minimum, "minimum");
}
function _mustCallInner(fn, criteria = 1, field) {
if (process._exiting)
throw new Error("Cannot use common.mustCall*() in process exit handler");
if (typeof fn === "number") {
criteria = fn;
fn = noop;
} else if (fn === undefined) {
fn = noop;
}
if (typeof criteria !== "number")
throw new TypeError(`Invalid ${field} value: ${criteria}`);
let actual = 0;
let expected = criteria;
// mustCallChecks.push(context);
const done = createDone(timeout);
const _return = (...args) => {
const result = fn.apply(this, args);
actual++;
if (actual >= expected) {
done();
}
return result;
};
// Function instances have own properties that may be relevant.
// Let's replicate those properties to the returned function.
// Refs: https://tc39.es/ecma262/#sec-function-instances
Object.defineProperties(_return, {
name: {
value: fn.name,
writable: false,
enumerable: false,
configurable: true,
},
length: {
value: fn.length,
writable: false,
enumerable: false,
configurable: true,
},
});
return _return;
}
return {
mustSucceed,
mustCall,
mustCallAtLeast,
};
};
export function createDoneDotAll(done) {
let toComplete = 0;
let completed = 0;
function createDoneCb(timeout) {
toComplete += 1;
const timer = setTimeout(() => {
console.log("Timeout");
done(new Error("Timed out!"));
}, timeout);
return (result) => {
clearTimeout(timer);
if (result instanceof Error) {
done(result);
return;
}
completed += 1;
if (completed === toComplete) {
done();
}
};
}
return createDoneCb;
}