test: call "expect()" on a correct call stack (#23675)

This commit is contained in:
Alexey Kuzmin 2020-05-20 22:18:48 +02:00 committed by GitHub
parent 9d851b8791
commit 33d6a99d40
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 49 deletions

View file

@ -8,7 +8,7 @@ import * as path from 'path';
import { app, BrowserWindow, Menu, session } from 'electron/main'; import { app, BrowserWindow, Menu, session } from 'electron/main';
import { emittedOnce } from './events-helpers'; import { emittedOnce } from './events-helpers';
import { closeWindow, closeAllWindows } from './window-helpers'; import { closeWindow, closeAllWindows } from './window-helpers';
import { ifdescribe } from './spec-helpers'; import { ifdescribe, ifit } from './spec-helpers';
import split = require('split') import split = require('split')
const features = process.electronBinding('features'); const features = process.electronBinding('features');
@ -376,47 +376,38 @@ describe('app module', () => {
afterEach(() => closeWindow(w).then(() => { w = null as any; })); afterEach(() => closeWindow(w).then(() => { w = null as any; }));
it('should emit browser-window-focus event when window is focused', (done) => { it('should emit browser-window-focus event when window is focused', async () => {
app.once('browser-window-focus', (e, window) => { const emitted = emittedOnce(app, 'browser-window-focus');
expect(w.id).to.equal(window.id);
done();
});
w = new BrowserWindow({ show: false }); w = new BrowserWindow({ show: false });
w.emit('focus'); 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) => { it('should emit browser-window-blur event when window is blured', async () => {
app.once('browser-window-blur', (e, window) => { const emitted = emittedOnce(app, 'browser-window-blur');
expect(w.id).to.equal(window.id);
done();
});
w = new BrowserWindow({ show: false }); w = new BrowserWindow({ show: false });
w.emit('blur'); 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) => { it('should emit browser-window-created event when window is created', async () => {
app.once('browser-window-created', (e, window) => { const emitted = emittedOnce(app, 'browser-window-created');
setImmediate(() => {
expect(w.id).to.equal(window.id);
done();
});
});
w = new BrowserWindow({ show: false }); 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) => { it('should emit web-contents-created event when a webContents is created', async () => {
app.once('web-contents-created', (e, webContents) => { const emitted = emittedOnce(app, 'web-contents-created');
setImmediate(() => {
expect(w.webContents.id).to.equal(webContents.id);
done();
});
});
w = new BrowserWindow({ show: false }); 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. // FIXME: re-enable this test on win32.
if (process.platform === 'win32') { return this.skip(); } ifit(process.platform !== 'win32')('should emit renderer-process-crashed event when renderer crashes', async () => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences: {
@ -425,16 +416,15 @@ describe('app module', () => {
}); });
await w.loadURL('about:blank'); await w.loadURL('about:blank');
const promise = emittedOnce(app, 'renderer-process-crashed'); const emitted = emittedOnce(app, 'renderer-process-crashed');
w.webContents.executeJavaScript('process.crash()'); w.webContents.executeJavaScript('process.crash()');
const [, webContents] = await promise; const [, webContents] = await emitted;
expect(webContents).to.equal(w.webContents); expect(webContents).to.equal(w.webContents);
}); });
it('should emit render-process-gone event when renderer crashes', async function () {
// FIXME: re-enable this test on win32. // FIXME: re-enable this test on win32.
if (process.platform === 'win32') { return this.skip(); } ifit(process.platform !== 'win32')('should emit render-process-gone event when renderer crashes', async () => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences: {
@ -443,10 +433,10 @@ describe('app module', () => {
}); });
await w.loadURL('about:blank'); await w.loadURL('about:blank');
const promise = emittedOnce(app, 'render-process-gone'); const emitted = emittedOnce(app, 'render-process-gone');
w.webContents.executeJavaScript('process.crash()'); w.webContents.executeJavaScript('process.crash()');
const [, webContents, details] = await promise; const [, webContents, details] = await emitted;
expect(webContents).to.equal(w.webContents); expect(webContents).to.equal(w.webContents);
expect(details.reason).to.be.oneOf(['crashed', 'abnormal-exit']); expect(details.reason).to.be.oneOf(['crashed', 'abnormal-exit']);
}); });

View file

@ -866,19 +866,18 @@ describe('BrowserWindow module', () => {
}); });
describe('BrowserWindow.setContentSize(width, height)', () => { 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 // 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 // larger than the screen will limit the window's size to the screen and
// cause the test to fail. // cause the test to fail.
const size = [456, 567]; const size = [456, 567];
w.setContentSize(size[0], size[1]); w.setContentSize(size[0], size[1]);
setImmediate(() => { await new Promise(setImmediate);
const after = w.getContentSize(); const after = w.getContentSize();
expect(after).to.deep.equal(size); expect(after).to.deep.equal(size);
done();
}); });
});
it('works for a frameless window', (done) => { it('works for a frameless window', async () => {
w.destroy(); w.destroy();
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
@ -888,11 +887,9 @@ describe('BrowserWindow module', () => {
}); });
const size = [456, 567]; const size = [456, 567];
w.setContentSize(size[0], size[1]); w.setContentSize(size[0], size[1]);
setImmediate(() => { await new Promise(setImmediate);
const after = w.getContentSize(); const after = w.getContentSize();
expect(after).to.deep.equal(size); expect(after).to.deep.equal(size);
done();
});
}); });
}); });

View file

@ -44,7 +44,7 @@ describe('ipc module', () => {
it('receives a response from an asynchronous handler', async () => { it('receives a response from an asynchronous handler', async () => {
ipcMain.handleOnce('test', async (e: IpcMainInvokeEvent, arg: number) => { ipcMain.handleOnce('test', async (e: IpcMainInvokeEvent, arg: number) => {
expect(arg).to.equal(123); expect(arg).to.equal(123);
await new Promise(resolve => setImmediate(resolve)); await new Promise(setImmediate);
return 3; return 3;
}); });
const done = new Promise(resolve => ipcMain.once('result', (e, arg) => { 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 () => { it('receives an error from an asynchronous handler', async () => {
ipcMain.handleOnce('test', async () => { ipcMain.handleOnce('test', async () => {
await new Promise(resolve => setImmediate(resolve)); await new Promise(setImmediate);
throw new Error('some error'); throw new Error('some error');
}); });
const done = new Promise(resolve => ipcMain.once('result', (e, arg) => { const done = new Promise(resolve => ipcMain.once('result', (e, arg) => {