restore
Overview
Removes this mock's network interceptor, clears its history and disables its override rules. It does not reverse effects of requests that already ran, such as cookies stored by the browser. To undo browser emulation instead, use browser.restore.
Await restore() before the next request or the end of the test. To change rules for the same URL in the same BiDi session, use reset on the existing mock rather than restoring it and creating another. In @testplane/webdriverio 9.6.2, listeners from the old mock can still see requests blocked by the new one.
Usage
await mock.restore()
Parameters and result
Takes no arguments. Returns a Promise resolving to the same mock in BiDi and no useful value in Classic/CDP. Use it for cleanup in finally, including when an assertion fails.
Example
The example uses Chrome with WebDriver BiDi. Point baseUrl to a test HTTP server: / serves an HTML page, and /api/items returns JSON with source: "original" and status 200. The explicit event subscription supports new sessions in the same worker in WDIO 9.6.2.
const assert = require("node:assert/strict");
it("mock.restore: remove interception before the next request", async ({ browser }) => {
await browser.url("/");
await browser.sessionSubscribe({
events: ["network.beforeRequestSent", "network.responseStarted"],
});
const mock = await browser.mock("/api/items");
try {
mock.respond({ source: "mocked" });
const body = await browser.execute(async () =>
(await fetch("/api/items", { cache: "no-store" })).json(),
);
assert.equal(body.source, "mocked");
} finally {
await mock.restore();
}
const body = await browser.execute(async () =>
(await fetch("/api/items", { cache: "no-store" })).json(),
);
assert.equal(body.source, "original");
assert.equal(mock.calls.length, 0);
assert.throws(() => mock.respond({ source: "unused" }), /restored mock/);
});
Related commands
References
This page draws on the original WebdriverIO article.