variable
test.mock
function mock.clearAllMocks(): void;
Reset all mock function state (calls, results, etc.) without restoring their original implementation.
function mock.module(
id: string,
factory: () => any
): void | Promise<void>;
Replace the module id with the return value of factory.
If the module is already loaded, exports are overwritten with the return value of factory. If an export didn't exist before, it is not added to existing import statements. This is a consequence of how ESM works.
@param id
module ID to mock
@param factory
a function returning an object used as the exports of the mocked module
import { mock } from "bun:test";
mock.module("fs/promises", () => {
return {
readFile: () => Promise.resolve("hello world"),
};
});
import { readFile } from "fs/promises";
console.log(await readFile("hello.txt", "utf8")); // hello worldfunction mock.restore(): void;
Restore the previous value of mocks.