import { AnyFunction } from "bun"; import { gcTick, hideFromStackTrace } from "harness"; import assertNode from "node:assert"; type DoneCb = (err?: Error) => any; function noop() {} export function createTest(path: string) { const { expect, test, it, describe, beforeAll, afterAll, beforeEach, afterEach } = Bun.jest(path); hideFromStackTrace(expect); // Assert const strictEqual = (...args: Parameters) => { assertNode.strictEqual(...args); expect(true).toBe(true); }; const notStrictEqual = (...args: Parameters) => { assertNode.notStrictEqual(...args); expect(true).toBe(true); }; const deepStrictEqual = (...args: Parameters) => { assertNode.deepStrictEqual(...args); expect(true).toBe(true); }; const throws = (...args: Parameters) => { assertNode.throws(...args); expect(true).toBe(true); }; const ok = (...args: Parameters) => { assertNode.ok(...args); expect(true).toBe(true); }; const ifError = (...args: Parameters) => { assertNode.ifError(...args); expect(true).toBe(true); }; const match = (...args: Parameters) => { assertNode.match(...args); expect(true).toBe(true); }; interface NodeAssert { (args: any): void; strictEqual: typeof strictEqual; deepStrictEqual: typeof deepStrictEqual; notStrictEqual: typeof notStrictEqual; throws: typeof throws; ok: typeof ok; ifError: typeof ifError; match: typeof match; } const assert = function (...args: any[]) { // @ts-ignore assertNode(...args); } as NodeAssert; hideFromStackTrace(strictEqual); hideFromStackTrace(notStrictEqual); hideFromStackTrace(deepStrictEqual); hideFromStackTrace(throws); hideFromStackTrace(ok); hideFromStackTrace(ifError); hideFromStackTrace(match); hideFromStackTrace(assert); Object.assign(assert, { strictEqual, deepStrictEqual, notStrictEqual, throws, ok, ifError, match, }); // End assert const createCallCheckCtx = (done: DoneCb) => { var timers: Timer[] = []; const createDone = createDoneDotAll(done, undefined, timers); // 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?: (...args: any[]) => any, exact?: number) { return mustCallAtLeast(fn!, exact!); } function closeTimers() { timers.forEach(t => clearTimeout(t)); } function mustNotCall(reason: string = "function should not have been called", optionalCb?: (err?: any) => void) { const localDone = createDone(); timers.push(setTimeout(() => localDone(), 200)); return () => { closeTimers(); if (optionalCb) optionalCb.apply(undefined, reason ? [reason] : []); done(new Error(reason)); }; } function mustSucceed(fn: () => any, exact?: number) { return mustCall(function (err, ...args) { ifError(err); // @ts-ignore if (typeof fn === "function") return fn(...(args as [])); }, exact); } function mustCallAtLeast(fn: AnyFunction, minimum: number) { return _mustCallInner(fn, minimum, "minimum"); } function _mustCallInner(fn: AnyFunction, criteria = 1, field: string) { // @ts-ignore 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(); const _return = (...args: any[]) => { try { // @ts-ignore const result = fn(...args); actual++; if (actual >= expected) { closeTimers(); done(); } return result; } catch (err) { if (err instanceof Error) done(err); else if (err?.toString) done(new Error(err?.toString())); else { console.error("Unknown error", err); done(new Error("Unknown error")); } closeTimers(); } }; // 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, mustNotCall, closeTimers, }; }; function createDoneDotAll(done: DoneCb, globalTimeout?: number, timers: Timer[] = []) { let toComplete = 0; let completed = 0; const globalTimer = globalTimeout ? (timers.push( setTimeout(() => { console.log("Global Timeout"); done(new Error("Timed out!")); }, globalTimeout), ), timers[timers.length - 1]) : undefined; function createDoneCb(timeout?: number) { toComplete += 1; const timer = timeout !== undefined ? (timers.push( setTimeout(() => { console.log("Timeout"); done(new Error("Timed out!")); }, timeout), ), timers[timers.length - 1]) : timeout; return (result?: Error) => { if (timer) clearTimeout(timer); if (globalTimer) clearTimeout(globalTimer); if (result instanceof Error) { done(result); return; } completed += 1; if (completed === toComplete) { done(); } }; } return createDoneCb; } return { expect, test, it, describe, beforeAll, afterAll, beforeEach, afterEach, createDoneDotAll, strictEqual, notStrictEqual, deepStrictEqual, throws, ok, ifError, createCallCheckCtx, match, assert, }; } declare namespace Bun { function jest(path: string): typeof import("bun:test"); } Unnamed repository; edit this file 'description' to name the repository.
aboutsummaryrefslogtreecommitdiff
AgeCommit message (Expand)AuthorFilesLines
2023-09-21Update completions listGravatar Colin McDonnell 1-0/+2
2023-09-21Update install docsGravatar Colin McDonnell 1-6/+14
2023-09-21Update test scriptGravatar Colin McDonnell 2-2/+4
2023-09-21Add docs for extending process.envGravatar Colin McDonnell 1-0/+25
2023-09-21decorator metadata defaults and rest argsGravatar Dylan Conway 2-9/+72
2023-09-21Fix test failures (#5862)Gravatar Jarred Sumner 5-19/+24
2023-09-21On Linux, respect memory limit from cgroups (#5849)Gravatar Jarred Sumner 8-58/+89
2023-09-21Revert "feat(encoding): support BOM detection (#5550)"Gravatar Jarred Sumner 5-70/+8
2023-09-21Don't await pluginsGravatar Jarred Sumner 2-2/+2
2023-09-21Add another testGravatar Jarred Sumner 1-0/+5
2023-09-21Fixes #5859jarred/5859Gravatar Jarred Sumner 2-6/+55
2023-09-21Regenerate builtinsGravatar Jarred Sumner 3-5009/+5009
2023-09-21Fix `make jsc` on Linux (#4779)Gravatar Aaron Dewes 1-1/+1
2023-09-21PrettierGravatar Jarred Sumner 1-11/+10
2023-09-20fix: add check to sqlite extension loading logic (#5773)Gravatar Liz 2-0/+9
2023-09-20fix(console.log): change default depth from 8 to 2 (#5839)Gravatar Jibran Kalia 3-1/+22
2023-09-20In http client, use .toOwnedSlice() instead of potentially re-using the WTFSt...Gravatar Jarred Sumner 1-4/+7
2023-09-20fix(fetch): fix redirect in relative path location. (#5781)Gravatar Ai Hoshino 2-14/+106
2023-09-20remove /test.js and git ignore it - sorryGravatar dave caruso 2-1/+1
2023-09-20docs(project): development fix Arch install-dependencies command (#5270)Gravatar saurabh 1-1/+1
2023-09-20fix(run): interpret extensionless files as typescript (#5711)Gravatar dave caruso 13-19/+76
2023-09-20fix(ffi) fix size limit for dlopen (#5516)Gravatar Ciro Spaciari 8-5016/+5306
2023-09-20feat(encoding): support BOM detection (#5550)Gravatar WingLim 5-8/+71
2023-09-20feat(node:dns): implement `dns.lookupService` (#5613)Gravatar Ai Hoshino 6-9/+492
2023-09-20implement `Module.prototype._compile` (#5840)Gravatar dave caruso 4-2/+78
2023-09-20feat(runtime): implement `console._stdout` (#5842)Gravatar dave caruso 2-1/+59
2023-09-20Improve types for `test.each`, `describe.each` (#5838)Gravatar Colin McDonnell 2-10/+59
2023-09-20Fix rendering of bun.lockb in vscode extensionGravatar Ashcon Partovi 5-114/+4
2023-09-20Run bun fmtGravatar Ashcon Partovi 2-12/+9
2023-09-20Update quickstartGravatar Colin McDonnell 1-0/+19
2023-09-20Update prisma guideGravatar Colin McDonnell 1-0/+6
2023-09-20Update env docGravatar Colin McDonnell 1-1/+1
2023-09-20Clarify hot modeGravatar Colin McDonnell 1-10/+9
2023-09-20[bun install] Add `-E` as alias of `--exact` (#5104)Gravatar Jonah Snider 2-2/+57
2023-09-20feat: switch disableTelemetry to bunfig (#5690)Gravatar Lucas Coratger 3-1/+14
2023-09-20Treat `undefined` value as empty in expect.toThrow (#5788)Gravatar LongYinan 1-3/+3
2023-09-20Fix various bugs in vscode extension (#5772)Gravatar JeremyFunk 3-9/+85
2023-09-20add `emitDecoratorMetadata` (#5777)Gravatar Dylan Conway 19-110/+1884
2023-09-20fix(doc): correct `server.reload` (#5799)Gravatar Ai Hoshino 1-1/+1
2023-09-20Call `Error.prepareStackTrace` on `new Error().stack` (#5802)Gravatar Jarred Sumner 6-155/+277
2023-09-20Fixes #5800Gravatar Jarred Sumner 1-1/+1
2023-09-20Fix path used in bunx (#5785)Gravatar Jarred Sumner 2-26/+51
2023-09-20Fix RedirectURLTooLong errors (#5786)Gravatar ggobbe 1-1/+1
2023-09-19Show when a newer version is available in the install screen (#5780)Gravatar Jarred Sumner 3-14/+95
2023-09-19Fix broken linksGravatar Colin McDonnell 4-4/+4