Get Started with JS Testing
Your guide to running Javascript unit tests on BrowserStack Automate.
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:
Once we have set up the environment, create a file odd.js
in a src
directory. Use the below code:
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:
To run the tests, run the below command from the root directory:
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:
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.
Create a file with name karma.conf.js
in the root directory of the project,
and use the below code
Since we are including the source and test files in karma.conf.js files
,
change the odd.js
code to not use export:
Similarly, change the code test.js
file:
Trigger the test case by using the following command:
It will run the test on the BrowserStack browsers. You can check the status of your test on the Automate Dashboard.
Next Steps
- Integrate your JS unit test framework
- Learn how to run your tests from a CI
- Run your tests in parallel to get faster feedback
- Explore the browsers and devices you can test on to gain confidence in your code
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!