From 286aca0e2cb18925becf6638190573533d95c05d Mon Sep 17 00:00:00 2001 From: Andrei Rusu Date: Wed, 11 Jan 2023 15:26:49 +0100 Subject: [PATCH] added support for writing global test hooks using async/await and without a callback --- lib/api/element-commands/getNextSibling.js | 1 + lib/testsuite/context.js | 6 ++-- lib/testsuite/globals.js | 17 +++++++++- test/extra/external-globals-async.js | 34 +++++++++++++++++++ test/src/runner/testRunWithExternalGlobals.js | 11 ++++++ 5 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 test/extra/external-globals-async.js diff --git a/lib/api/element-commands/getNextSibling.js b/lib/api/element-commands/getNextSibling.js index b21931f7ee..cf54871c85 100644 --- a/lib/api/element-commands/getNextSibling.js +++ b/lib/api/element-commands/getNextSibling.js @@ -20,6 +20,7 @@ const BaseElementCommand = require('./_baseElementCommand.js'); * @moreinfo developer.mozilla.org/en-US/docs/Web/API/Element/nextElementSibling * @since 2.0.0 * @api protocol.elements + * @exampleLink /api/getNextSibling.js */ class GetNextSibling extends BaseElementCommand { diff --git a/lib/testsuite/context.js b/lib/testsuite/context.js index 7db19a825a..3187860ccf 100644 --- a/lib/testsuite/context.js +++ b/lib/testsuite/context.js @@ -420,9 +420,11 @@ class Context extends EventEmitter { */ invokeMethod(fnName, client, expectedArgs, done) { const isES6Async = this.isES6Async(fnName); + const isTestHook = this.isTestHook; + if (client) { client.isES6AsyncTestcase = isES6Async; - client.isES6AsyncTestHook = this.isTestHook ? isES6Async : undefined; + client.isES6AsyncTestHook = isTestHook ? isES6Async : undefined; } const api = client && client.api || null; @@ -438,7 +440,7 @@ class Context extends EventEmitter { switch (expectedArgs) { case 2: case 1: - return this.callAsync({fnName, api, expectedArgs, done, context, isES6Async}); + return this.callAsync({fnName, api, expectedArgs, done, context, isES6Async, isTestHook}); case 0: { try { diff --git a/lib/testsuite/globals.js b/lib/testsuite/globals.js index e22c1eb309..fb60ecc460 100644 --- a/lib/testsuite/globals.js +++ b/lib/testsuite/globals.js @@ -3,6 +3,7 @@ const lodashClone = require('lodash.clone'); const TestHooks = require('./hooks.js'); const Context = require('./context.js'); const Utils = require('../utils'); +const {Logger} = Utils; const PluginLoader = require('../api/_loaders/plugin.js'); class GlobalsContext extends Context { @@ -144,7 +145,21 @@ class GlobalsContext extends Context { }); } - callAsync({fnName, api, expectedArgs = 2, done = function() {}}) { + callAsync({fnName, api, expectedArgs = 2, isTestHook = false, isES6Async, done = function() {}}) { + + if (isES6Async && isTestHook && expectedArgs === 1) { + return this.module[fnName].call(this.module, api) + .then(function(result) { + return done(result); + }) + .catch(function(err) { + Logger.error(err); + + return done(err); + }); + } + + let fnAsync = Utils.makeFnAsync(expectedArgs, this.module[fnName], this.module); let args = this.getHookFnArgs(done, api, expectedArgs); diff --git a/test/extra/external-globals-async.js b/test/extra/external-globals-async.js new file mode 100644 index 0000000000..c2462613fe --- /dev/null +++ b/test/extra/external-globals-async.js @@ -0,0 +1,34 @@ +const assert = require('assert'); + +module.exports = { + 'default': {}, + + async before() { + this.isFungus = true; + }, + + async beforeEach(client) { + expect(this.isFungus).to.be.true; + + return new Promise(resolve => { + setTimeout(resolve, 500); + }); + }, + + async afterEach(client) { + assert.strictEqual(client.globals.isFungus, true); + }, + + async reporter(results) { + assert.ok('modules' in results); + + if (results.lastError instanceof Error) { + throw results.lastError; + } + + assert.strictEqual(this.reporterCount, 0); + assert.strictEqual(this.isFungus, true); + + this.reporterCount++; + } +}; diff --git a/test/src/runner/testRunWithExternalGlobals.js b/test/src/runner/testRunWithExternalGlobals.js index e4398a922e..0735272415 100644 --- a/test/src/runner/testRunWithExternalGlobals.js +++ b/test/src/runner/testRunWithExternalGlobals.js @@ -39,5 +39,16 @@ describe('testRunWithExternalGlobals', function() { })); }); + it('testRun with async external globals', function() { + let testsPath = path.join(__dirname, '../../sampletests/before-after/sampleSingleTest.js'); + const globals = { + reporterCount: 0 + }; + return runTests(testsPath, settings({ + globals, + globals_path: path.join(__dirname, '../../extra/external-globals-async.js'), + output_folder: false + })); + }); });