variable
test.mock
Reset all mock function state (calls, results, etc.) without restoring their original implementation.
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.
If the module is already loaded and factory returns a promise that is still pending, the exports are overwritten once it settles and the returned promise resolves after that. Otherwise nothing is returned.
module ID to mock
a function returning an object used as the exports of the mocked module. It receives a function that loads the real 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 worldRestore the previous value of mocks.