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 the local example site as baseUrl. /api/items returns JSON with source: "original" and the received x-example header in header (null if absent). The test resets the same mock between requests.

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​