Skip to content

Lesson 6 of 9 · 26m

Testing with `bun test` and Bundling with `bun build`

Adopt Bun's fast test runner and bundler with realistic expectations about Jest compatibility, artifact shape, sourcemaps, and deployment targets.

bun test feels like cheating the first time a large suite starts before your coffee cools. The speed is real. The danger is assuming Jest compatibility means Jest identity, or that a bundle which runs locally is automatically a deployable artifact. Tools fail differently at the edges, and edges are exactly where production lives.

Use the test runner for fast feedback

Bun's test API is intentionally familiar:

import { describe, expect, test, beforeEach } from "bun:test";

describe("invoice totals", () => {
  beforeEach(() => resetInvoices());

  test("applies tax after discount", () => {
    expect(totalCents({ subtotal: 10000, discount: 1000, taxRate: 0.08 })).toBe(9720);
  });
});

Run it directly:

bun test
bun test tests/invoice.test.ts
bun test --coverage

For many projects this replaces a Jest plus ts-jest plus ESM configuration puzzle with one command. But snapshot formats, mocking behavior, module isolation, fake timers, and environment globals can differ. The closer your tests depend on test-framework implementation details, the more migration work you have.

Compatibility tests belong outside the framework edge

Write most assertions against behavior and injected interfaces. Reserve framework-specific mocking for adapters:

test("rejects expired sessions", async () => {
  const auth = new SessionAuth({ now: () => new Date("2026-01-01T00:00:00Z") });
  await expect(auth.validate(expiredToken)).rejects.toThrow("expired");
});

That style travels between Bun, Vitest, and Node's test runner far better than deep mock-registry manipulation.

Bundle deliberately, not accidentally

A basic service bundle looks simple:

bun build src/server.ts   --outdir dist   --target bun   --sourcemap=external   --minify

Each flag is a decision. --target bun assumes a Bun runtime in deployment. --sourcemap=external preserves debugging while keeping sourcemaps separable. --minify reduces artifact size but complicates incident reading. External packages may be bundled or left as imports depending on flags and dependency shape. Inspect the result instead of guessing:

ls -lh dist
head -30 dist/server.js
grep -o 'require("sharp")' dist/server.js || true

Know what bundling cannot fix

Bundling does not typecheck. It does not include arbitrary runtime files unless they are imported in a way the bundler understands. It does not make native addons portable across operating systems. It does not preserve dynamic paths constructed at runtime:

const plugin = await import(`./plugins/${name}.ts`);

That dynamic expression cannot be statically enumerated. Either use an explicit registry or copy plugin files and document the runtime dependency.

Verify the artifact as a black box

bun build src/server.ts --outdir dist --target bun
PORT=4100 bun dist/server.js &
curl -fsS http://127.0.0.1:4100/healthz

Then run the smoke suite against the same artifact in the same container image you ship. Deployment artifacts should not merely compile; they should boot.

Field notes

  • bun test is Jest-like, not a byte-for-byte Jest replacement. Mocks, snapshots, and timers deserve focused migration checks.
  • Keep tsc --noEmit beside bun test; neither replaces the other.
  • Bundling can hide workspace and path-alias mistakes until runtime. Boot the output artifact in CI.
  • Minified production output is fine, but retain external sourcemaps if you expect humans to debug incidents.

Was this lesson helpful?

Sign in to rate