WDIO Mocha plugin is an useful adapter to use Mocha in WDIO. Mocha options can be provided through wdio config file or through command line argument. To invoke WDIO from npm scripts passing command line arguments is tricky.
Mocha Tags are useful to run specific methods tagged by common name. Requirements like running smoke test, remote, local, headless, fast, slow tests can be done using tags. https://github.com/mochajs/mocha/wiki/Tagging
Let say i have a package.json scripts section like below
"scripts" : {
"smoke": "node node\_modules/webdriverio/bin/wdio conf/wdio.conf.js --mochaOpts.grep=@Smoke",
"slow": "node node\_modules/webdriverio/bin/wdio conf/wdio.conf.js --mochaOpts.grep=@Slow",
"fast": "node node\_modules/webdriverio/bin/wdio conf/wdio.conf.js --mochaOpts.grep=@Fast"
}
The above example is straight forward. Smoke, Slow, Fast are tags in your spec file. You could call these scripts as
npm run-script smoke
npm run-script slow
npm run-script fast
If your requirement is to pass the grep from command line not from package.json scripts section, then you could do the following
Your package.json scripts section could be below
"scripts": {
"test-local": "node node\_modules/webdriverio/bin/wdio conf/wdio.conf.js"
}
In Wdio.conf.js you could have mochoOpts section as below
mochaOpts: {
ui: 'bdd',
grep: process.env.npm_config_grep
}
Now you could call from the command line like below
npm run-script --grep=smoke test-local
npm run-script --grep=slow test-local
npm run-script --grep=fast test-local
process.env.npm_config_grep can be read from any javascript file which gives value passed from command line