test: refactor deprecate-helpers.ts to warning-helpers.ts (#46808)
Add a generic expectWarningMessages and start checking warning names
This commit is contained in:
parent
27f6adf8df
commit
0c103f390c
4 changed files with 69 additions and 35 deletions
|
@ -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');
|
||||
|
|
|
@ -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 () => {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
60
spec/lib/warning-helpers.ts
Normal file
60
spec/lib/warning-helpers.ts
Normal 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 }))
|
||||
);
|
||||
}
|
Loading…
Add table
Reference in a new issue