Skip to end of metadata
Go to start of metadata

This topic describes how to set up a new JavaScript unit testing project using the Karma test runner and an example project. If you already have an existing Karma configuration, you should check out the karma-sauce-launcher to add Sauce Labs testing. You can also edit the Karma config files to work with your specific project by modifying the src and test configuration properties to point to your project files. 


Installing Karma and the Sample Karma Project

  1. Open a terminal window and clone the Karma sample project from GitHub, and navigate to the karma-sauce-example directory. 

    git clone &&
    cd JS-Karma-UnitTest

    No Git, No Problem

    You can also download the project directly from GitHub without needing to use the Git interface.

  2. Run this command to install the Karma command command line interface globally, and to install the sample project's local node.js dependencies.

    npm install -g karma-cli && npm install

Changing Browser and Frameworks in the Sample Project

By default, this example runs Jasmine tests in Chrome and Firefox on your local machine. You can add more browsers by editing the browsers array in the karma.conf.js file, or change the test framework in the frameworks array. 

Running Karma on Sauce Labs with the Karma-Sauce-Launcher

  1. To use Karma with Sauce Labs and run the example project, you first need to set up your Sauce authentication credentials as environment variables
  2. After your environment variables are set, run the tests with this command.

    karma start karma.conf-ci.js

This will launch Sauce Connect to establish a tunnel between your local machine and the Sauce Labs testing cloud. You can speed up the time it takes to connect to Sauce by starting Sauce Connect in the background. Check out Setting Up Sauce Connect Proxy for information on downloading the binaries for your operating system and running Sauce Connect. If you want to launch Sauce Connect from your command line rather than as part of the karma-sauce-launcher start up, you need to edit the sauceLabs dictionary in the karma.conf-ci.js file so that the startConnect option is set to false

Reset Before Using with a CI Platform

If you set the startConnect option to false, be sure to re-set it to true before you try to use the karma-sauce-launcher with a continuous integration platform.

Using the Karma-Sauce-Launcher with a Continuous Integration Platform

The topics under Using Sauce Labs with Continuous Integration Platforms describe how to set up your CI platform to integrate with Sauce. These integrations require that you have your Sauce Labs authentication credentials set as environment variables. The sample karma.conf-ci.js file is already set up to read environment variables for CI platforms, so you shouldn't need to modify it as long as process.env.SAUCE_USERNAME and process.env.SAUCE_ACCESS_KEY are set properly during the build. 


  • No labels