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 9b25ca3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 81 deletions.
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 = emittedOnce(child, 'message');
expect(msg).to.equal('ok');
});

ifit(process.platform === 'win32')('can be required if electron.exe is renamed', () => {
Expand Down
12 changes: 5 additions & 7 deletions spec-main/node-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,17 +311,15 @@ describe('node feature', () => {
expect(result.status).to.equal(0);
});

ifit(features.isRunAsNodeEnabled())('handles Promise timeouts correctly', (done) => {
ifit(features.isRunAsNodeEnabled())('handles Promise timeouts correctly', async () => {
const scriptPath = path.join(fixtures, 'module', 'node-promise-timer.js');
const child = childProcess.spawn(process.execPath, [scriptPath], {
env: { ELECTRON_RUN_AS_NODE: 'true' }
});
emittedOnce(child, 'exit').then(([code, signal]) => {
expect(code).to.equal(0);
expect(signal).to.equal(null);
child.kill();
done();
});
const [code, signal] = await emittedOnce(child, 'exit');
expect(code).to.equal(0);
expect(signal).to.equal(null);
child.kill();
});

it('performs microtask checkpoint correctly', (done) => {
Expand Down
11 changes: 4 additions & 7 deletions spec-main/webview-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,18 +415,15 @@ describe('<webview> tag', function () {
await emittedOnce(app, 'browser-window-created');
});

it('emits a web-contents-created event', (done) => {
app.on('web-contents-created', function listener (event, contents) {
if (contents.getType() === 'window') {
app.removeListener('web-contents-created', listener);
done();
}
});
it('emits a web-contents-created event', async () => {
loadWebView(w.webContents, {
allowpopups: 'on',
webpreferences: 'nativeWindowOpen=1',
src: `file://${fixtures}/pages/window-open.html`
});

const [, contents] = await emittedOnce(app, 'web-contents-created');
expect(contents.getType()).to.equal('window');
});
});

Expand Down
68 changes: 21 additions & 47 deletions spec/api-web-frame-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,70 +39,51 @@ describe('webFrame module', function () {
childFrameElement.remove();
});

it('executeJavaScript() yields results via a promise and a sync callback', done => {
it('executeJavaScript() yields results via a promise and a sync callback', async () => {
let callbackResult, callbackError;

childFrame
const executeJavaScript = childFrame
.executeJavaScript('1 + 1', (result, error) => {
callbackResult = result;
callbackError = error;
})
.then(
promiseResult => {
expect(promiseResult).to.equal(2);
done();
},
promiseError => {
done(promiseError);
}
);
});

expect(callbackResult).to.equal(2);
expect(callbackError).to.be.undefined();

const promiseResult = await executeJavaScript;
expect(promiseResult).to.equal(2);
});

it('executeJavaScriptInIsolatedWorld() yields results via a promise and a sync callback', done => {
it('executeJavaScriptInIsolatedWorld() yields results via a promise and a sync callback', async () => {
let callbackResult, callbackError;

childFrame
const executeJavaScriptInIsolatedWorld = childFrame
.executeJavaScriptInIsolatedWorld(999, [{ code: '1 + 1' }], (result, error) => {
callbackResult = result;
callbackError = error;
})
.then(
promiseResult => {
expect(promiseResult).to.equal(2);
done();
},
promiseError => {
done(promiseError);
}
);
});

expect(callbackResult).to.equal(2);
expect(callbackError).to.be.undefined();

const promiseResult = await executeJavaScriptInIsolatedWorld;
expect(promiseResult).to.equal(2);
});

it('executeJavaScript() yields errors via a promise and a sync callback', done => {
it('executeJavaScript() yields errors via a promise and a sync callback', async () => {
let callbackResult, callbackError;

childFrame
const executeJavaScript = childFrame
.executeJavaScript('thisShouldProduceAnError()', (result, error) => {
callbackResult = result;
callbackError = error;
})
.then(
promiseResult => {
done(new Error('error is expected'));
},
promiseError => {
expect(promiseError).to.be.an('error');
done();
}
);
});

expect(callbackResult).to.be.undefined();
expect(callbackError).to.be.an('error');

await expect(executeJavaScript).to.eventually.be.rejected('error is expected');
});

// executeJavaScriptInIsolatedWorld is failing to detect exec errors and is neither
Expand All @@ -113,23 +94,16 @@ describe('webFrame module', function () {
// it('executeJavaScriptInIsolatedWorld() yields errors via a promise and a sync callback', done => {
// let callbackResult, callbackError
//
// childFrame
// const executeJavaScriptInIsolatedWorld = childFrame
// .executeJavaScriptInIsolatedWorld(999, [{ code: 'thisShouldProduceAnError()' }], (result, error) => {
// callbackResult = result
// callbackError = error
// })
// .then(
// promiseResult => {
// done(new Error('error is expected'))
// },
// promiseError => {
// expect(promiseError).to.be.an('error')
// done()
// }
// )
// });
//
// expect(callbackResult).to.be.undefined()
// expect(callbackError).to.be.an('error')
//
// expect(executeJavaScriptInIsolatedWorld).to.eventually.be.rejected('error is expected');
// })

it('executeJavaScript(InIsolatedWorld) can be used without a callback', async () => {
Expand Down
21 changes: 6 additions & 15 deletions spec/chromium-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const ChildProcess = require('child_process');
const { ipcRenderer } = require('electron');
const { emittedOnce } = require('./events-helpers');
const { resolveGetters } = require('./expect-helpers');
const { ifdescribe } = require('./spec-helpers');
const features = process._linkedBinding('electron_common_features');

/* Most of the APIs here don't use standard callbacks */
Expand Down Expand Up @@ -46,21 +47,11 @@ describe('chromium feature', () => {
});
});

describe('navigator.geolocation', () => {
before(function () {
if (!features.isFakeLocationProviderEnabled()) {
return this.skip();
}
});

it('returns position when permission is granted', (done) => {
navigator.geolocation.getCurrentPosition((position) => {
expect(position).to.have.a.property('coords');
expect(position).to.have.a.property('timestamp');
done();
}, (error) => {
done(error);
});
ifdescribe(features.isFakeLocationProviderEnabled())('navigator.geolocation', () => {
it('returns position when permission is granted', async () => {
const position = await new Promise((resolve, reject) => navigator.geolocation.getCurrentPosition(resolve, reject));
expect(position).to.have.a.property('coords');
expect(position).to.have.a.property('timestamp');
});
});

Expand Down

0 comments on commit 9b25ca3

Please sign in to comment.