Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 21 additions & 30 deletions spec-main/api-app-spec.ts
Expand Up @@ -8,7 +8,7 @@ import * as path from 'path';
import { app, BrowserWindow, Menu, session } from 'electron';
import { emittedOnce } from './events-helpers';
import { closeWindow, closeAllWindows } from './window-helpers';
import { ifdescribe } from './spec-helpers';
import { ifdescribe, ifit } from './spec-helpers';
import split = require('split')

const features = process.electronBinding('features');
Expand Down Expand Up @@ -376,47 +376,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: {
Expand All @@ -425,10 +416,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);
});

Expand Down
21 changes: 9 additions & 12 deletions spec-main/api-browser-window-spec.ts
Expand Up @@ -847,19 +847,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();
});
await new Promise(setImmediate);
const after = w.getContentSize();
expect(after).to.deep.equal(size);
});
it('works for a frameless window', (done) => {

it('works for a frameless window', async () => {
w.destroy();
w = new BrowserWindow({
show: false,
Expand All @@ -869,11 +868,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);
});
});

Expand Down
4 changes: 2 additions & 2 deletions spec-main/api-ipc-spec.ts
Expand Up @@ -39,7 +39,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) => {
Expand All @@ -64,7 +64,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) => {
Expand Down