Skip to content

Chrome DevTools for coding agents

test: run sinon.restore() once in tests/setup.ts (#3240)

Alex Rudenkoea462180beed
diff --git a/AGENTS.md b/AGENTS.md
index fd2cf31c..753e5367 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -48,7 +48,7 @@ This repository contains an MCP server and CLI for Chrome DevTools.
- Use `sinon.assert.calledOnceWithExactly(stub, ...args)` for single calls with exact arguments.
- Use `sinon.assert.calledWithExactly(stub.secondCall, ...args)` for subsequent calls.
- Use `sinon.assert.notCalled(stub)` to assert a method was not invoked.
-- **Clean up stubs**: Always include `afterEach(() => sinon.restore());` in test suites when using Sinon.
+- **Clean up stubs**: `tests/setup.ts` runs `sinon.restore()` after every test. Do not add `afterEach(() => sinon.restore());` to test suites.
### Test Cleanliness
diff --git a/eslint.config.js b/eslint.config.js
index 54f571a3..f74af1af 100644
--- a/eslint.config.js
+++ b/eslint.config.js
@@ -623,6 +623,7 @@ export default defineConfig([
// promises, but we don't need to await them.
'@typescript-eslint/no-floating-promises': 'off',
'@local/enforce-using': 'error',
+ '@local/no-sinon-restore-in-hooks': 'error',
},
},
{
diff --git a/scripts/eslint_rules/local-plugin.js b/scripts/eslint_rules/local-plugin.js
index c2514915..b959c673 100644
--- a/scripts/eslint_rules/local-plugin.js
+++ b/scripts/eslint_rules/local-plugin.js
@@ -9,6 +9,7 @@ import enforceUsingRule from './enforce-using-rule.js';
import enforceZodSchemaRule from './enforce-zod-schema-rule.js';
import requireParsedArgumentsRule from './require-parsed-arguments-rule.js';
import noDirectThirdPartyImportsRule from './no-direct-third-party-imports-rule.js';
+import noSinonRestoreInHooksRule from './no-sinon-restore-in-hooks-rule.js';
export default {
rules: {
@@ -17,5 +18,6 @@ export default {
'no-direct-third-party-imports': noDirectThirdPartyImportsRule,
'enforce-zod-schema': enforceZodSchemaRule,
'require-parsed-arguments': requireParsedArgumentsRule,
+ 'no-sinon-restore-in-hooks': noSinonRestoreInHooksRule,
},
};
diff --git a/scripts/eslint_rules/no-sinon-restore-in-hooks-rule.js b/scripts/eslint_rules/no-sinon-restore-in-hooks-rule.js
new file mode 100644
index 00000000..17110bf5
--- /dev/null
+++ b/scripts/eslint_rules/no-sinon-restore-in-hooks-rule.js
@@ -0,0 +1,103 @@
+/**
+ * @license
+ * Copyright 2026 Google LLC
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+const HOOK_NAMES = new Set(['before', 'beforeEach', 'after', 'afterEach']);
+
+function isSinonRestoreCall(node) {
+ const {callee} = node;
+ return (
+ callee.type === 'MemberExpression' &&
+ callee.object.type === 'Identifier' &&
+ callee.object.name === 'sinon' &&
+ callee.property.type === 'Identifier' &&
+ callee.property.name === 'restore' &&
+ node.arguments.length === 0
+ );
+}
+
+function getEnclosingHook(node) {
+ for (let current = node.parent; current; current = current.parent) {
+ if (
+ current.type === 'CallExpression' &&
+ current.callee.type === 'Identifier' &&
+ HOOK_NAMES.has(current.callee.name)
+ ) {
+ return current;
+ }
+ }
+ return null;
+}
+
+// Returns the node to delete: the whole hook when `sinon.restore()` is its
+// only statement, the statement alone otherwise, or null when no safe fix
+// exists.
+function getRemovalTarget(node, hook) {
+ const callback = hook.arguments[0];
+ const hookStatement = hook.parent;
+ if (
+ hook.arguments.length !== 1 ||
+ hookStatement?.type !== 'ExpressionStatement' ||
+ (callback.type !== 'ArrowFunctionExpression' &&
+ callback.type !== 'FunctionExpression')
+ ) {
+ return null;
+ }
+ if (callback.body === node) {
+ return hookStatement;
+ }
+ const statement = node.parent;
+ if (statement?.type !== 'ExpressionStatement') {
+ return null;
+ }
+ if (
+ callback.body.type === 'BlockStatement' &&
+ callback.body.body.length === 1 &&
+ callback.body.body[0] === statement
+ ) {
+ return hookStatement;
+ }
+ return statement;
+}
+
+export default {
+ name: 'no-sinon-restore-in-hooks',
+ meta: {
+ type: 'suggestion',
+ fixable: 'code',
+ docs: {
+ description:
+ 'Disallow `sinon.restore()` in test hooks. tests/setup.ts restores all Sinon fakes after every test.',
+ },
+ schema: [],
+ messages: {
+ redundantRestore:
+ 'Remove `sinon.restore()` from `{{hook}}`. tests/setup.ts already runs it after every test.',
+ },
+ },
+ defaultOptions: [],
+ create(context) {
+ return {
+ CallExpression(node) {
+ if (!isSinonRestoreCall(node)) {
+ return;
+ }
+ const hook = getEnclosingHook(node);
+ if (!hook) {
+ return;
+ }
+ context.report({
+ node,
+ messageId: 'redundantRestore',
+ data: {hook: hook.callee.name},
+ fix(fixer) {
+ const target = getRemovalTarget(node, hook);
+ return target ? fixer.remove(target) : null;
+ },
+ });
+ },
+ };
+ },
+};
diff --git a/tests/McpContext.test.ts b/tests/McpContext.test.ts
index 362fce8d..73b6c167 100644
--- a/tests/McpContext.test.ts
+++ b/tests/McpContext.test.ts
@@ -8,7 +8,7 @@ import assert from 'node:assert';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import {pathToFileURL} from 'node:url';
import {Locator} from 'puppeteer';
@@ -61,10 +61,6 @@ const EXTENSION_CONTENT_SCRIPT_PATH = path.join(
);
describe('McpContext', () => {
- afterEach(() => {
- sinon.restore();
- });
-
function createPageTarget() {
const page = createMockPuppeteerPage();
page.emulateFocusedPage.resolves();
diff --git a/tests/McpPage.test.ts b/tests/McpPage.test.ts
index e75da8e3..faaea949 100644
--- a/tests/McpPage.test.ts
+++ b/tests/McpPage.test.ts
@@ -5,7 +5,7 @@
*/
import assert from 'node:assert';
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import sinon from 'sinon';
@@ -307,10 +307,6 @@ describe('McpPage', () => {
}
describe('lazy initialization', () => {
- afterEach(() => {
- sinon.restore();
- });
-
it('does not initialize Puppeteer Page in constructor and throws if pptrPage is accessed before init()', async () => {
const pptrPage = createMockPuppeteerPage();
pptrPage.emulateFocusedPage.resolves();
@@ -413,10 +409,6 @@ describe('McpPage', () => {
});
describe('getTitle()', () => {
- afterEach(() => {
- sinon.restore();
- });
-
function createPage(url: string, title: string): McpPage {
const target = createMockPuppeteerTarget({url, title});
return new McpPage(target, 1, {
@@ -476,10 +468,6 @@ describe('McpPage', () => {
});
describe('emulate()', () => {
- afterEach(() => {
- sinon.restore();
- });
-
it('calls emulateNetworkConditions with offline settings', async () => {
const {mcpPage, pptrPage} = await createMcpPage();
await mcpPage.emulate({networkConditions: 'Offline'});
@@ -794,10 +782,6 @@ describe('McpPage', () => {
});
describe('restoreEmulation()', () => {
- afterEach(() => {
- sinon.restore();
- });
-
it('re-applies previously configured viewport emulation', async () => {
const {mcpPage, pptrPage} = await createMcpPage();
await mcpPage.emulate({
@@ -1024,10 +1008,6 @@ describe('McpPage', () => {
});
describe('DevToolsCommentBridge lifecycle', () => {
- afterEach(() => {
- sinon.restore();
- });
-
it('does not create commentBridge on construction or getDevToolsPage', async () => {
const {mcpPage, pptrPage} = await createMcpPage();
pptrPage.hasDevTools.resolves(true);
@@ -1120,10 +1100,6 @@ describe('McpPage', () => {
});
describe('resolveBackendNodeId()', () => {
- afterEach(() => {
- sinon.restore();
- });
-
function createSnapshotWithNodes(
nodes: Array<{id: string; backendNodeId?: number}>,
verbose = false,
@@ -1256,10 +1232,6 @@ describe('McpPage', () => {
return getSelectors(matchedStyles);
}
- afterEach(() => {
- sinon.restore();
- });
-
it('throws when snapshot has not been captured', async () => {
const {mcpPage} = await createMcpPage();
await assert.rejects(
@@ -1432,10 +1404,6 @@ describe('McpPage', () => {
});
describe('executeThirdPartyDeveloperTool()', () => {
- afterEach(() => {
- sinon.restore();
- });
-
it('appends "Tool returned no result." and skips cleanup evaluate when the tool returns undefined and stashed is 0', async () => {
const {mcpPage, pptrPage} = await createMcpPage();
const response = createMockMcpResponse();
@@ -1472,10 +1440,6 @@ describe('McpPage', () => {
});
describe('WebMCP third-party developer tools', () => {
- afterEach(() => {
- sinon.restore();
- });
-
it('filters WebMCP tools by annotations.debugging in discoverThirdPartyDeveloperTools()', async () => {
const {mcpPage, pptrPage} = await createMcpPage();
diff --git a/tests/McpResponse.test.ts b/tests/McpResponse.test.ts
index 1d49f160..6a2b1073 100644
--- a/tests/McpResponse.test.ts
+++ b/tests/McpResponse.test.ts
@@ -7,7 +7,7 @@
import assert from 'node:assert';
import {readFile} from 'node:fs/promises';
import {join} from 'node:path';
-import {after, afterEach, describe, it} from 'node:test';
+import {after, describe, it} from 'node:test';
import sinon from 'sinon';
@@ -91,10 +91,6 @@ after(() => {
});
describe('McpResponse', () => {
- afterEach(() => {
- sinon.restore();
- });
-
it('reports only pages opened by the attached action', async () => {
const openedTarget = createMockPuppeteerTarget({
url: 'https://example.com/opened',
@@ -1170,10 +1166,6 @@ describe('lighthouse', () => {
});
describe('third-party developer tools', () => {
- afterEach(() => {
- sinon.restore();
- });
-
it('lists third-party developer tools', async t => {
const args = createMockParsedArguments({
categoryExperimentalThirdParty: true,
@@ -1346,10 +1338,6 @@ describe('third-party developer tools', () => {
});
describe('webmcp', () => {
- afterEach(() => {
- sinon.restore();
- });
-
async function testIncludesWebmcpTools(
t: it.TestContext,
parseArguments: Partial<ParsedArguments>,
diff --git a/tests/McpServer.test.ts b/tests/McpServer.test.ts
index 47e827b0..2d8239e2 100644
--- a/tests/McpServer.test.ts
+++ b/tests/McpServer.test.ts
@@ -27,7 +27,6 @@ import {createTempFile} from './utils.js';
describe('McpServer', () => {
afterEach(() => {
- sinon.restore();
ClearcutLogger.resetForTesting();
});
diff --git a/tests/McpWorker.test.ts b/tests/McpWorker.test.ts
index 1abea573..29fe8071 100644
--- a/tests/McpWorker.test.ts
+++ b/tests/McpWorker.test.ts
@@ -5,19 +5,13 @@
*/
import assert from 'node:assert';
-import {afterEach, describe, it} from 'node:test';
-
-import sinon from 'sinon';
+import {describe, it} from 'node:test';
import {McpWorker, workerIdPrefix} from '../src/McpWorker.js';
import {createMockTarget, createMockWebWorker} from './mocks.js';
describe('McpWorker', () => {
- afterEach(() => {
- sinon.restore();
- });
-
it('labels ids by worker type', () => {
assert.equal(workerIdPrefix('service_worker'), 'sw');
assert.equal(workerIdPrefix('dedicated_worker'), 'dw');
diff --git a/tests/SlimMcpResponse.test.ts b/tests/SlimMcpResponse.test.ts
index f443f295..27eaa59b 100644
--- a/tests/SlimMcpResponse.test.ts
+++ b/tests/SlimMcpResponse.test.ts
@@ -5,18 +5,12 @@
*/
import assert from 'node:assert';
-import {afterEach, describe, it} from 'node:test';
-
-import sinon from 'sinon';
+import {describe, it} from 'node:test';
import {SlimMcpResponse} from '../src/SlimMcpResponse.js';
import {createMockMcpContext, createMockParsedArguments} from './mocks.js';
describe('SlimMcpResponse', () => {
- afterEach(() => {
- sinon.restore();
- });
-
it('preserves response lines when there is no error', async () => {
const response = new SlimMcpResponse(createMockParsedArguments());
response.appendResponseLine('Navigation complete');
diff --git a/tests/TextSnapshot.test.ts b/tests/TextSnapshot.test.ts
index e66e73c8..7c1970f5 100644
--- a/tests/TextSnapshot.test.ts
+++ b/tests/TextSnapshot.test.ts
@@ -7,8 +7,6 @@
import assert from 'node:assert';
import {afterEach, describe, it} from 'node:test';
-import sinon from 'sinon';
-
import {TextSnapshot} from '../src/TextSnapshot.js';
import type {TextSnapshotNode} from '../src/types.js';
@@ -17,7 +15,6 @@ import {html, withMcpContext} from './utils.js';
describe('TextSnapshot', () => {
afterEach(() => {
- sinon.restore();
TextSnapshot.resetCounter();
});
diff --git a/tests/ToolHandler.test.ts b/tests/ToolHandler.test.ts
index 3b27cde9..a51e2637 100644
--- a/tests/ToolHandler.test.ts
+++ b/tests/ToolHandler.test.ts
@@ -35,7 +35,6 @@ import {getMockBrowser} from './utils.js';
describe('ToolHandler', () => {
afterEach(() => {
- sinon.restore();
ClearcutLogger.resetForTesting();
});
diff --git a/tests/browser.test.ts b/tests/browser.test.ts
index a48993ef..b3911b09 100644
--- a/tests/browser.test.ts
+++ b/tests/browser.test.ts
@@ -7,7 +7,7 @@
import assert from 'node:assert';
import os from 'node:os';
import path from 'node:path';
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import {executablePath} from 'puppeteer';
import sinon from 'sinon';
@@ -58,10 +58,6 @@ async function runWithRetry(fn: () => Promise<void>) {
}
describe('browser', () => {
- afterEach(() => {
- sinon.restore();
- });
-
it('detects display does not crash', () => {
BrowserManager.detectDisplay();
});
diff --git a/tests/collectors/PageCollector.test.ts b/tests/collectors/PageCollector.test.ts
index a314a2c3..27795866 100644
--- a/tests/collectors/PageCollector.test.ts
+++ b/tests/collectors/PageCollector.test.ts
@@ -5,7 +5,7 @@
*/
import assert from 'node:assert';
-import {afterEach, beforeEach, describe, it} from 'node:test';
+import {beforeEach, describe, it} from 'node:test';
import type {
ConsoleMessage,
@@ -431,10 +431,6 @@ describe('ConsoleCollector', () => {
};
});
- afterEach(() => {
- sinon.restore();
- });
-
it('collects issues', async () => {
const browser = getMockBrowser();
const page = (await browser.pages())[0];
diff --git a/tests/collectors/ServiceWorkerCollector.test.ts b/tests/collectors/ServiceWorkerCollector.test.ts
index c7d7ea67..289d3ada 100644
--- a/tests/collectors/ServiceWorkerCollector.test.ts
+++ b/tests/collectors/ServiceWorkerCollector.test.ts
@@ -5,7 +5,7 @@
*/
import assert from 'node:assert';
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import sinon from 'sinon';
@@ -20,10 +20,6 @@ import {
} from '../mocks.js';
describe('ServiceWorkerConsoleCollector', () => {
- afterEach(() => {
- sinon.restore();
- });
-
it('limits logs to 1000 per extension', () => {
const collector = new ServiceWorkerConsoleCollector(undefined, 10);
const extensionId = 'test-extension';
diff --git a/tests/config/ConfigWatcher.test.ts b/tests/config/ConfigWatcher.test.ts
index 26227338..3fa9af0b 100644
--- a/tests/config/ConfigWatcher.test.ts
+++ b/tests/config/ConfigWatcher.test.ts
@@ -6,7 +6,7 @@
import fs from 'node:fs';
import path from 'node:path';
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import sinon from 'sinon';
@@ -87,10 +87,6 @@ async function startWatcher(
}
describe('ConfigWatcher', () => {
- afterEach(() => {
- sinon.restore();
- });
-
it('calls onChange once for several quick writes', async () => {
using dir = createTempDir('cd4a-watcher-');
const configPath = writeConfig(dir.path);
diff --git a/tests/config/mcp-options.test.ts b/tests/config/mcp-options.test.ts
index 757b02fb..601c4669 100644
--- a/tests/config/mcp-options.test.ts
+++ b/tests/config/mcp-options.test.ts
@@ -6,7 +6,7 @@
import assert from 'node:assert';
import fs from 'node:fs';
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import sinon from 'sinon';
@@ -16,8 +16,6 @@ import {DEFAULT_FILESYSTEM_ROOT} from '../../src/config/mcp-options.js';
import {createTempFile} from '../utils.js';
describe('mcp-options steps', () => {
- afterEach(() => sinon.restore());
-
const parser = new ConfigParser('0.0.0');
describe('parseCliArgs', () => {
diff --git a/tests/daemon/utils.test.ts b/tests/daemon/utils.test.ts
index f63b58b1..d8d6b07b 100644
--- a/tests/daemon/utils.test.ts
+++ b/tests/daemon/utils.test.ts
@@ -301,7 +301,6 @@ describe('daemon pid handling', () => {
});
afterEach(() => {
- sinon.restore();
if (savedXdgRuntimeDir === undefined) {
delete process.env['XDG_RUNTIME_DIR'];
} else {
diff --git a/tests/devtools/DevToolsCommentBridge.test.ts b/tests/devtools/DevToolsCommentBridge.test.ts
index fbe26c85..0dcd4d44 100644
--- a/tests/devtools/DevToolsCommentBridge.test.ts
+++ b/tests/devtools/DevToolsCommentBridge.test.ts
@@ -21,7 +21,6 @@ describe('DevToolsCommentBridge', () => {
afterEach(() => {
clock.restore();
- sinon.restore();
});
it('attaches to DevTools page and registers function and evaluation', async () => {
diff --git a/tests/devtools/DevtoolsUtils.test.ts b/tests/devtools/DevtoolsUtils.test.ts
index 345d6cf5..b44390de 100644
--- a/tests/devtools/DevtoolsUtils.test.ts
+++ b/tests/devtools/DevtoolsUtils.test.ts
@@ -5,7 +5,7 @@
*/
import assert from 'node:assert';
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import sinon from 'sinon';
@@ -21,10 +21,6 @@ import {html, withBrowser} from '../utils.js';
describe('createTargetUniverse', () => {
const server = serverHooks();
- afterEach(() => {
- sinon.restore();
- });
-
it('works with a real browser', async () => {
await withBrowser(async (browser, page) => {
const targetUniverse = await createTargetUniverse(
diff --git a/tests/formatters/CommentFormatter.test.ts b/tests/formatters/CommentFormatter.test.ts
index 120ac8cd..fd8f2785 100644
--- a/tests/formatters/CommentFormatter.test.ts
+++ b/tests/formatters/CommentFormatter.test.ts
@@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import sinon from 'sinon';
@@ -15,10 +15,6 @@ import {
import type {CD4ACommentThread} from '../../src/types.js';
describe('CommentFormatter', () => {
- afterEach(() => {
- sinon.restore();
- });
-
function formatterTest(
label: string,
setup: (t: it.TestContext) => CommentFormatter | Promise<CommentFormatter>,
diff --git a/tests/formatters/CssFormatter.test.ts b/tests/formatters/CssFormatter.test.ts
index f4be3e34..d5219177 100644
--- a/tests/formatters/CssFormatter.test.ts
+++ b/tests/formatters/CssFormatter.test.ts
@@ -4,9 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
-import {afterEach, describe, it} from 'node:test';
-import sinon from 'sinon';
-
+import {describe, it} from 'node:test';
import {
type ContainerQuery,
CssFormatter,
@@ -29,10 +27,6 @@ import {
} from '../mocks.js';
describe('CssFormatter', () => {
- afterEach(() => {
- sinon.restore();
- });
-
function formatterTest(
label: string,
setup: (t: it.TestContext) => CssFormatter | Promise<CssFormatter>,
diff --git a/tests/formatters/IssueFormatter.test.ts b/tests/formatters/IssueFormatter.test.ts
index 4b805704..6337130d 100644
--- a/tests/formatters/IssueFormatter.test.ts
+++ b/tests/formatters/IssueFormatter.test.ts
@@ -5,7 +5,7 @@
*/
import assert from 'node:assert';
-import {describe, it, beforeEach, afterEach} from 'node:test';
+import {describe, it, beforeEach} from 'node:test';
import sinon from 'sinon';
@@ -20,10 +20,6 @@ describe('IssueFormatter', () => {
getIssueDescriptionStub = sinon.stub(ISSUE_UTILS, 'getIssueDescription');
});
- afterEach(() => {
- sinon.restore();
- });
-
function formatterTestConcise(
label: string,
setup: (t: it.TestContext) => Promise<IssueFormatter>,
diff --git a/tests/processors/HeapSnapshotManager.test.ts b/tests/processors/HeapSnapshotManager.test.ts
index e8c5a6a6..dc06dcca 100644
--- a/tests/processors/HeapSnapshotManager.test.ts
+++ b/tests/processors/HeapSnapshotManager.test.ts
@@ -5,7 +5,7 @@
*/
import assert from 'node:assert';
-import {describe, it, after, afterEach, before} from 'node:test';
+import {describe, it, after, before} from 'node:test';
import sinon from 'sinon';
@@ -13,10 +13,6 @@ import {HeapSnapshotManager} from '../../src/processors/HeapSnapshotManager.js';
import {DevTools} from '../../src/third_party/index.js';
describe('HeapSnapshotManager', () => {
- afterEach(() => {
- sinon.restore();
- });
-
it('rejects when a file without .heapsnapshot or .heaptimeline extension is passed', async () => {
const manager = new HeapSnapshotManager();
diff --git a/tests/roots.test.ts b/tests/roots.test.ts
index 0dcbd2cb..b77f0584 100644
--- a/tests/roots.test.ts
+++ b/tests/roots.test.ts
@@ -8,7 +8,7 @@ import assert from 'node:assert';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import {pathToFileURL} from 'node:url';
import sinon from 'sinon';
@@ -169,8 +169,6 @@ function stubMissingPaths(...missingPaths: string[]) {
}
describe('McpContext path validation escaping', () => {
- afterEach(() => sinon.restore());
-
async function createContext(): Promise<McpContext> {
const browser = createMockPuppeteerBrowser();
browser.targets.returns([]);
diff --git a/tests/setup.ts b/tests/setup.ts
index 2f01f9db..87838858 100644
--- a/tests/setup.ts
+++ b/tests/setup.ts
@@ -5,11 +5,19 @@
*/
import path from 'node:path';
-import {it} from 'node:test';
+import {afterEach, it} from 'node:test';
+
+import sinon from 'sinon';
// This module runs for every test file, so it must not import src code. Test
// files that use DevTools import tests/setupDevTools.ts.
+// Root-level hooks are inherited by every test in the file, so one hook
+// restores all Sinon stubs, spies, and fake timers after each test.
+afterEach(() => {
+ sinon.restore();
+});
+
// This is run by Node when we execute the tests via the --import flag.
it.snapshot.setResolveSnapshotPath(testPath => {
// By default the snapshots go into the build directory, but we want them
diff --git a/tests/telemetry/ClearcutLogger.test.ts b/tests/telemetry/ClearcutLogger.test.ts
index 74886de4..1f1cb5bf 100644
--- a/tests/telemetry/ClearcutLogger.test.ts
+++ b/tests/telemetry/ClearcutLogger.test.ts
@@ -31,7 +31,6 @@ describe('ClearcutLogger', () => {
});
afterEach(() => {
- sinon.restore();
ClearcutLogger.resetForTesting();
});
diff --git a/tests/telemetry/WatchdogClient.test.ts b/tests/telemetry/WatchdogClient.test.ts
index 81b77657..1637f278 100644
--- a/tests/telemetry/WatchdogClient.test.ts
+++ b/tests/telemetry/WatchdogClient.test.ts
@@ -7,7 +7,7 @@
import assert from 'node:assert';
import {ChildProcess} from 'node:child_process';
import {Writable} from 'node:stream';
-import {describe, it, afterEach, beforeEach} from 'node:test';
+import {describe, it, beforeEach} from 'node:test';
import sinon from 'sinon';
@@ -34,10 +34,6 @@ describe('WatchdogClient', () => {
});
});
- afterEach(() => {
- sinon.restore();
- });
-
it('spawns watchdog process with correct arguments', () => {
new WatchdogClient(
{
diff --git a/tests/telemetry/persistence.test.ts b/tests/telemetry/persistence.test.ts
index c6007e07..89fd3849 100644
--- a/tests/telemetry/persistence.test.ts
+++ b/tests/telemetry/persistence.test.ts
@@ -36,7 +36,6 @@ describe('FilePersistence', () => {
});
afterEach(async () => {
- sinon.restore();
ClearcutLogger.resetForTesting();
});
diff --git a/tests/telemetry/watchdog/ClearcutSender.test.ts b/tests/telemetry/watchdog/ClearcutSender.test.ts
index 99e90f01..518ce4eb 100644
--- a/tests/telemetry/watchdog/ClearcutSender.test.ts
+++ b/tests/telemetry/watchdog/ClearcutSender.test.ts
@@ -39,7 +39,6 @@ describe('ClearcutSender', () => {
randomUUIDStub.restore();
fetchStub.restore();
clock.restore();
- sinon.restore();
});
it('enriches events with app version, os type, and session id', async () => {
diff --git a/tests/tools/comments.test.ts b/tests/tools/comments.test.ts
index 252eaed7..135ba270 100644
--- a/tests/tools/comments.test.ts
+++ b/tests/tools/comments.test.ts
@@ -5,7 +5,7 @@
*/
import assert from 'node:assert';
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import sinon from 'sinon';
@@ -30,10 +30,6 @@ function trackResponseLines(
}
describe('comments tools', () => {
- afterEach(() => {
- sinon.restore();
- });
-
describe('get_devtools_comments', () => {
it('reports error when DevTools window is not open', async t => {
const {page, context, response, args} = createHandlerMocks();
diff --git a/tests/tools/css.test.ts b/tests/tools/css.test.ts
index 2b23abbf..ca3ae350 100644
--- a/tests/tools/css.test.ts
+++ b/tests/tools/css.test.ts
@@ -5,7 +5,7 @@
*/
import assert from 'node:assert';
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import sinon from 'sinon';
@@ -14,10 +14,6 @@ import {getCssStyles} from '../../src/tools/css.js';
import {createHandlerMocks} from '../mocks.js';
describe('get_css_styles', () => {
- afterEach(() => {
- sinon.restore();
- });
-
it('defaults pagination to the first page of 10 rules', () => {
const {args} = createHandlerMocks();
diff --git a/tests/tools/emulation.test.ts b/tests/tools/emulation.test.ts
index ed34cc8b..5cfa94de 100644
--- a/tests/tools/emulation.test.ts
+++ b/tests/tools/emulation.test.ts
@@ -5,7 +5,7 @@
*/
import assert from 'node:assert';
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import sinon from 'sinon';
@@ -18,10 +18,6 @@ import {
import {createHandlerMocks} from '../mocks.js';
describe('emulation', () => {
- afterEach(() => {
- sinon.restore();
- });
-
describe('transforms', () => {
describe('viewportTransform', () => {
it('returns undefined for undefined input', () => {
diff --git a/tests/tools/extensions.test.ts b/tests/tools/extensions.test.ts
index cbca5d0a..6e14319f 100644
--- a/tests/tools/extensions.test.ts
+++ b/tests/tools/extensions.test.ts
@@ -5,7 +5,7 @@
*/
import assert from 'node:assert';
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import sinon from 'sinon';
@@ -19,10 +19,6 @@ import {
import {createHandlerMocks, createMockExtension} from '../mocks.js';
describe('extensions', () => {
- afterEach(() => {
- sinon.restore();
- });
-
describe('install_extension', () => {
it('installs an extension and appends response line', async () => {
const {context, response, args} = createHandlerMocks();
diff --git a/tests/tools/input.test.ts b/tests/tools/input.test.ts
index bfdad378..b0044221 100644
--- a/tests/tools/input.test.ts
+++ b/tests/tools/input.test.ts
@@ -5,7 +5,7 @@
*/
import assert from 'node:assert';
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import sinon from 'sinon';
@@ -38,10 +38,6 @@ import {
describe('input', () => {
const server = serverHooks();
- afterEach(() => {
- sinon.restore();
- });
-
describe('click', () => {
it('clicks', async () => {
await withMcpContext(async (response, context, args) => {
diff --git a/tests/tools/lighthouse.test.ts b/tests/tools/lighthouse.test.ts
index 93fd6f0f..8f5ffa24 100644
--- a/tests/tools/lighthouse.test.ts
+++ b/tests/tools/lighthouse.test.ts
@@ -7,7 +7,7 @@
import assert from 'node:assert/strict';
import fs from 'node:fs/promises';
import path from 'node:path';
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import sinon from 'sinon';
@@ -18,10 +18,6 @@ import {serverHooks} from '../server.js';
import {html, withMcpContext} from '../utils.js';
describe('lighthouse', () => {
- afterEach(() => {
- sinon.restore();
- });
-
const server = serverHooks();
describe('lighthouse_audit', () => {
it('runs Lighthouse audit by default (navigation, desktop)', async () => {
diff --git a/tests/tools/memory.test.ts b/tests/tools/memory.test.ts
index c8653fa0..0a8e191c 100644
--- a/tests/tools/memory.test.ts
+++ b/tests/tools/memory.test.ts
@@ -7,7 +7,7 @@
import assert from 'node:assert';
import {existsSync} from 'node:fs';
import {join} from 'node:path';
-import {describe, it, afterEach} from 'node:test';
+import {describe, it} from 'node:test';
import sinon from 'sinon';
@@ -48,10 +48,6 @@ import {
import {createTempDir, withMcpContext} from '../utils.js';
describe('memory', () => {
- afterEach(() => {
- sinon.restore();
- });
-
describe('take_heapsnapshot', () => {
it('with default options', async () => {
using tmpDir = createTempDir();
diff --git a/tests/tools/network.test.ts b/tests/tools/network.test.ts
index 9de82bcf..8eb14884 100644
--- a/tests/tools/network.test.ts
+++ b/tests/tools/network.test.ts
@@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import sinon from 'sinon';
@@ -15,10 +15,6 @@ import {
import {createHandlerMocks} from '../mocks.js';
describe('network', () => {
- afterEach(() => {
- sinon.restore();
- });
-
describe('list_network_requests', () => {
it('handles default parameters', async () => {
const {page, context, response, args} = createHandlerMocks();
diff --git a/tests/tools/pages.test.ts b/tests/tools/pages.test.ts
index 8b261b4c..afbeab27 100644
--- a/tests/tools/pages.test.ts
+++ b/tests/tools/pages.test.ts
@@ -6,7 +6,7 @@
import assert from 'node:assert';
import path from 'node:path';
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import type {Dialog} from 'puppeteer-core';
import sinon from 'sinon';
@@ -40,10 +40,6 @@ const EXTENSION_SIDE_PANEL_PATH = path.join(
);
describe('pages', () => {
- afterEach(() => {
- sinon.restore();
- });
-
describe('list_pages', () => {
it('list pages', async () => {
const {context, response, args} = createHandlerMocks();
diff --git a/tests/tools/performance.test.ts b/tests/tools/performance.test.ts
index 400330c2..0040561c 100644
--- a/tests/tools/performance.test.ts
+++ b/tests/tools/performance.test.ts
@@ -5,7 +5,7 @@
*/
import assert from 'node:assert';
-import {describe, it, afterEach, beforeEach} from 'node:test';
+import {describe, it, beforeEach} from 'node:test';
import zlib from 'node:zlib';
import sinon from 'sinon';
@@ -26,10 +26,6 @@ import {loadTraceAsBuffer} from '../trace-processing/fixtures/load.js';
import {withMcpContext} from '../utils.js';
describe('performance', () => {
- afterEach(() => {
- sinon.restore();
- });
-
beforeEach(() => {
sinon.stub(globalThis, 'fetch').callsFake(async url => {
const cruxEndpoint =
diff --git a/tests/tools/pwa.test.ts b/tests/tools/pwa.test.ts
index 11f6b90d..61605b08 100644
--- a/tests/tools/pwa.test.ts
+++ b/tests/tools/pwa.test.ts
@@ -5,7 +5,7 @@
*/
import assert from 'node:assert';
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import sinon from 'sinon';
@@ -56,10 +56,6 @@ const MANIFEST = JSON.stringify({
describe('pwa', () => {
const server = serverHooks();
- afterEach(() => {
- sinon.restore();
- });
-
function setupPwaRoutes(): {
manifestId: string;
startUrl: string;
diff --git a/tests/tools/screencast.test.ts b/tests/tools/screencast.test.ts
index 47977b65..0792e7cf 100644
--- a/tests/tools/screencast.test.ts
+++ b/tests/tools/screencast.test.ts
@@ -8,7 +8,7 @@ import assert from 'node:assert';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import sinon from 'sinon';
@@ -37,10 +37,6 @@ function createScreencastMocks() {
}
describe('screencast', () => {
- afterEach(() => {
- sinon.restore();
- });
-
describe('screencast_start', () => {
it('starts an mp4 recording with filePath', async () => {
const {page, context, response, mockRecording, args} =
diff --git a/tests/tools/screenshot.test.ts b/tests/tools/screenshot.test.ts
index 756fa6dc..3222413a 100644
--- a/tests/tools/screenshot.test.ts
+++ b/tests/tools/screenshot.test.ts
@@ -7,7 +7,7 @@
import assert from 'node:assert';
import {stat, chmod, writeFile} from 'node:fs/promises';
import {join} from 'node:path';
-import {describe, it, afterEach} from 'node:test';
+import {describe, it} from 'node:test';
import sinon from 'sinon';
@@ -36,10 +36,6 @@ function pngHeight(data: Buffer): number {
}
describe('screenshot', () => {
- afterEach(() => {
- sinon.restore();
- });
-
describe('downscale clip', () => {
for (const viewport of [{width: 800, height: 600}, null]) {
const mode = viewport ? 'emulated viewport' : 'native viewport';
diff --git a/tests/tools/script.test.ts b/tests/tools/script.test.ts
index f9964163..1dfe1910 100644
--- a/tests/tools/script.test.ts
+++ b/tests/tools/script.test.ts
@@ -8,7 +8,7 @@ import assert from 'node:assert';
import {readFile} from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import {pathToFileURL} from 'node:url';
import sinon from 'sinon';
@@ -36,8 +36,6 @@ const EXTENSION_PATH = path.join(
describe('script', () => {
const server = serverHooks();
- afterEach(() => sinon.restore());
-
describe('browser_evaluate_script', () => {
it('evaluates', async () => {
await withMcpContext(async (response, context, args) => {
diff --git a/tests/tools/slim/tools.test.ts b/tests/tools/slim/tools.test.ts
index 82278ea9..d701030a 100644
--- a/tests/tools/slim/tools.test.ts
+++ b/tests/tools/slim/tools.test.ts
@@ -7,7 +7,7 @@
import assert from 'node:assert';
import fs from 'node:fs';
import path from 'node:path';
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import sinon from 'sinon';
@@ -18,10 +18,6 @@ import {screenshots} from '../../snapshot.js';
import {withMcpContext} from '../../utils.js';
describe('slim', () => {
- afterEach(() => {
- sinon.restore();
- });
-
it('evaluates', async () => {
const {page, context, response, args} = createHandlerMocks();
const script = '2 * 5';
diff --git a/tests/tools/snapshot.test.ts b/tests/tools/snapshot.test.ts
index 94f06268..720ba927 100644
--- a/tests/tools/snapshot.test.ts
+++ b/tests/tools/snapshot.test.ts
@@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import sinon from 'sinon';
@@ -12,10 +12,6 @@ import {takeSnapshot, waitFor} from '../../src/tools/snapshot.js';
import {createHandlerMocks} from '../mocks.js';
describe('snapshot', () => {
- afterEach(() => {
- sinon.restore();
- });
-
describe('take_snapshot', () => {
it('includes a snapshot', async () => {
const {page, context, response, args} = createHandlerMocks();
diff --git a/tests/tools/thirdPartyDeveloper.test.ts b/tests/tools/thirdPartyDeveloper.test.ts
index 0c18bf2d..f8fabb26 100644
--- a/tests/tools/thirdPartyDeveloper.test.ts
+++ b/tests/tools/thirdPartyDeveloper.test.ts
@@ -5,7 +5,7 @@
*/
import assert from 'node:assert';
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import sinon from 'sinon';
@@ -21,10 +21,6 @@ import {createHandlerMocks} from '../mocks.js';
import {getTextContent, withMcpContext} from '../utils.js';
describe('thirdPartyDeveloperTools', () => {
- afterEach(() => {
- sinon.restore();
- });
-
describe('list_3p_developer_tools', () => {
it('lists tools', async () => {
await withMcpContext(
diff --git a/tests/tools/webmcp.test.ts b/tests/tools/webmcp.test.ts
index 8fbe01fe..0040bf37 100644
--- a/tests/tools/webmcp.test.ts
+++ b/tests/tools/webmcp.test.ts
@@ -5,7 +5,7 @@
*/
import assert from 'node:assert';
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import sinon from 'sinon';
@@ -16,10 +16,6 @@ import {createHandlerMocks, createMockWebMCPTool} from '../mocks.js';
import {html, withMcpContext} from '../utils.js';
describe('webmcp', () => {
- afterEach(() => {
- sinon.restore();
- });
-
describe('list_webmcp_tools', () => {
it('list webmcp tools in navigate_page response', async () => {
await withMcpContext(async (response, context, args) => {
diff --git a/tests/trace-processing/parse.test.ts b/tests/trace-processing/parse.test.ts
index fb29bde7..5c8452d6 100644
--- a/tests/trace-processing/parse.test.ts
+++ b/tests/trace-processing/parse.test.ts
@@ -7,7 +7,7 @@
import '../setupDevTools.js';
import assert from 'node:assert';
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import sinon from 'sinon';
@@ -20,10 +20,6 @@ import {DevTools} from '../../src/third_party/index.js';
import {loadTraceAsBuffer} from './fixtures/load.js';
describe('Trace parsing', () => {
- afterEach(() => {
- sinon.restore();
- });
-
it('creates an isolated model instance for each parsed trace', async () => {
const createModelSpy = sinon.spy(
DevTools.TraceEngine.TraceModel.Model,
diff --git a/tests/utils/WaitForHelper.test.ts b/tests/utils/WaitForHelper.test.ts
index fd40a6eb..9c3cddd3 100644
--- a/tests/utils/WaitForHelper.test.ts
+++ b/tests/utils/WaitForHelper.test.ts
@@ -5,7 +5,7 @@
*/
import assert from 'node:assert';
-import {afterEach, describe, it} from 'node:test';
+import {describe, it} from 'node:test';
import sinon from 'sinon';
@@ -21,10 +21,6 @@ import {html, withMcpContext} from '../utils.js';
describe('WaitForHelper', () => {
const server = serverHooks();
- afterEach(() => {
- sinon.restore();
- });
-
it('aborts the action signal when an unhandled dialog opens', async () => {
const pptrPage = createMockPuppeteerPage();
pptrPage.url.returns('https://example.com');
diff --git a/tests/utils/check-for-updates.test.ts b/tests/utils/check-for-updates.test.ts
index eba35d2e..96f6fe30 100644
--- a/tests/utils/check-for-updates.test.ts
+++ b/tests/utils/check-for-updates.test.ts
@@ -27,7 +27,6 @@ describe('checkForUpdates', () => {
});
afterEach(() => {
- sinon.restore();
resetUpdateCheckFlagForTesting();
});