Menu

Post image 1
Post image 2
Post image 3
1 / 3
0

Mocking ESM in 2026: Vitest, Bun, and Node's mock.module

DEV Community·Gabriel Anhaia·27 days ago
#h9BzQPhR
Reading 0:00
15s threshold

Book: TypeScript in Production — Tooling, Build, and Library Authoring Across Runtimes Also by me: The TypeScript Library — the 5-book collection My project: Hermes IDE | GitHub — an IDE for developers who ship with Claude Code and other AI coding tools Me: xgabriel.com | GitHub You open a file called payments.test.ts . The first thing you need to do is replace one function on a module the file under test imports. In CJS this was a one-liner: reassign the property on the require d object and move on. In ESM that line throws. Module namespace exports are read-only. You cannot patch them. You cannot reassign them. The runtime will let you compile, then slap your wrist at the moment the test tries to write. So you reach for the runner. And here the answer depends on which runner you reached for. Vitest has vi.mock . Bun has mock.module . Node 24 has mock.module on the built-in node:test runner, behind no flag now, and a different shape again.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More