Skip to main content

reset

Overview​

Clear recorded calls and all request and response override rules while keeping the mock’s interception active. Use it to reuse the mock with its original URL pattern and filters.

Usage​

mock.reset()

Parameters​

This command has no parameters.

Result​

The same mock object, synchronously.

Limitations​

Requires a WebDriver BiDi session (webSocketUrl: true); this method is not provided by the Classic/CDP mock implementation. The examples explicitly subscribe to network events in every session; see the subscription limitation. Always call mock.restore() in finally when the mock is no longer needed.

clear() removes only history and preserves rules. reset() also removes rules, including redirects and aborts. restore() removes interception and makes the mock unusable for new overrides. Reset does not undo a request already sent or a response already delivered.

Example​

Use a local HTTP test site as baseUrl. / serves HTML. /api/items returns JSON with source: "original", the request method in method, and the x-example request header in header (null when absent). /api/other returns the same fields with source: "redirected". The examples use real requests to these endpoints.

const assert = require("node:assert/strict");

it("mock.reset: remove overrides but keep the mock usable", async ({ browser }) => {
await browser.url("/");
await browser.sessionSubscribe({
events: ["network.beforeRequestSent", "network.responseStarted"],
});
const mock = await browser.mock("/api/items");
try {
mock.request({ headers: { "x-example": "overridden" } });
const first = await browser.execute(async () =>
(await fetch("/api/items", { cache: "no-store" })).json(),
);
assert.equal(first.header, "overridden");
await mock.waitForResponse({ timeout: 3000 });
assert.equal(mock.reset(), mock);
assert.equal(mock.calls.length, 0);
const second = await browser.execute(async () =>
(await fetch("/api/items", { cache: "no-store" })).json(),
);
assert.equal(second.header, null);
await mock.waitForResponse({ timeout: 3000 });
assert.equal(mock.calls.length, 1);
mock.respond({ source: "mocked" });
const third = await browser.execute(async () =>
(await fetch("/api/items", { cache: "no-store" })).json(),
);
assert.equal(third.source, "mocked");
mock.reset();
const fourth = await browser.execute(async () =>
(await fetch("/api/items", { cache: "no-store" })).json(),
);
assert.equal(fourth.source, "original");
} finally {
await mock.restore();
}
});

See also​