Skip to content

Commit

Permalink
added support for writing global test hooks using async/await and wit…
Browse files Browse the repository at this point in the history
…hout a callback
  • Loading branch information
beatfactor committed Jan 29, 2023
1 parent 54dcf39 commit 286aca0
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/api/element-commands/getNextSibling.js
Expand Up @@ -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 {

Expand Down
6 changes: 4 additions & 2 deletions lib/testsuite/context.js
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
17 changes: 16 additions & 1 deletion lib/testsuite/globals.js
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);

Expand Down
34 changes: 34 additions & 0 deletions 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++;
}
};
11 changes: 11 additions & 0 deletions test/src/runner/testRunWithExternalGlobals.js
Expand Up @@ -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
}));
});
});

0 comments on commit 286aca0

Please sign in to comment.