Skip to content

Vitest module mocking does not work with the Bun runtime #9031

@nazarhussain

Description

@nazarhussain

Describe the bug

Starting Vitest 4.0.7+ it works well with the Bun runtime. Tried out with different configurations and setup and that works fine.

Except the Module Mocking, which always end up loading the actual version of the module instead of mocked.

Reproduction

// my_modules/sum.js
export function sum (a, b) {
    return a + b;
}
// sum.test.js

import {describe, it, vi, expect} from "vitest";
import {sum} from "./my_modules/sum.js";

vi.mock("./my_modules/sum.js")

describe("sum", () => {
   it("add up", () => {
         sum.mockReturnVaue(10);

         expect(sum(9, 8)).toEqual(10);
   });
});

When executed in NodeJS it works fine and sum import would be a mock. But when executed in Bun the sum returns is actual function not mock.

System Info

System:
    OS: macOS 26.1
    CPU: (11) arm64 Apple M3 Pro
    Memory: 212.30 MB / 18.00 GB
    Shell: 5.9 - /opt/homebrew/bin/zsh
  Binaries:
    Node: 22.13.0 - /Users/nazar/.asdf/installs/nodejs/22.13.0/bin/node
    Yarn: 1.22.22 - /Users/nazar/.asdf/installs/nodejs/22.13.0/bin/yarn
    npm: 10.9.2 - /Users/nazar/.asdf/plugins/nodejs/shims/npm
    pnpm: 10.13.1 - /Users/nazar/.bun/bin/pnpm
    bun: 1.3.1 - /Users/nazar/.asdf/shims/bun
    Deno: 2.2.8 - /Users/nazar/.asdf/shims/deno
  Browsers:
    Brave Browser: 142.1.84.135
    Edge: 140.0.3485.54
    Safari: 26.1

Used Package Manager

yarn

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions