Skip to content

Karma testing

tpronk edited this page Apr 29, 2021 · 9 revisions

Home - Karma testing


Creating a Karma test

Each Karma test should be in a separate subdirectory of ./tests/ that contains two files:

  1. A testscript that performs a set of tests of the PsychoJS library.
  2. A test configuration file that describes your test.

Inside of your testscript, you've got the following features at your disposal:

  • Any of the PsychoJS modules for importing.
  • The jasmine API for making assertions.

For an example see tests/karma_formattedString.

Running a Karma test

A Karma testrun can be started via the command below, with a set of CLI options (or environment variables) for configuring the testrun. Additionally, there are a couple of CLI options provided by Karma itself. A handy one is --no-single-run, which keeps the browser window open, thus allowing you to debug your unit tests. For an overview of Karma's CLI options, run: karma start --help

node scripts/cli/runkarma.cjs start scripts/shared/karma.conf.cjs

Examples

Example 1

Run all Karma tests via a local testrunner. Note that when you're using the bash shell, you'll need to escape the star wildcard (\*)

node scripts/cli/runkarma.cjs start scripts/shared/karma.conf.cjs --server local --label *

Example 2

This example requires features only available to the PsychoPy/PsychoJS team

Run all tests via BrowserStack, on all platforms supported by BrowserStack, with branch having the value "thomas".

node scripts/cli/runkarma.cjs start scripts/shared/karma.conf.cjs --server bs --label * --branch thomas --platform *

Example 3

This example requires features only available to the PsychoPy/PsychoJS team

Run a Karma test with the following settings:

  • Via BrowserStack
  • All tests labeled unit_randomization
  • Use thomas as name for the branch
  • On each Windows 10 browser supported by BrowserStack
  • Use test1 as name for the testrun
  • Upload the testlogs to the PsychoPy staging server, where they will be available at https://staging.psychopy.org/report/<branch>/test1/e2e_img

node scripts/cli/runkarma.cjs start scripts/shared/karma.conf.cjs --server bs --label unit_randomization --branch thomas --platform *Windows*10* --testrun test1 --uploadResults

Issues and notes

Custom fork of karma-browerstack-launcher for platformNames

To have the logs use the Platform naming scheme required a fork of the karma-browerstack-launcher. This fork logs platform names in BrowserStack session names, which can next be queried and joined with the karma JSON logs on browser id.

Terminating a unit-testrun via BrowserStack

When you abort test that is running via BrowserStack, the sessions (called workers) keep running in the background, and so prevent new BrowserStack sessions to start. You'll need to stop the sessions manually via the BrowserStack dashboard instead. I've got an automated solution in mind, but I have not implemented it yet. This solution involves modifying my fork of karma-browserstack-launcher to log workerIds somewhere around this line and calling the BrowserStack REST API to terminate a browser worker.