diff --git a/script/spec-runner.js b/script/spec-runner.js index eb6407d409ad..d7d9b8cbfcc8 100755 --- a/script/spec-runner.js +++ b/script/spec-runner.js @@ -98,7 +98,7 @@ async function runElectronTests () { async function runRemoteBasedElectronTests () { let exe = path.resolve(BASE, utils.getElectronExec()) - const runnerArgs = ['electron/spec', ...args._] + const runnerArgs = ['electron/spec', ...args._.slice(2)] if (process.platform === 'linux') { runnerArgs.unshift(path.resolve(__dirname, 'dbus_mock.py'), exe) exe = 'python' @@ -116,7 +116,7 @@ async function runRemoteBasedElectronTests () { async function runMainProcessElectronTests () { const exe = path.resolve(BASE, utils.getElectronExec()) - const { status } = childProcess.spawnSync(exe, ['electron/spec-main', ...args._], { + const { status } = childProcess.spawnSync(exe, ['electron/spec-main', ...args._.slice(2)], { cwd: path.resolve(__dirname, '../..'), stdio: 'inherit' }) diff --git a/spec/events-helpers.js b/spec/events-helpers.js index 4bcbdcabb190..64a4fba447eb 100644 --- a/spec/events-helpers.js +++ b/spec/events-helpers.js @@ -1,3 +1,42 @@ -require('ts-node/register') +/** + * @fileoverview A set of helper functions to make it easier to work + * with events in async/await manner. + */ -module.exports = require('../spec-main/window-helpers') +/** + * @param {!EventTarget} target + * @param {string} eventName + * @return {!Promise} + */ +const waitForEvent = (target, eventName) => { + return new Promise(resolve => { + target.addEventListener(eventName, resolve, { once: true }) + }) +} + +/** + * @param {!EventEmitter} emitter + * @param {string} eventName + * @return {!Promise} With Event as the first item. + */ +const emittedOnce = (emitter, eventName) => { + return emittedNTimes(emitter, eventName, 1).then(([result]) => result) +} + +const emittedNTimes = (emitter, eventName, times) => { + const events = [] + return new Promise(resolve => { + const handler = (...args) => { + events.push(args) + if (events.length === times) { + emitter.removeListener(eventName, handler) + resolve(events) + } + } + emitter.on(eventName, handler) + }) +} + +exports.emittedOnce = emittedOnce +exports.emittedNTimes = emittedNTimes +exports.waitForEvent = waitForEvent