Skip to main content

Run Mocha Tests in Parallel

Run automated tests on multiple browsers in parallel with Mocha JS using BrowserStack Automate.

Note: Code samples in this guide can be found in the mocha-browserstack sample repo on GitHub.

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:

  1. Prerequisites
  2. Running your first parallel test

Prerequisites

If you have already run your first test, you can skip the prerequisites.

Run your first parallel test

  1. Clone the mocha-browserstack sample repo on GitHub using the following command:

    git clone https://github.com/browserstack/mocha-browserstack.git
    cd mocha-browserstack
    
  2. 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
    
  3. 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.js
     var 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);
     });
    
  4. Run your tests in parallel on BrowserStack using the following command:

     npm run parallel
    
  5. Optionally, you can also choose to run your tests on other environments by specifying them in the multiCapabilities key of the mocha-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'
       }]
    
  6. View your tests on BrowserStack on the BrowserStack Automate Dashboard.
Protip: You can use our capability builder and select from a wide range of custom capabilities that BrowserStack supports.

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






Thank you for your valuable feedback

Is this page helping you?

Yes
No

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!

Talk to an Expert
Talk to an Expert