From ec4eafecaed91d7c344f1c827ba3d7beeef80f58 Mon Sep 17 00:00:00 2001 From: David Sanders Date: Thu, 1 May 2025 07:53:22 -0700 Subject: [PATCH] test: refactor deprecate-helpers.ts to warning-helpers.ts (#46866) Add a generic expectWarningMessages and start checking warning names --- spec/api-system-preferences-spec.ts | 2 +- spec/extensions-spec.ts | 16 ++++---- spec/lib/deprecate-helpers.ts | 26 ------------- spec/lib/warning-helpers.ts | 60 +++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 35 deletions(-) delete mode 100644 spec/lib/deprecate-helpers.ts create mode 100644 spec/lib/warning-helpers.ts diff --git a/spec/api-system-preferences-spec.ts b/spec/api-system-preferences-spec.ts index 44637669f24c..4ba73d36142b 100644 --- a/spec/api-system-preferences-spec.ts +++ b/spec/api-system-preferences-spec.ts @@ -2,8 +2,8 @@ import { systemPreferences } from 'electron/main'; import { expect } from 'chai'; -import { expectDeprecationMessages } from './lib/deprecate-helpers'; import { ifdescribe } from './lib/spec-helpers'; +import { expectDeprecationMessages } from './lib/warning-helpers'; describe('systemPreferences module', () => { ifdescribe(process.platform === 'win32')('systemPreferences.getAccentColor', () => { diff --git a/spec/extensions-spec.ts b/spec/extensions-spec.ts index 5f4c32650d69..1c95bf622f0c 100644 --- a/spec/extensions-spec.ts +++ b/spec/extensions-spec.ts @@ -10,6 +10,7 @@ import * as path from 'node:path'; import { emittedNTimes, emittedUntil } from './lib/events-helpers'; import { ifit, listen, waitUntil } from './lib/spec-helpers'; +import { expectWarningMessages } from './lib/warning-helpers'; import { closeAllWindows, closeWindow, cleanupWebContents } from './lib/window-helpers'; const uuid = require('uuid'); @@ -95,14 +96,13 @@ describe('chrome extensions', () => { it('recognize malformed host permissions', async () => { await w.loadURL(url); - const extPath = path.join(fixtures, 'extensions', 'host-permissions', 'malformed'); - customSession.loadExtension(extPath); - - const warning = await new Promise(resolve => { process.on('warning', resolve); }); - - const malformedHost = /URL pattern 'malformed_host' is malformed/; - - expect(warning).to.match(malformedHost); + await expectWarningMessages( + async () => { + const extPath = path.join(fixtures, 'extensions', 'host-permissions', 'malformed'); + await customSession.loadExtension(extPath); + }, + { name: 'ExtensionLoadWarning', message: /URL pattern 'malformed_host' is malformed/ } + ); }); it('can grant special privileges to urls with host permissions', async () => { diff --git a/spec/lib/deprecate-helpers.ts b/spec/lib/deprecate-helpers.ts deleted file mode 100644 index 524e656dd56d..000000000000 --- a/spec/lib/deprecate-helpers.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { expect } from 'chai'; - -export async function expectDeprecationMessages (func: () => any, ...expected: string[]) { - const messages: string[] = []; - - const originalWarn = console.warn; - console.warn = (message) => { - messages.push(message); - }; - - const warningListener = (error: Error) => { - messages.push(error.message); - }; - - process.on('warning', warningListener); - - try { - return await func(); - } finally { - // process.emitWarning seems to need us to wait a tick - await new Promise(process.nextTick); - console.warn = originalWarn; - process.off('warning' as any, warningListener); - expect(messages).to.deep.equal(expected); - } -} diff --git a/spec/lib/warning-helpers.ts b/spec/lib/warning-helpers.ts new file mode 100644 index 000000000000..46241117a1f7 --- /dev/null +++ b/spec/lib/warning-helpers.ts @@ -0,0 +1,60 @@ +import { expect } from 'chai'; + +type ExpectedWarningMessage = RegExp | string; + +async function expectWarnings ( + func: () => any, + expected: { name: string, message: ExpectedWarningMessage }[] +) { + const messages: { name: string, message: string }[] = []; + + const originalWarn = console.warn; + console.warn = (message) => { + messages.push({ name: 'Warning', message }); + }; + + const warningListener = (error: Error) => { + messages.push({ name: error.name, message: error.message }); + }; + + process.on('warning', warningListener); + + try { + return await func(); + } finally { + // process.emitWarning seems to need us to wait a tick + await new Promise(process.nextTick); + console.warn = originalWarn; + process.off('warning' as any, warningListener); + expect(messages).to.have.lengthOf(expected.length); + for (const [idx, { name, message }] of messages.entries()) { + expect(name).to.equal(expected[idx].name); + if (expected[idx].message instanceof RegExp) { + expect(message).to.match(expected[idx].message); + } else { + expect(message).to.equal(expected[idx].message); + } + } + } +} + +export async function expectWarningMessages ( + func: () => any, + ...expected: ({ name: string, message: ExpectedWarningMessage } | ExpectedWarningMessage)[] +) { + return expectWarnings(func, expected.map((message) => { + if (typeof message === 'string' || message instanceof RegExp) { + return { name: 'Warning', message }; + } else { + return message; + } + })); +} + +export async function expectDeprecationMessages ( + func: () => any, ...expected: ExpectedWarningMessage[] +) { + return expectWarnings( + func, expected.map((message) => ({ name: 'DeprecationWarning', message })) + ); +}