Skip to content

Commit

Permalink
test: don't use expect in async callbacks, use async/await instead
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak committed Jun 26, 2020
1 parent 16a3f41 commit 6528bcf
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 224 deletions.
42 changes: 16 additions & 26 deletions spec-main/api-app-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,21 +209,17 @@ describe('app module', () => {
});

describe('app.requestSingleInstanceLock', () => {
it('prevents the second launch of app', function (done) {
it('prevents the second launch of app', async function () {
this.timeout(120000);
const appPath = path.join(fixturesPath, 'api', 'singleton');
const first = cp.spawn(process.execPath, [appPath]);
first.once('exit', code => {
expect(code).to.equal(0);
});
await emittedOnce(first.stdout, 'data');
// Start second app when received output.
first.stdout.once('data', () => {
const second = cp.spawn(process.execPath, [appPath]);
second.once('exit', code => {
expect(code).to.equal(1);
done();
});
});
const second = cp.spawn(process.execPath, [appPath]);
const [code2] = await emittedOnce(second, 'exit');
expect(code2).to.equal(1);
const [code1] = await emittedOnce(first, 'exit');
expect(code1).to.equal(0);
});

it('passes arguments to the second-instance event', async () => {
Expand Down Expand Up @@ -986,34 +982,28 @@ describe('app module', () => {
}
});

it('does not launch for argument following a URL', done => {
it('does not launch for argument following a URL', async () => {
const appPath = path.join(fixturesPath, 'api', 'quit-app');
// App should exit with non 123 code.
const first = cp.spawn(process.execPath, [appPath, 'electron-test:?', 'abc']);
first.once('exit', code => {
expect(code).to.not.equal(123);
done();
});
const [code] = await emittedOnce(first, 'exit');
expect(code).to.not.equal(123);
});

it('launches successfully for argument following a file path', done => {
it('launches successfully for argument following a file path', async () => {
const appPath = path.join(fixturesPath, 'api', 'quit-app');
// App should exit with code 123.
const first = cp.spawn(process.execPath, [appPath, 'e:\\abc', 'abc']);
first.once('exit', code => {
expect(code).to.equal(123);
done();
});
const [code] = await emittedOnce(first, 'exit');
expect(code).to.equal(123);
});

it('launches successfully for multiple URIs following --', done => {
it('launches successfully for multiple URIs following --', async () => {
const appPath = path.join(fixturesPath, 'api', 'quit-app');
// App should exit with code 123.
const first = cp.spawn(process.execPath, [appPath, '--', 'http://electronjs.org', 'electron-test://testdata']);
first.once('exit', code => {
expect(code).to.equal(123);
done();
});
const [code] = await emittedOnce(first, 'exit');
expect(code).to.equal(123);
});
});

Expand Down
31 changes: 14 additions & 17 deletions spec-main/api-auto-updater-spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { autoUpdater } from 'electron/main';
import { expect } from 'chai';
import { ifit, ifdescribe } from './spec-helpers';
import { emittedOnce } from './events-helpers';

ifdescribe(!process.mas)('autoUpdater module', function () {
describe('checkForUpdates', function () {
ifit(process.platform === 'win32')('emits an error on Windows if the feed URL is not set', function (done) {
autoUpdater.once('error', function (error) {
expect(error.message).to.equal('Update URL is not set');
done();
});
ifit(process.platform === 'win32')('emits an error on Windows if the feed URL is not set', async function () {
const errorEvent = emittedOnce(autoUpdater, 'error');
autoUpdater.setFeedURL({ url: '' });
autoUpdater.checkForUpdates();
const [error] = await errorEvent;
expect(error.message).to.equal('Update URL is not set');
});
});

Expand All @@ -19,11 +19,10 @@ ifdescribe(!process.mas)('autoUpdater module', function () {
expect(autoUpdater.getFeedURL()).to.equal('');
});

ifit(process.platform === 'win32')('correctly fetches the previously set FeedURL', function (done) {
ifit(process.platform === 'win32')('correctly fetches the previously set FeedURL', function () {
const updateURL = 'https://fake-update.electron.io';
autoUpdater.setFeedURL({ url: updateURL });
expect(autoUpdater.getFeedURL()).to.equal(updateURL);
done();
});
});

Expand Down Expand Up @@ -56,12 +55,11 @@ ifdescribe(!process.mas)('autoUpdater module', function () {
});

ifdescribe(process.platform === 'darwin')('on Mac', function () {
it('emits an error when the application is unsigned', done => {
autoUpdater.once('error', function (error) {
expect(error.message).equal('Could not get code signature for running application');
done();
});
it('emits an error when the application is unsigned', async () => {
const errorEvent = emittedOnce(autoUpdater, 'error');
autoUpdater.setFeedURL({ url: '' });
const [error] = await errorEvent;
expect(error.message).equal('Could not get code signature for running application');
});

it('does not throw if default is the serverType', () => {
Expand All @@ -81,12 +79,11 @@ ifdescribe(!process.mas)('autoUpdater module', function () {
});

describe('quitAndInstall', () => {
ifit(process.platform === 'win32')('emits an error on Windows when no update is available', function (done) {
autoUpdater.once('error', function (error) {
expect(error.message).to.equal('No update available, can\'t quit and install');
done();
});
ifit(process.platform === 'win32')('emits an error on Windows when no update is available', async function () {
const errorEvent = emittedOnce(autoUpdater, 'error');
autoUpdater.quitAndInstall();
const [error] = await errorEvent;
expect(error.message).to.equal('No update available, can\'t quit and install');
});
});
});
15 changes: 7 additions & 8 deletions spec-main/api-browser-view-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,16 @@ describe('BrowserView module', () => {
});

describe('window.open()', () => {
it('works in BrowserView', (done) => {
it('works in BrowserView', async () => {
view = new BrowserView();
w.setBrowserView(view);
view.webContents.once('new-window', (e, url, frameName, disposition, options, additionalFeatures) => {
e.preventDefault();
expect(url).to.equal('http://host/');
expect(frameName).to.equal('host');
expect(additionalFeatures[0]).to.equal('this-is-not-a-standard-feature');
done();
});
const newWindow = emittedOnce(view.webContents, 'new-window');
view.webContents.once('new-window', event => event.preventDefault());
view.webContents.loadFile(path.join(fixtures, 'pages', 'window-open.html'));
const [, url, frameName,,, additionalFeatures] = await newWindow;
expect(url).to.equal('http://host/');
expect(frameName).to.equal('host');
expect(additionalFeatures[0]).to.equal('this-is-not-a-standard-feature');
});
});
});
74 changes: 33 additions & 41 deletions spec-main/api-browser-window-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,18 @@ describe('BrowserWindow module', () => {
const v8Util = process._linkedBinding('electron_common_v8_util');
afterEach(closeAllWindows);

it('window does not get garbage collected when opened', (done) => {
it('window does not get garbage collected when opened', async () => {
const w = new BrowserWindow({ show: false });
// Keep a weak reference to the window.
// eslint-disable-next-line no-undef
const wr = new (globalThis as any).WeakRef(w);
setTimeout(() => {
// Do garbage collection, since |w| is not referenced in this closure
// it would be gone after next call if there is no other reference.
v8Util.requestGarbageCollectionForTesting();
await new Promise(resolve => setTimeout(resolve));
// Do garbage collection, since |w| is not referenced in this closure
// it would be gone after next call if there is no other reference.
v8Util.requestGarbageCollectionForTesting();

setTimeout(() => {
expect(wr.deref()).to.not.be.undefined();
done();
});
});
await new Promise(resolve => setTimeout(resolve));
expect(wr.deref()).to.not.be.undefined();
});
});

Expand Down Expand Up @@ -324,30 +321,27 @@ describe('BrowserWindow module', () => {
});
// TODO(deepak1556): The error code now seems to be `ERR_FAILED`, verify what
// changed and adjust the test.
it.skip('should emit did-fail-load event for files that do not exist', (done) => {
w.webContents.on('did-fail-load', (event, code, desc, url, isMainFrame) => {
expect(code).to.equal(-6);
expect(desc).to.equal('ERR_FILE_NOT_FOUND');
expect(isMainFrame).to.equal(true);
done();
});
it.skip('should emit did-fail-load event for files that do not exist', async () => {
const didFailLoad = emittedOnce(w.webContents, 'did-fail-load');
w.loadURL('file://a.txt');
const [, code, desc,, isMainFrame] = await didFailLoad;
expect(code).to.equal(-6);
expect(desc).to.equal('ERR_FILE_NOT_FOUND');
expect(isMainFrame).to.equal(true);
});
it('should emit did-fail-load event for invalid URL', (done) => {
w.webContents.on('did-fail-load', (event, code, desc, url, isMainFrame) => {
expect(desc).to.equal('ERR_INVALID_URL');
expect(code).to.equal(-300);
expect(isMainFrame).to.equal(true);
done();
});
it('should emit did-fail-load event for invalid URL', async () => {
const didFailLoad = emittedOnce(w.webContents, 'did-fail-load');
w.loadURL('http://example:port');
const [, code, desc,, isMainFrame] = await didFailLoad;
expect(desc).to.equal('ERR_INVALID_URL');
expect(code).to.equal(-300);
expect(isMainFrame).to.equal(true);
});
it('should set `mainFrame = false` on did-fail-load events in iframes', (done) => {
w.webContents.on('did-fail-load', (event, code, desc, url, isMainFrame) => {
expect(isMainFrame).to.equal(false);
done();
});
it('should set `mainFrame = false` on did-fail-load events in iframes', async () => {
const didFailLoad = emittedOnce(w.webContents, 'did-fail-load');
w.loadFile(path.join(fixtures, 'api', 'did-fail-load-iframe.html'));
const [,,,, isMainFrame] = await didFailLoad;
expect(isMainFrame).to.equal(false);
});
it('does not crash in did-fail-provisional-load handler', (done) => {
w.webContents.once('did-fail-provisional-load', () => {
Expand All @@ -356,15 +350,14 @@ describe('BrowserWindow module', () => {
});
w.loadURL('http://127.0.0.1:11111');
});
it('should emit did-fail-load event for URL exceeding character limit', (done) => {
w.webContents.on('did-fail-load', (event, code, desc, url, isMainFrame) => {
expect(desc).to.equal('ERR_INVALID_URL');
expect(code).to.equal(-300);
expect(isMainFrame).to.equal(true);
done();
});
it('should emit did-fail-load event for URL exceeding character limit', async () => {
const data = Buffer.alloc(2 * 1024 * 1024).toString('base64');
const didFailLoad = emittedOnce(w.webContents, 'did-fail-load');
w.loadURL(`data:image/png;base64,${data}`);
const [, code, desc,, isMainFrame] = await didFailLoad;
expect(desc).to.equal('ERR_INVALID_URL');
expect(code).to.equal(-300);
expect(isMainFrame).to.equal(true);
});

it('should return a promise', () => {
Expand Down Expand Up @@ -433,12 +426,11 @@ describe('BrowserWindow module', () => {
});
});

it('should support support base url for data urls', (done) => {
ipcMain.once('answer', (event, test) => {
expect(test).to.equal('test');
done();
});
it('should support support base url for data urls', async () => {
const answer = emittedOnce(ipcMain, 'answer');
w.loadURL('data:text/html,<script src="loaded-from-dataurl.js"></script>', { baseURLForDataURL: `other://${path.join(fixtures, 'api')}${path.sep}` });
const [, test] = await answer;
expect(test).to.equal('test');
});
});

Expand Down
38 changes: 18 additions & 20 deletions spec-main/chromium-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ describe('chromium features', () => {

describe('window.open', () => {
for (const show of [true, false]) {
it(`inherits parent visibility over parent {show=${show}} option`, (done) => {
it(`inherits parent visibility over parent {show=${show}} option`, async () => {
const w = new BrowserWindow({ show });

// toggle visibility
Expand All @@ -561,12 +561,11 @@ describe('chromium features', () => {
w.show();
}

w.webContents.once('new-window', (e, url, frameName, disposition, options) => {
expect(options.show).to.equal(w.isVisible());
w.close();
done();
});
const newWindow = emittedOnce(w.webContents, 'new-window');
w.loadFile(path.join(fixturesPath, 'pages', 'window-open.html'));
const [,,,, options] = await newWindow;
expect(options.show).to.equal(w.isVisible());
w.close();
});
}

Expand Down Expand Up @@ -1389,21 +1388,20 @@ describe('iframe using HTML fullscreen API while window is OS-fullscreened', ()
w.loadURL(`data:text/html,${html}`);
});

it('can fullscreen from in-process iframes', done => {
ipcMain.once('fullscreenChange', async () => {
const fullscreenWidth = await w.webContents.executeJavaScript(
"document.querySelector('iframe').offsetWidth"
);
expect(fullscreenWidth > 0).to.true();
it('can fullscreen from in-process iframes', async () => {
const fullscreenChange = emittedOnce(ipcMain, 'fullscreenChange');
w.loadFile(path.join(fixturesPath, 'pages', 'fullscreen-ipif.html'));
await fullscreenChange;

await w.webContents.executeJavaScript('document.exitFullscreen()');
const width = await w.webContents.executeJavaScript(
"document.querySelector('iframe').offsetWidth"
);
expect(width).to.equal(0);
done();
});
const fullscreenWidth = await w.webContents.executeJavaScript(
"document.querySelector('iframe').offsetWidth"
);
expect(fullscreenWidth > 0).to.true();

w.loadFile(path.join(fixturesPath, 'pages', 'fullscreen-ipif.html'));
await w.webContents.executeJavaScript('document.exitFullscreen()');
const width = await w.webContents.executeJavaScript(
"document.querySelector('iframe').offsetWidth"
);
expect(width).to.equal(0);
});
});
9 changes: 4 additions & 5 deletions spec-main/modules-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as fs from 'fs';
import { BrowserWindow } from 'electron/main';
import { ifdescribe, ifit } from './spec-helpers';
import { closeAllWindows } from './window-helpers';
import { emittedOnce } from './events-helpers';
import * as childProcess from 'child_process';

const Module = require('module');
Expand All @@ -23,12 +24,10 @@ describe('modules support', () => {
await expect(w.webContents.executeJavaScript('{ require(\'echo\'); null }')).to.be.fulfilled();
});

ifit(features.isRunAsNodeEnabled())('can be required in node binary', function (done) {
ifit(features.isRunAsNodeEnabled())('can be required in node binary', async function () {
const child = childProcess.fork(path.join(fixtures, 'module', 'echo.js'));
child.on('message', (msg) => {
expect(msg).to.equal('ok');
done();
});
const [msg] = await emittedOnce(child, 'message');
expect(msg).to.equal('ok');
});

ifit(process.platform === 'win32')('can be required if electron.exe is renamed', () => {
Expand Down

0 comments on commit 6528bcf

Please sign in to comment.