test: refactor deprecate-helpers.ts to warning-helpers.ts (#46837)

Add a generic expectWarningMessages and start checking warning names

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: David Sanders <dsanders11@ucsbalum.com>
This commit is contained in:
trop[bot] 2025-04-28 23:39:57 -05:00 committed by GitHub
parent dcd8224c15
commit 069ca16b9e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 69 additions and 35 deletions

View file

@ -4,8 +4,8 @@ import { expect } from 'chai';
import * as path from 'node:path';
import { expectDeprecationMessages } from './lib/deprecate-helpers';
import { ifdescribe, ifit, itremote, useRemoteContext } from './lib/spec-helpers';
import { expectDeprecationMessages } from './lib/warning-helpers';
describe('nativeImage module', () => {
const fixturesPath = path.join(__dirname, 'fixtures');

View file

@ -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.extensions.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.extensions.loadExtension(extPath);
},
{ name: 'ExtensionLoadWarning', message: /URL pattern 'malformed_host' is malformed/ }
);
});
it('can grant special privileges to urls with host permissions', async () => {

View file

@ -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);
}
}

View file

@ -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 }))
);
}