test: use async helpers to simplify tests (#37314)
test: use async helpers to simplify the tests Co-authored-by: Milan Burda <miburda@microsoft.com>
This commit is contained in:
parent
0de1012280
commit
ee87438d28
9 changed files with 22 additions and 21 deletions
|
@ -62,7 +62,7 @@ describe('BrowserWindow module', () => {
|
|||
ifit(process.platform === 'linux')('does not crash when setting large window icons', async () => {
|
||||
const appPath = path.join(fixtures, 'apps', 'xwindow-icon');
|
||||
const appProcess = childProcess.spawn(process.execPath, [appPath]);
|
||||
await new Promise((resolve) => { appProcess.once('exit', resolve); });
|
||||
await emittedOnce(appProcess, 'exit');
|
||||
});
|
||||
|
||||
it('does not crash or throw when passed an invalid icon', async () => {
|
||||
|
|
|
@ -291,7 +291,7 @@ describe('session module', () => {
|
|||
expect(itemUrl).to.equal(url);
|
||||
expect(itemFilename).to.equal('mockFile.txt');
|
||||
// Delay till the next tick.
|
||||
await new Promise<void>(resolve => setImmediate(() => resolve()));
|
||||
await new Promise(setImmediate);
|
||||
expect(() => item.getURL()).to.throw('DownloadItem used after being destroyed');
|
||||
});
|
||||
});
|
||||
|
|
|
@ -364,7 +364,7 @@ describe('webContents module', () => {
|
|||
|
||||
it('resolves when navigating within the page', async () => {
|
||||
await w.loadFile(path.join(fixturesPath, 'pages', 'base-page.html'));
|
||||
await new Promise(resolve => setTimeout(resolve));
|
||||
await delay();
|
||||
await expect(w.loadURL(w.getURL() + '#foo')).to.eventually.be.fulfilled();
|
||||
});
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ import { BrowserWindow, WebFrameMain, webFrameMain, ipcMain, app, WebContents }
|
|||
import { closeAllWindows } from './lib/window-helpers';
|
||||
import { emittedOnce, emittedNTimes } from './lib/events-helpers';
|
||||
import { AddressInfo } from 'net';
|
||||
import { defer, ifit, waitUntil } from './lib/spec-helpers';
|
||||
import { defer, delay, ifit, waitUntil } from './lib/spec-helpers';
|
||||
|
||||
describe('webFrameMain module', () => {
|
||||
const fixtures = path.resolve(__dirname, 'fixtures');
|
||||
|
@ -297,7 +297,7 @@ describe('webFrameMain module', () => {
|
|||
const { mainFrame } = w.webContents;
|
||||
w.destroy();
|
||||
// Wait for WebContents, and thus RenderFrameHost, to be destroyed.
|
||||
await new Promise(resolve => setTimeout(resolve, 0));
|
||||
await delay();
|
||||
expect(() => mainFrame.url).to.throw();
|
||||
});
|
||||
|
||||
|
@ -338,7 +338,7 @@ describe('webFrameMain module', () => {
|
|||
w.webContents.forcefullyCrashRenderer();
|
||||
await crashEvent;
|
||||
// A short wait seems to be required to reproduce the crash.
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
await delay(100);
|
||||
await w.webContents.loadURL(crossOriginUrl);
|
||||
// Log just to keep mainFrame in scope.
|
||||
console.log('mainFrame.url', mainFrame.url);
|
||||
|
|
|
@ -474,7 +474,7 @@ describe('command line switches', () => {
|
|||
const stdio = appProcess.stdio as unknown as [NodeJS.ReadableStream, NodeJS.WritableStream, NodeJS.WritableStream, NodeJS.WritableStream, NodeJS.ReadableStream];
|
||||
const pipe = new PipeTransport(stdio[3], stdio[4]);
|
||||
pipe.send({ id: 1, method: 'Browser.close', params: {} });
|
||||
await new Promise(resolve => { appProcess!.on('exit', resolve); });
|
||||
await emittedOnce(appProcess, 'exit');
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -2658,7 +2658,7 @@ ifdescribe((process.platform !== 'linux' || app.isUnityRunning()))('navigator.se
|
|||
async function waitForBadgeCount (value: number) {
|
||||
let badgeCount = app.getBadgeCount();
|
||||
while (badgeCount !== value) {
|
||||
await new Promise(resolve => setTimeout(resolve, 10));
|
||||
await delay(10);
|
||||
badgeCount = app.getBadgeCount();
|
||||
}
|
||||
return badgeCount;
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import * as walkdir from 'walkdir';
|
||||
import { emittedOnce } from './lib/events-helpers';
|
||||
|
||||
export async function getFiles (directoryPath: string, { filter = null }: {filter?: ((file: string) => boolean) | null} = {}) {
|
||||
const files: string[] = [];
|
||||
|
@ -8,6 +9,6 @@ export async function getFiles (directoryPath: string, { filter = null }: {filte
|
|||
walker.on('file', (file) => {
|
||||
if (!filter || filter(file)) { files.push(file); }
|
||||
});
|
||||
await new Promise((resolve) => walker.on('end', resolve));
|
||||
await emittedOnce(walker, 'end');
|
||||
return files;
|
||||
}
|
||||
|
|
|
@ -62,9 +62,7 @@ describe('modules support', () => {
|
|||
|
||||
ifit(features.isRunAsNodeEnabled())('can be required in node binary', async function () {
|
||||
const child = childProcess.fork(path.join(fixtures, 'module', 'uv-dlopen.js'));
|
||||
const exitCode = await new Promise<number | null>(resolve => child.once('exit', (exitCode) => {
|
||||
resolve(exitCode);
|
||||
}));
|
||||
const [exitCode] = await emittedOnce(child, 'exit');
|
||||
expect(exitCode).to.equal(0);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -104,7 +104,7 @@ describe('node feature', () => {
|
|||
it('has the electron version in process.versions', async () => {
|
||||
const source = 'process.send(process.versions)';
|
||||
const forked = require('child_process').fork('--eval', [source]);
|
||||
const message = await new Promise(resolve => forked.once('message', resolve));
|
||||
const [message] = await emittedOnce(forked, 'message');
|
||||
expect(message)
|
||||
.to.have.own.property('electron')
|
||||
.that.is.a('string')
|
||||
|
|
|
@ -1774,6 +1774,14 @@ describe('<webview> tag', function () {
|
|||
describe('<webview>.goForward()', () => {
|
||||
useRemoteContext({ webPreferences: { webviewTag: true } });
|
||||
itremote('should work after a replaced history entry', async (fixtures: string) => {
|
||||
function waitForEvent (target: EventTarget, event: string) {
|
||||
return new Promise<any>(resolve => target.addEventListener(event, resolve, { once: true }));
|
||||
}
|
||||
|
||||
function waitForEvents (target: EventTarget, ...events: string[]) {
|
||||
return Promise.all(events.map(event => waitForEvent(webview, event)));
|
||||
}
|
||||
|
||||
const webview = new WebView();
|
||||
|
||||
webview.setAttribute('nodeintegration', 'on');
|
||||
|
@ -1782,10 +1790,7 @@ describe('<webview> tag', function () {
|
|||
document.body.appendChild(webview);
|
||||
|
||||
{
|
||||
const [e] = await Promise.all([
|
||||
new Promise<any>(resolve => webview.addEventListener('ipc-message', resolve, { once: true })),
|
||||
new Promise<void>(resolve => webview.addEventListener('did-stop-loading', resolve, { once: true }))
|
||||
]);
|
||||
const [e] = await waitForEvents(webview, 'ipc-message', 'did-stop-loading');
|
||||
expect(e.channel).to.equal('history');
|
||||
expect(e.args[0]).to.equal(1);
|
||||
expect(webview.canGoBack()).to.be.false();
|
||||
|
@ -1802,10 +1807,7 @@ describe('<webview> tag', function () {
|
|||
webview.goBack();
|
||||
|
||||
{
|
||||
const [e] = await Promise.all([
|
||||
new Promise<any>(resolve => webview.addEventListener('ipc-message', resolve, { once: true })),
|
||||
new Promise<void>(resolve => webview.addEventListener('did-stop-loading', resolve, { once: true }))
|
||||
]);
|
||||
const [e] = await waitForEvents(webview, 'ipc-message', 'did-stop-loading');
|
||||
expect(e.channel).to.equal('history');
|
||||
expect(e.args[0]).to.equal(2);
|
||||
expect(webview.canGoBack()).to.be.false();
|
||||
|
|
Loading…
Reference in a new issue