mockzilla
A mocking toolkit leveraging the power of TypeScript to enhance your jest experience.
Painless
Writing mocks should be simple and fun. With mockzilla you can skip writing manual mocks of deeply nested APIs and focus on writing tests.
Typesafe
You use TypeScript to have code-completion and type-safety? Great! mockzilla uses the types you have to ensure a pleasant experience!
Good Errors
Errors should point to where the errors come from. mockzilla will give you hints on where you expected calls and where they actually happened.
This is a Work In Progress! The API might change before version 1.0 is released.
# Features
- Deep Mocking
- Mock Assimilation (replace methods of an existing object with mocks)
- Time Manipulation
- Property protection & whitelisting
# Example: Web-Extensions
This is an example of how a deep mock with mockzilla looks like:
import type { Browser } from "webextension-polyfill";
import { deepMock } from "mockzilla";
const [browser, mockBrowser, mockBrowserNode] = deepMock<Browser>("browser", false);
jest.mock("webextension-polyfill", () => browser);
describe("Web-Extension Helpers", () => {
beforeEach(() => mockBrowserNode.enable());
afterEach(() => mockBrowserNode.verifyAndDisable());
describe("getActiveTabs()", () => {
it("should return active tabs", async () => {
const tabs: any[] = [{id: 1}, {id: 2}];
mockBrowser.tabs.query.expect({ active: true }).andResolve(tabs);
expect(await getActiveTabs()).toEqual(tabs);
});
});
describe("onBeforeRedirect()", () => {
it("should register a listener and return a handle to remove the listener again", () => {
const listener = jest.fn();
mockBrowser.webRequest.onBeforeRedirect.addListener.expect(listener, expect.anything());
const removeListener = onBeforeRedirect(listener);
mockBrowser.webRequest.onBeforeRedirect.removeListener.expect(listener);
removeListener();
});
});
});