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:
parent
dcd8224c15
commit
069ca16b9e
4 changed files with 69 additions and 35 deletions
|
@ -4,8 +4,8 @@ import { expect } from 'chai';
|
||||||
|
|
||||||
import * as path from 'node:path';
|
import * as path from 'node:path';
|
||||||
|
|
||||||
import { expectDeprecationMessages } from './lib/deprecate-helpers';
|
|
||||||
import { ifdescribe, ifit, itremote, useRemoteContext } from './lib/spec-helpers';
|
import { ifdescribe, ifit, itremote, useRemoteContext } from './lib/spec-helpers';
|
||||||
|
import { expectDeprecationMessages } from './lib/warning-helpers';
|
||||||
|
|
||||||
describe('nativeImage module', () => {
|
describe('nativeImage module', () => {
|
||||||
const fixturesPath = path.join(__dirname, 'fixtures');
|
const fixturesPath = path.join(__dirname, 'fixtures');
|
||||||
|
|
|
@ -10,6 +10,7 @@ import * as path from 'node:path';
|
||||||
|
|
||||||
import { emittedNTimes, emittedUntil } from './lib/events-helpers';
|
import { emittedNTimes, emittedUntil } from './lib/events-helpers';
|
||||||
import { ifit, listen, waitUntil } from './lib/spec-helpers';
|
import { ifit, listen, waitUntil } from './lib/spec-helpers';
|
||||||
|
import { expectWarningMessages } from './lib/warning-helpers';
|
||||||
import { closeAllWindows, closeWindow, cleanupWebContents } from './lib/window-helpers';
|
import { closeAllWindows, closeWindow, cleanupWebContents } from './lib/window-helpers';
|
||||||
|
|
||||||
const uuid = require('uuid');
|
const uuid = require('uuid');
|
||||||
|
@ -95,14 +96,13 @@ describe('chrome extensions', () => {
|
||||||
it('recognize malformed host permissions', async () => {
|
it('recognize malformed host permissions', async () => {
|
||||||
await w.loadURL(url);
|
await w.loadURL(url);
|
||||||
|
|
||||||
const extPath = path.join(fixtures, 'extensions', 'host-permissions', 'malformed');
|
await expectWarningMessages(
|
||||||
customSession.extensions.loadExtension(extPath);
|
async () => {
|
||||||
|
const extPath = path.join(fixtures, 'extensions', 'host-permissions', 'malformed');
|
||||||
const warning = await new Promise(resolve => { process.on('warning', resolve); });
|
await customSession.extensions.loadExtension(extPath);
|
||||||
|
},
|
||||||
const malformedHost = /URL pattern 'malformed_host' is malformed/;
|
{ name: 'ExtensionLoadWarning', message: /URL pattern 'malformed_host' is malformed/ }
|
||||||
|
);
|
||||||
expect(warning).to.match(malformedHost);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can grant special privileges to urls with host permissions', async () => {
|
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
Add a link
Reference in a new issue