From 5781647974aa6abae46cfb33df2af98a4b0223ac Mon Sep 17 00:00:00 2001 From: Alexey Kuzmin Date: Wed, 20 May 2020 22:18:48 +0200 Subject: [PATCH 1/2] test: call "expect()" on a correct call stack (cherry picked from commit 33d6a99d40eac66efc34a7c9e2f766112f637177) --- spec-main/api-app-spec.ts | 62 ++++++++++++---------------- spec-main/api-browser-window-spec.ts | 23 +++++------ spec-main/api-ipc-spec.ts | 4 +- 3 files changed, 38 insertions(+), 51 deletions(-) diff --git a/spec-main/api-app-spec.ts b/spec-main/api-app-spec.ts index 726096759b9d3..f141910e7fccc 100644 --- a/spec-main/api-app-spec.ts +++ b/spec-main/api-app-spec.ts @@ -10,8 +10,8 @@ import { homedir } from 'os' import split = require('split') import { app, BrowserWindow, Menu, session } from 'electron' import { emittedOnce } from './events-helpers'; -import { closeWindow, closeAllWindows } from './window-helpers'; -import { ifdescribe } from './spec-helpers'; +import { closeWindow, closeAllWindows } from './window-helpers' +import { ifdescribe, ifit } from './spec-helpers' const features = process.electronBinding('features') @@ -382,48 +382,38 @@ describe('app module', () => { afterEach(() => closeWindow(w).then(() => { w = null as any })) - it('should emit browser-window-focus event when window is focused', (done) => { - app.once('browser-window-focus', (e, window) => { - expect(w.id).to.equal(window.id) - done() - }) + it('should emit browser-window-focus event when window is focused', async () => { + const emitted = emittedOnce(app, 'browser-window-focus') w = new BrowserWindow({ show: false }) w.emit('focus') - }) + const [, window] = await emitted + expect(window.id).to.equal(w.id) + }); - it('should emit browser-window-blur event when window is blured', (done) => { - app.once('browser-window-blur', (e, window) => { - expect(w.id).to.equal(window.id) - done() - }) + it('should emit browser-window-blur event when window is blured', async () => { + const emitted = emittedOnce(app, 'browser-window-blur') w = new BrowserWindow({ show: false }) w.emit('blur') - }) + const [, window] = await emitted + expect(window.id).to.equal(w.id) + }); - it('should emit browser-window-created event when window is created', (done) => { - app.once('browser-window-created', (e, window) => { - setImmediate(() => { - expect(w.id).to.equal(window.id) - done() - }) - }) + it('should emit browser-window-created event when window is created', async () => { + const emitted = emittedOnce(app, 'browser-window-created') w = new BrowserWindow({ show: false }) - }) + const [, window] = await emitted + expect(window.id).to.equal(w.id) + }); - it('should emit web-contents-created event when a webContents is created', (done) => { - app.once('web-contents-created', (e, webContents) => { - setImmediate(() => { - expect(w.webContents.id).to.equal(webContents.id) - done() - }) - }) + it('should emit web-contents-created event when a webContents is created', async () => { + const emitted = emittedOnce(app, 'web-contents-created') w = new BrowserWindow({ show: false }) - }) + const [, webContents] = await emitted + expect(webContents.id).to.equal(w.webContents.id) + }); - it('should emit renderer-process-crashed event when renderer crashes', async function() { - // FIXME: re-enable this test on win32. - if (process.platform === 'win32') - return this.skip() + // FIXME: re-enable this test on win32. + ifit(process.platform !== 'win32')('should emit renderer-process-crashed event when renderer crashes', async () => { w = new BrowserWindow({ show: false, webPreferences: { @@ -432,10 +422,10 @@ describe('app module', () => { }) await w.loadURL('about:blank') - const promise = emittedOnce(app, 'renderer-process-crashed') + const emitted = emittedOnce(app, 'renderer-process-crashed') w.webContents.executeJavaScript('process.crash()') - const [, webContents] = await promise + const [, webContents] = await emitted expect(webContents).to.equal(w.webContents) }) diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts index 8f6ce7d590a1e..1cecfe31816c1 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -850,19 +850,18 @@ describe('BrowserWindow module', () => { }) describe('BrowserWindow.setContentSize(width, height)', () => { - it('sets the content size', (done) => { + it('sets the content size', async () => { // NB. The CI server has a very small screen. Attempting to size the window // larger than the screen will limit the window's size to the screen and // cause the test to fail. const size = [456, 567] w.setContentSize(size[0], size[1]) - setImmediate(() => { - const after = w.getContentSize() - expect(after).to.deep.equal(size) - done() - }) - }) - it('works for a frameless window', (done) => { + await new Promise(setImmediate) + const after = w.getContentSize() + expect(after).to.deep.equal(size) + }); + + it('works for a frameless window', async () => { w.destroy() w = new BrowserWindow({ show: false, @@ -872,11 +871,9 @@ describe('BrowserWindow module', () => { }) const size = [456, 567] w.setContentSize(size[0], size[1]) - setImmediate(() => { - const after = w.getContentSize() - expect(after).to.deep.equal(size) - done() - }) + await new Promise(setImmediate) + const after = w.getContentSize() + expect(after).to.deep.equal(size) }) }) diff --git a/spec-main/api-ipc-spec.ts b/spec-main/api-ipc-spec.ts index 3cd78f101596f..70654aa2e328d 100644 --- a/spec-main/api-ipc-spec.ts +++ b/spec-main/api-ipc-spec.ts @@ -44,7 +44,7 @@ describe('ipc module', () => { it('receives a response from an asynchronous handler', async () => { ipcMain.handleOnce('test', async (e: IpcMainInvokeEvent, arg: number) => { expect(arg).to.equal(123) - await new Promise(resolve => setImmediate(resolve)) + await new Promise(setImmediate) return 3 }) const done = new Promise(resolve => ipcMain.once('result', (e, arg) => { @@ -69,7 +69,7 @@ describe('ipc module', () => { it('receives an error from an asynchronous handler', async () => { ipcMain.handleOnce('test', async () => { - await new Promise(resolve => setImmediate(resolve)) + await new Promise(setImmediate) throw new Error('some error') }) const done = new Promise(resolve => ipcMain.once('result', (e, arg) => { From 994547302ef04c52a7eb1b74359b0df07c676e76 Mon Sep 17 00:00:00 2001 From: Aleksei Kuzmin Date: Thu, 21 May 2020 14:36:11 +0200 Subject: [PATCH 2/2] fixup! test: call "expect()" on a correct call stack --- spec-main/api-app-spec.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spec-main/api-app-spec.ts b/spec-main/api-app-spec.ts index f141910e7fccc..b9940cf4a0735 100644 --- a/spec-main/api-app-spec.ts +++ b/spec-main/api-app-spec.ts @@ -9,7 +9,7 @@ import * as path from 'path' import { homedir } from 'os' import split = require('split') import { app, BrowserWindow, Menu, session } from 'electron' -import { emittedOnce } from './events-helpers'; +import { emittedOnce } from './events-helpers' import { closeWindow, closeAllWindows } from './window-helpers' import { ifdescribe, ifit } from './spec-helpers' @@ -388,7 +388,7 @@ describe('app module', () => { w.emit('focus') const [, window] = await emitted expect(window.id).to.equal(w.id) - }); + }) it('should emit browser-window-blur event when window is blured', async () => { const emitted = emittedOnce(app, 'browser-window-blur') @@ -396,21 +396,21 @@ describe('app module', () => { w.emit('blur') const [, window] = await emitted expect(window.id).to.equal(w.id) - }); + }) it('should emit browser-window-created event when window is created', async () => { const emitted = emittedOnce(app, 'browser-window-created') w = new BrowserWindow({ show: false }) const [, window] = await emitted expect(window.id).to.equal(w.id) - }); + }) it('should emit web-contents-created event when a webContents is created', async () => { const emitted = emittedOnce(app, 'web-contents-created') w = new BrowserWindow({ show: false }) const [, webContents] = await emitted expect(webContents.id).to.equal(w.webContents.id) - }); + }) // FIXME: re-enable this test on win32. ifit(process.platform !== 'win32')('should emit renderer-process-crashed event when renderer crashes', async () => {