diff options
author | 2022-11-08 17:33:47 -0600 | |
---|---|---|
committer | 2022-11-08 15:33:47 -0800 | |
commit | 9ccc455f8d71e46b8bd967317a2e0e907db27012 (patch) | |
tree | 8f3585457438de35ffc1662b44e0f9a7184233c8 /test/bun.js/node-test-helpers.js | |
parent | 8b0a3c75cb98f5026de668b3a23d2e42f94e5d1a (diff) | |
download | bun-9ccc455f8d71e46b8bd967317a2e0e907db27012.tar.gz bun-9ccc455f8d71e46b8bd967317a2e0e907db27012.tar.zst bun-9ccc455f8d71e46b8bd967317a2e0e907db27012.zip |
Fix child_process tests (#1471)
* test(child_process): fix broken tests, add our-assert pkg for testing
* test(child_process): replace console.log with debug()
* test(child_process): rename our-assert -> node-test-helpers, use Bun.peek for subproc.exited
Diffstat (limited to 'test/bun.js/node-test-helpers.js')
-rw-r--r-- | test/bun.js/node-test-helpers.js | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/test/bun.js/node-test-helpers.js b/test/bun.js/node-test-helpers.js new file mode 100644 index 000000000..e7f6c74f6 --- /dev/null +++ b/test/bun.js/node-test-helpers.js @@ -0,0 +1,145 @@ +import { expect } from "bun:test"; +import assertNode from "node:assert"; + +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(() => 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; +} |