Skip to content

[🐛 Bug]: browser runner does not support mocha's global fixtures and root hooks #10220

@bhootd

Description

@bhootd

Have you read the Contributing Guidelines on issues?

WebdriverIO Version

latest

Node.js Version

16.18.1

Mode

WDIO Testrunner

Which capabilities are you using?

{
        maxInstances: 5,
        browserName: 'chrome',
        acceptInsecureCerts: true,
}

What happened?

Standalone mocha provides global fixtures as well as root hooks. Both can be loaded via mocha config's require option.

wdio's runners also theoretically supports the require config option as mochaOpts.require.

However, while mochaOpts.require works in case of the local runner, the browser runner fails with the following error:

[0-5]  Error:  Test failed due to following error(s):
  - mocha.js: Uncaught TypeError: self[opt] is not a function
    at http://localhost:57323/node_modules/mocha/browser-entry.js:161:18
    at Array.forEach (<anonymous>)
    at Mocha.mocha.setup (http://localhost:57323/node_modules/mocha/browser-entry.js:159:6)
    at new MochaFramework (http://localhost:57323/node_modules/@wdio/browser-runner/build/browser/frameworks/mocha.js?v=b6ac1b03:37:15)
    at http://localhost:57323/node_modules/@wdio/browser-runner/build/browser/frameworks/mocha.js?v=b6ac1b03:236:16

self[opt] here implies an unsupported config key. Similar error can be raised by using a mistyped key like timeot instead of timeout.

Steps

  1. Implement mocha's global fixtures in tests/fixtures.js
export async function mochaGlobalSetup() {
    console.log(`server running on port`);
}
  1. Configure mocha to use tests/fixtures.js through mochaOpts.require in wdio's browser runner config wdio.browser.conf.js:
    mochaOpts: {
        ui: 'bdd',
        timeout: 60000,
        require: ['tests/fixtures.js']
    },
  1. Run the browser runner.
$ DEBUG=true npx wdio run ./wdio.browsr.conf.js
  1. The run fails (see log below) with an error described above.

What is your expected behavior?

The browser runner should read and execute the global fixtures / root hooks provided by mochaOpts.require.

Helper files

Full wdio.browser.config.js:

export const config = {
    injectGlobals: false,
    runner: ['browser', {
        preset: 'react',
        headless: !process.env.DEBUG,
    }],

    specs: [
        './tests/L1/**/*.test.js',
    ],
    exclude: [
    ],
    maxInstances: 10,
    capabilities: [{
        maxInstances: 5,
        browserName: 'chrome',
        acceptInsecureCerts: true,
    }],
    logLevel: 'warn',
    bail: 0,
    baseUrl: '',
    waitforTimeout: 10000,
    connectionRetryTimeout: 120000,
    connectionRetryCount: 3,
    services: [
        'chromedriver',
        'geckodriver',
    ],
    framework: 'mocha',
    reporters: ['dot'],
    mochaOpts: {
        ui: 'bdd',
        timeout: 60000,
        require: ['tests/fixtures.js']
    },
};

Code of Conduct

  • I agree to follow this project's Code of Conduct

Is there an existing issue for this?

  • I have searched the existing issues

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions