Run JestJS Tests in Parallel
Run automated tests on multiple browsers in parallel with JestJS using BrowserStack Automate.
The sample test script in this section is compatible with JSON wire protocol-based client bindings. Check out our W3C-based scripts in the selenium-4 branch of the repository.
Introduction
On BrowserStack, you can run multiple JestJS 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.
Run sample test in parallel
- Clone the jest-js-browserstack sample repository on GitHub, using the following command:
git clone https://github.com/browserstack/jest-js-browserstack.git cd jest-js-browserstack
- Install the required dependencies in the sample repository 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
jest-js-browserstack/conf/parallel.conf.js
file as follows:conf/parallel.conf.jsrequire("dotenv").config(); const username = process.env.BROWSERSTACK_USERNAME || ""; const accessKey = process.env.BROWSERSTACK_ACCESS_KEY || ""; const buildName = process.env.BROWSERSTACK_BUILD_NAME || `jest-browserstack Dt - ${Date.now()}`; module.exports = [ { "bstack:options": { os: "Windows", osVersion: "11", local: "false", seleniumVersion: "4.1.0", projectName: "BStack Demo", buildName: buildName, sessionName: "parallel test - Chrome latest", userName: username, accessKey: accessKey, }, browserName: "Chrome", browserVersion: "latest", }, { "bstack:options": { os: "Windows", osVersion: "11", local: "false", seleniumVersion: "4.1.0", projectName: "BStack Demo", buildName: buildName, sessionName: "parallel test - Chrome latest-1", userName: username, accessKey: accessKey, }, browserName: "Chrome", browserVersion: "latest-1", }, { "bstack:options": { os: "OS X", local: "false", seleniumVersion: "4.1.0", projectName: "BStack Demo", buildName: buildName, sessionName: "parallel test - Safari latest", userName: username, accessKey: accessKey, }, browserName: "Safari", browserVersion: "latest", }, { "bstack:options": { osVersion: "15", deviceName: "iPhone 13", realMobile: "true", local: "false", seleniumVersion: "4.1.0", projectName: "BStack Demo", buildName: buildName, sessionName: "parallel test - iPhone", userName: username, accessKey: accessKey, }, browserName: "iPhone", }, { "bstack:options": { osVersion: "12.0", deviceName: "Google Pixel 6 Pro", realMobile: "true", local: "false", seleniumVersion: "4.1.0", projectName: "BStack Demo", buildName: buildName, sessionName: "parallel test - Android", userName: username, accessKey: accessKey, }, browserName: "Chrome", }, ];
- Run your tests in parallel on BrowserStack using the following command:
npm run parallel
- View your tests on BrowserStack on the BrowserStack Automate Dashboard.
Understand the test script
The parallel.conf.js
file is added as a dependency in the parallel.test.js
test script for accessing the capabilities to run the parallel tests. The describe.each
command is used to run our test over multiple browser-device combinations as follows:
...
test.concurrent(
"add products to cart",
async () => {
capabilities["bstack:options"].sessionName =
"add products to cart - parallel test " +
(capabilities["bstack:options"].deviceName || capabilities.browserName);
let statusFail;
let driver = await createDriver(capabilities);
try {
await driver.get("https://bstackdemo.com/");
await driver.wait(until.titleMatches(/StackDemo/i), 10000);
// locating product on webpage and getting name of the product
let productText = await driver
.findElement(By.xpath('//*[@id="1"]/p'))
.getText();
// clicking the 'Add to cart' button
await driver.findElement(By.xpath('//*[@id="1"]/div[4]')).click();
// waiting until the Cart pane has been displayed on the webpage
driver.findElement(By.className("float-cart__content"));
// locating product in cart and getting name of the product in cart
let productCartText = await driver
.findElement(
By.xpath(
'//*[@id="__next"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]'
)
)
.getText();
// checking whether product has been added to cart by comparing product name
expect(productText).toBe(productCartText);
} catch (e) {
statusFail = e.message;
} finally {
await setStatusAndKillDriver(driver, statusFail);
}
},
10000000
);
...
Next steps
After you have successfully run your first test on BrowserStack, you might want to do one of the following:
- Test privately hosted websites
- Generate a list of capabilities that you want to use in tests
- Find information about your Projects, Builds and Sessions using our REST APIs
- Set up your CI/CD: Jenkins, Bamboo, TeamCity, Azure, CircleCI, BitBucket, TravisCI, GitHub Actions
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!