Skip to content

Bun bug reports — drafted from this migration's actual findings #40056

Open

Description

@sajaqabas-ship-it

How can we reproduce the crash?

Bun bug reports — drafted from this migration's actual findings

Ready to file at [github.com/oven-sh/bun/issues](https://github.com/oven-sh/bun/issues/new). I haven't submitted these — filing them publishes to Bun's public tracker, so say the word if you want me to.


Issue 1: expect(promise).rejects/.resolves hangs indefinitely on a promise rejected by a native-binding client

Likely a duplicate of [#19130](#19130) (same failure shape, different DB client) — worth filing as corroboration with a second reproduction, or commenting directly on that issue instead of opening a new one.

Bun version: 1.4.0 (34cbb9a40)
Platform: Windows 11 (also worth testing on Linux/macOS to see if it's platform-general)

Description
expect(promise).rejects.toThrow(...) (and .resolves.toX(...)) hangs bun test past any --timeout ceiling when the promise settles via a real native-binding client's own error/value object — reproduced independently across two different apps and two different assertion variants (.rejects.toThrow() and .resolves.toBeTruthy()). A plain new Error() rejection through the identical matcher resolves normally in milliseconds.

Minimal reproduction shape:

import { expect, it } from "bun:test";
import Surreal from "surrealdb"; // any client with its own native/WASM binding works

it("hangs", async () => {
  const db = new Surreal();
  await db.connect(/* ... */);
  // A write that a DB-side guard is expected to reject:
  const promise = db.query("CREATE thing SET x = 1"); // guarded to throw server-side
  await expect(promise).rejects.toThrow(/expected message/); // hangs past any timeout
});

Expected: the assertion resolves (pass or fail) within the promise's real settle time, same as under Vitest with the identical code.
Actual: hangs indefinitely; --timeout=N has no effect. Confirmed via baseline comparison — the exact same code path completed in 787ms under Vitest 5.0.0-rc.2, hung past 200s under bun test.
Workaround in use: replace with try { await promise; expect.unreachable(); } catch (err) { expect(String(err)).toMatch(...); }.


Issue 2: toMatchSnapshot() OOM-crashes when snapshotting a live/rendered DOM node

Bun version: 1.4.0
Platform: Windows 11, happy-dom as the DOM environment (also reproduced with a vi.spyOn-mocked canvas.getContext present on the node)

Description
expect(node).toMatchSnapshot() fails with a Bun-level memory-allocation crash (memory allocation of 30064771072 bytes failed, ~30GB) when node is a live, React-rendered DOM element, rather than producing a snapshot or a normal serialization error. Reproduced across three independent codebases in this migration, isolated down to a minimal repro: a bare document.createElement() node snapshots fine; the SAME node after being rendered into by React hangs/OOMs.

Minimal reproduction shape:

import { expect, it } from "bun:test";
import { render } from "@testing-library/react";

it("OOMs", () => {
  const { container } = render(<SomeComponent />);
  expect(container.firstChild).toMatchSnapshot(); // memory allocation of ~30GB failed
});

Root cause (as far as we could determine without Bun's source): the snapshot serializer appears to walk the node's circular references (parentNode/ownerDocument/defaultView, and/or React's fiber expando properties) without cycle detection, unlike Vitest's pretty-format which carries a dedicated DOM plugin.

Expected: either a working DOM-aware snapshot (matching Vitest's pretty-format behavior), or a clean, documented error — not an unbounded memory-allocation crash.
Actual: hard OOM crash, no diagnosable stack trace pointing at the cause.
Workaround in use, and it's a good one — worth Bun adopting as the documented pattern: snapshot node.outerHTML (a plain string) instead of the live node. Verified across 44 test files: identical captured markup, zero crashes. Bun's own snapshot docs already show a React example serializing innerHTML rather than a live node — this bug is what happens when a user does the more natural thing and passes the node directly instead.


Issue 3: Bun.plugin().module() cannot override a Bun builtin module (e.g. bun:sqlite)

Bun version: 1.4.0
Platform: Windows 11

Description
Bun.plugin({ setup(build) { build.module("bun:sqlite", () => ({...})) } }) throws at registration time when the target specifier is a Bun builtin, rather than either working or failing with a documented, discoverable error.

Minimal reproduction shape:

import { plugin } from "bun";

plugin({
  name: "override-sqlite",
  setup(build) {
    build.module("bun:sqlite", () => ({
      exports: { Database: class FakeDatabase {} },
      loader: "object",
    }));
  },
});

import { Database } from "bun:sqlite"; // still resolves to the REAL bun:sqlite

Expected: either full support for overriding builtins (useful and expected for test isolation — this is a completely standard Jest/Vitest moduleNameMapper/vi.mock pattern applied to a built-in), or an early, clear, documented error at plugin-registration time saying builtins can't be intercepted, rather than the runtime error surfacing wherever the builtin happens to get imported: "module() cannot be used to override builtin module".
Actual: the error IS produced, but only reactively wherever the real import happens, and nothing in the Bun.plugin() docs states this limitation up front — a user has to hit it empirically.
Workaround in use: add an env-var-driven path/config override inside the actual source that constructs the builtin connection, and point it at a throwaway location during tests, rather than trying to intercept the module.


Issue 4 (feature request, not a bug): document the real vi compatibility surface

Description
Bun's mocking docs list vi.fn, vi.spyOn, vi.mock, vi.restoreAllMocks, vi.resetAllMocks, vi.clearAllMocks as supported. In practice, migrating ~500 real Vitest test files this session, we also needed and had to hand-build workarounds for: vi.importActual/importOriginal (no replacement exists at all), vi.hoisted (unneeded, bun:test doesn't hoist), vi.resetModules (cache-busting dynamic import), vi.stubGlobal/unstubAllGlobals (manual globalThis save/restore), vi.setSystemTime/useFakeTimers (bun:test DOES have setSystemTime() + jest.useFakeTimers(), just undocumented as the vitest-migration path, and has no advanceTimersByTime equivalent at all).

Ask: a single "migrating from Vitest" compatibility table in the docs, covering exactly this — even just "not supported, no workaround" is more useful than silence, since right now each gap is discovered by a hang, a crash, or a runtime TypeError.

Relevant log output

Stack Trace (bun.report)

I don't have full raw stack traces for these — the subagents that found them reported the key error message text, not the complete captured output, so I won't fabricate a trace I don't actually have. Here's what to put per issue, honestly:

Issue 1 (.rejects/.resolves hang): Leave it blank or write "N/A — the process hangs with no exception thrown, nothing to trace." That's the whole bug: no error, no crash, just an unresolved promise. If Bun's template requires something, paste the exact command + how you killed it (e.g. bun test --timeout=60000 ... then a SIGKILL after 200s).

Issue 2 (snapshot OOM): The only text we captured is the one-line Bun-engine message:

memory allocation of 30064771072 bytes failed

This is likely all there is — it reads like a native allocator abort, not a JS-level exception with a catchable stack. Put that line in, and note "engine-level abort, no JS stack surfaced."

Issue 3 (plugin builtin override): Same situation — the captured text is the single-line error:

module() cannot be used to override builtin module

Activity

  1. robobun commented on Aug 22, 2026

    @robobun
    Collaborator

    Reproduced the snapshot report (number 2 in this list). expect(node).toMatchSnapshot() on a React-rendered happy-dom tree blows up exponentially. The serializer walks ownerDocument and sibling links, and its cycle check only covers the current recursion path, so shared nodes serialize again for every route to them. A list with 8 items already needs roughly 40 GB, which matches the reported 30 GB allocation failure.

    A fix is already open: #35712 serializes DOM nodes as markup, like the pretty-format DOMElement plugin in Jest.

    The other reports: number 1 looks like #19130, as you note. Number 3 already throws at plugin registration time on current canary. Number 4 is a docs request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions