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:
Milan Burda 2023-02-17 19:32:39 +01:00 committed by GitHub
parent 0de1012280
commit ee87438d28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 22 additions and 21 deletions

View file

@ -62,7 +62,7 @@ describe('BrowserWindow module', () => {
ifit(process.platform === 'linux')('does not crash when setting large window icons', async () => { ifit(process.platform === 'linux')('does not crash when setting large window icons', async () => {
const appPath = path.join(fixtures, 'apps', 'xwindow-icon'); const appPath = path.join(fixtures, 'apps', 'xwindow-icon');
const appProcess = childProcess.spawn(process.execPath, [appPath]); 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 () => { it('does not crash or throw when passed an invalid icon', async () => {

View file

@ -291,7 +291,7 @@ describe('session module', () => {
expect(itemUrl).to.equal(url); expect(itemUrl).to.equal(url);
expect(itemFilename).to.equal('mockFile.txt'); expect(itemFilename).to.equal('mockFile.txt');
// Delay till the next tick. // 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'); expect(() => item.getURL()).to.throw('DownloadItem used after being destroyed');
}); });
}); });

View file

@ -364,7 +364,7 @@ describe('webContents module', () => {
it('resolves when navigating within the page', async () => { it('resolves when navigating within the page', async () => {
await w.loadFile(path.join(fixturesPath, 'pages', 'base-page.html')); 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(); await expect(w.loadURL(w.getURL() + '#foo')).to.eventually.be.fulfilled();
}); });

View file

@ -6,7 +6,7 @@ import { BrowserWindow, WebFrameMain, webFrameMain, ipcMain, app, WebContents }
import { closeAllWindows } from './lib/window-helpers'; import { closeAllWindows } from './lib/window-helpers';
import { emittedOnce, emittedNTimes } from './lib/events-helpers'; import { emittedOnce, emittedNTimes } from './lib/events-helpers';
import { AddressInfo } from 'net'; import { AddressInfo } from 'net';
import { defer, ifit, waitUntil } from './lib/spec-helpers'; import { defer, delay, ifit, waitUntil } from './lib/spec-helpers';
describe('webFrameMain module', () => { describe('webFrameMain module', () => {
const fixtures = path.resolve(__dirname, 'fixtures'); const fixtures = path.resolve(__dirname, 'fixtures');
@ -297,7 +297,7 @@ describe('webFrameMain module', () => {
const { mainFrame } = w.webContents; const { mainFrame } = w.webContents;
w.destroy(); w.destroy();
// Wait for WebContents, and thus RenderFrameHost, to be destroyed. // Wait for WebContents, and thus RenderFrameHost, to be destroyed.
await new Promise(resolve => setTimeout(resolve, 0)); await delay();
expect(() => mainFrame.url).to.throw(); expect(() => mainFrame.url).to.throw();
}); });
@ -338,7 +338,7 @@ describe('webFrameMain module', () => {
w.webContents.forcefullyCrashRenderer(); w.webContents.forcefullyCrashRenderer();
await crashEvent; await crashEvent;
// A short wait seems to be required to reproduce the crash. // 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); await w.webContents.loadURL(crossOriginUrl);
// Log just to keep mainFrame in scope. // Log just to keep mainFrame in scope.
console.log('mainFrame.url', mainFrame.url); console.log('mainFrame.url', mainFrame.url);

View file

@ -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 stdio = appProcess.stdio as unknown as [NodeJS.ReadableStream, NodeJS.WritableStream, NodeJS.WritableStream, NodeJS.WritableStream, NodeJS.ReadableStream];
const pipe = new PipeTransport(stdio[3], stdio[4]); const pipe = new PipeTransport(stdio[3], stdio[4]);
pipe.send({ id: 1, method: 'Browser.close', params: {} }); 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) { async function waitForBadgeCount (value: number) {
let badgeCount = app.getBadgeCount(); let badgeCount = app.getBadgeCount();
while (badgeCount !== value) { while (badgeCount !== value) {
await new Promise(resolve => setTimeout(resolve, 10)); await delay(10);
badgeCount = app.getBadgeCount(); badgeCount = app.getBadgeCount();
} }
return badgeCount; return badgeCount;

View file

@ -1,4 +1,5 @@
import * as walkdir from 'walkdir'; import * as walkdir from 'walkdir';
import { emittedOnce } from './lib/events-helpers';
export async function getFiles (directoryPath: string, { filter = null }: {filter?: ((file: string) => boolean) | null} = {}) { export async function getFiles (directoryPath: string, { filter = null }: {filter?: ((file: string) => boolean) | null} = {}) {
const files: string[] = []; const files: string[] = [];
@ -8,6 +9,6 @@ export async function getFiles (directoryPath: string, { filter = null }: {filte
walker.on('file', (file) => { walker.on('file', (file) => {
if (!filter || filter(file)) { files.push(file); } if (!filter || filter(file)) { files.push(file); }
}); });
await new Promise((resolve) => walker.on('end', resolve)); await emittedOnce(walker, 'end');
return files; return files;
} }

View file

@ -62,9 +62,7 @@ describe('modules support', () => {
ifit(features.isRunAsNodeEnabled())('can be required in node binary', async function () { ifit(features.isRunAsNodeEnabled())('can be required in node binary', async function () {
const child = childProcess.fork(path.join(fixtures, 'module', 'uv-dlopen.js')); const child = childProcess.fork(path.join(fixtures, 'module', 'uv-dlopen.js'));
const exitCode = await new Promise<number | null>(resolve => child.once('exit', (exitCode) => { const [exitCode] = await emittedOnce(child, 'exit');
resolve(exitCode);
}));
expect(exitCode).to.equal(0); expect(exitCode).to.equal(0);
}); });
}); });

View file

@ -104,7 +104,7 @@ describe('node feature', () => {
it('has the electron version in process.versions', async () => { it('has the electron version in process.versions', async () => {
const source = 'process.send(process.versions)'; const source = 'process.send(process.versions)';
const forked = require('child_process').fork('--eval', [source]); 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) expect(message)
.to.have.own.property('electron') .to.have.own.property('electron')
.that.is.a('string') .that.is.a('string')

View file

@ -1774,6 +1774,14 @@ describe('<webview> tag', function () {
describe('<webview>.goForward()', () => { describe('<webview>.goForward()', () => {
useRemoteContext({ webPreferences: { webviewTag: true } }); useRemoteContext({ webPreferences: { webviewTag: true } });
itremote('should work after a replaced history entry', async (fixtures: string) => { 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(); const webview = new WebView();
webview.setAttribute('nodeintegration', 'on'); webview.setAttribute('nodeintegration', 'on');
@ -1782,10 +1790,7 @@ describe('<webview> tag', function () {
document.body.appendChild(webview); document.body.appendChild(webview);
{ {
const [e] = await Promise.all([ const [e] = await waitForEvents(webview, 'ipc-message', 'did-stop-loading');
new Promise<any>(resolve => webview.addEventListener('ipc-message', resolve, { once: true })),
new Promise<void>(resolve => webview.addEventListener('did-stop-loading', resolve, { once: true }))
]);
expect(e.channel).to.equal('history'); expect(e.channel).to.equal('history');
expect(e.args[0]).to.equal(1); expect(e.args[0]).to.equal(1);
expect(webview.canGoBack()).to.be.false(); expect(webview.canGoBack()).to.be.false();
@ -1802,10 +1807,7 @@ describe('<webview> tag', function () {
webview.goBack(); webview.goBack();
{ {
const [e] = await Promise.all([ const [e] = await waitForEvents(webview, 'ipc-message', 'did-stop-loading');
new Promise<any>(resolve => webview.addEventListener('ipc-message', resolve, { once: true })),
new Promise<void>(resolve => webview.addEventListener('did-stop-loading', resolve, { once: true }))
]);
expect(e.channel).to.equal('history'); expect(e.channel).to.equal('history');
expect(e.args[0]).to.equal(2); expect(e.args[0]).to.equal(2);
expect(webview.canGoBack()).to.be.false(); expect(webview.canGoBack()).to.be.false();