Run Mocha Tests in Parallel
Run automated tests on multiple browsers in parallel with Mocha JS using BrowserStack Automate.
Introduction
On BrowserStack, you can run multiple Mocha tests at the same time across various browser, device, and OS combinations. This is called Parallel Testing. Parallel Testing gives you the same benefits as running a multi-threaded application.
With Parallel Testing, you can run the same test on different browser/device combinations, namely cross-browser testing, or run different tests on the same or different browser/device combinations. Parallel Testing helps you reduce the run time of your test suite, resulting in faster build times and faster releases.
In this guide, you will learn about:
Prerequisites
If you have already run your first test, you can skip the prerequisites.
- BrowserStack Username and Access key, which you can find in your account settings. If you have not created an account yet, you can sign up for a Free Trial or purchase a plan.
- Node.js installed on your machine.
- Git installed on your machine.
- NPM installed on your machine.
-
Mocha JS installed on your machine using the
npm
command:npm install mocha
Run your first parallel test
- Clone the mocha-browserstack sample repo on GitHub using the following command:
git clone https://github.com/browserstack/mocha-browserstack.git cd mocha-browserstack
- Open the cloned project in the IDE of your choice and install the required dependencies by running the following command in your command line:
npm install
-
Set your BrowserStack credentials and browser/devices where you want to run your test in the
mocha-browserstack/conf/parallel.conf.js
file as follows:parallel.conf.jsvar config = { 'commanCapabilities': { 'browserstack.user': 'YOUR_USERNAME', 'browserstack.key': 'YOUR_ACCESS_KEY', 'build': 'mocha-browserstack', 'name': 'parallel_test', 'browserstack.debug': 'true', }, 'multiCapabilities': [{ 'os': 'Windows', 'os_version': '10', 'browserName': 'Chrome', 'browser_version': 'latest', 'name': 'Parallel test 1' }, { 'os': 'OS X', 'os_version': 'Monterey', 'browserName': 'Chrome', 'browser_version': 'latest', 'name': 'Parallel test 2' }, { 'os' : 'OS X', 'os_version' : 'Big Sur', 'browserName' : 'Safari', 'name': 'Parallel test 3' }, { 'browserName': 'Android', 'device': 'Samsung Galaxy S20', 'realMobile': 'true', 'name': 'Parallel test 4' }, { 'browserName': 'iPhone', 'device': 'iPhone 12 Pro Max', 'realMobile': 'true', 'name': 'Parallel test 5' }] }; exports.capabilities = []; // Code to support common capabilities config.multiCapabilities.forEach(function(caps) { var temp_caps = JSON.parse(JSON.stringify(config.commanCapabilities)); for(var i in caps) temp_caps[i] = caps[i]; exports.capabilities.push(temp_caps); });
- Run your tests in parallel on BrowserStack using the following command:
npm run parallel
- Optionally, you can also choose to run your tests on other environments by specifying them in the
multiCapabilities
key of themocha-browserstack/conf/parallel.conf.js
configuration file as follows:'multiCapabilities': [{ 'browserName': 'Chrome', 'browser_version': 'latest', 'os': 'Windows', 'os_version': '10' },{ 'device': 'iPhone 12 Pro', 'browserName': 'iPhone', 'os_version': '14' },{ 'browserName': 'safari' 'browser_version': 'latest', 'os': 'OS X', 'os_version': 'Big Sur' },{ 'device': 'Samsung Galaxy S20 Ultra', 'browserName': 'Android', 'os_version': '10.0' }]
- View your tests on BrowserStack on the BrowserStack Automate Dashboard.
Next steps
After you have successfully run your first parallel test using BrowserStack, you can explore the following sections:
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
Thank you for your valuable feedback!