How to use addMochaCommands method in Webdriverio

Best JavaScript code snippet using webdriverio-monorepo

Run Webdriverio automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

testInterfaceWrapper.js

Source: testInterfaceWrapper.js Github

copy
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.runTestInFiberContext = exports.wrapTestFunction = exports.runSpec = exports.runHook = void 0;
7
8var _utils = require("../utils");
9
10var _testFnWrapper = require("./testFnWrapper");
11
12const MOCHA_COMMANDS = ['skip', 'only'];
13
14const runHook = function (hookFn, origFn, beforeFn, beforeFnArgs, afterFn, afterFnArgs, cid, repeatTest) {
15  return origFn(function (...hookFnArgs) {
16    return _testFnWrapper.testFnWrapper.call(this, 'Hook', {
17      specFn: hookFn,
18      specFnArgs: (0, _utils.filterSpecArgs)(hookFnArgs)
19    }, {
20      beforeFn,
21      beforeFnArgs
22    }, {
23      afterFn,
24      afterFnArgs
25    }, cid, repeatTest);
26  });
27};
28
29exports.runHook = runHook;
30
31const runSpec = function (specTitle, specFn, origFn, beforeFn, beforeFnArgs, afterFn, afterFnArgs, cid, repeatTest) {
32  return origFn(specTitle, function (...specFnArgs) {
33    return _testFnWrapper.testFnWrapper.call(this, 'Test', {
34      specFn,
35      specFnArgs: (0, _utils.filterSpecArgs)(specFnArgs)
36    }, {
37      beforeFn,
38      beforeFnArgs
39    }, {
40      afterFn,
41      afterFnArgs
42    }, cid, repeatTest);
43  });
44};
45
46exports.runSpec = runSpec;
47
48const wrapTestFunction = function (origFn, isSpec, beforeFn, beforeArgsFn, afterFn, afterArgsFn, cid) {
49  return function (...specArguments) {
50    let retryCnt = typeof specArguments[specArguments.length - 1] === 'number' ? specArguments.pop() : 0;
51    const specFn = typeof specArguments[0] === 'function' ? specArguments.shift() : typeof specArguments[1] === 'function' ? specArguments.pop() : undefined;
52    const specTitle = specArguments[0];
53
54    if (isSpec) {
55      if (specFn) return runSpec(specTitle, specFn, origFn, beforeFn, beforeArgsFn, afterFn, afterArgsFn, cid, retryCnt);
56      return origFn(specTitle);
57    }
58
59    return runHook(specFn, origFn, beforeFn, beforeArgsFn, afterFn, afterArgsFn, cid, retryCnt);
60  };
61};
62
63exports.wrapTestFunction = wrapTestFunction;
64
65const runTestInFiberContext = function (isSpec, beforeFn, beforeArgsFn, afterFn, afterArgsFn, fnName, cid, scope = global) {
66  const origFn = scope[fnName];
67  scope[fnName] = wrapTestFunction(origFn, isSpec, beforeFn, beforeArgsFn, afterFn, afterArgsFn, cid);
68  addMochaCommands(origFn, scope[fnName]);
69};
70
71exports.runTestInFiberContext = runTestInFiberContext;
72
73function addMochaCommands(origFn, newFn) {
74  MOCHA_COMMANDS.forEach(commandName => {
75    if (typeof origFn[commandName] === 'function') {
76      newFn[commandName] = origFn[commandName];
77    }
78  });
79}
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Webdriverio on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)