Skip to main content
No Result Found

Get Started with JS Testing

Your guide to running Javascript unit tests on BrowserStack Automate.

Note: All the code samples and files referred to in this guide can be found in our GitHub karma-browserstack-example repository.

Running your Javascript Unit tests on BrowserStack is simple. We can use different Javascript unit testing frameworks such as QUnit, Jasmine, Mocha, and others to write our first unit test. In this sample we are using QUnit.

Sample test case

We will write a sample test case using Qunit framework. Follow the below bash commands to set up your test environment:

terminal
Copy icon Copy

Once we have set up the environment, create a file odd.js in a src directory. Use the below code:

odd.js
Copy icon Copy

Now that we have our source code, let’s add a few unit test cases for it.

Create a file test.js in a tests directory. The unit test checks if a number is odd or not. Use the below code:

test.js
Copy icon Copy

To run the tests, run the below command from the root directory:

terminal
Copy icon Copy

You can see the result in your terminal. Now that we have written the test case, we are ready to run this Javascript unit test on BrowserStack.

Run sample test case on BrowserStack

We will use Karma to run the above sample test cases on BrowserStack. Install the following dependencies:

terminal
Copy icon Copy

You also need to set the auth credentials of Automate so that the test runs are authenticated on BrowserStack. You can find these in the accounts page and in the Automate Dashboard.

terminal
Copy icon Copy

Create a file with name karma.conf.js in the root directory of the project, and use the below code

karma.conf.js
Copy icon Copy

Since we are including the source and test files in karma.conf.js files, change the odd.js code to not use export:

odd.js
Copy icon Copy

Similarly, change the code test.js file:

test.js
Copy icon Copy

Trigger the test case by using the following command:

terminal
Copy icon Copy

It will run the test on the BrowserStack browsers. You can check the status of your test on the Automate Dashboard.

Next Steps

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
Download Copy Check Circle