Skip to content
This repository was archived by the owner on Apr 24, 2019. It is now read-only.

Latest commit

 

History

History
42 lines (32 loc) · 1.88 KB

writing-tests.md

File metadata and controls

42 lines (32 loc) · 1.88 KB

« back to Table of Contents

Writing JavaScript Unit Tests

INIT relies on the library Jasmine version 2 for writing JavaScript unit tests. Please see the full documentation for more information on how to use Jasmine.

Jasmine is utilized by Karma which runs all unit tests across different browsers. You can find Karma's configuration within the karma.conf.js file in grunt/.

There are four different tasks of interested to run with Grunt:

  • grunt test: Runs all unit tests on a PhantomJS instance. This is meant to be the default task for developers to use when running unit tests. You can also use the slightly longer but more specific karma:unit.
  • grunt test:all: Runs instances of PhantomJS, Chrome, Safari and Firefox and executes your tests. This is an alias for karma:all.
  • grunt test:build: Tests the production-ready RequireJS build with PhantomJS to prevent errors when minifying and compressing JS.
  • grunt karma:travis: Runs all unit tests on a PhantomJS and a Firefox instance. Meant to be used with Travis CI.

Adding a test suite

In general you want to write a test suite for each module you write. You can do this by following this steps:

  • If you haven't created the module within src/modules/ yet, do so now.
  • Add a file modulename.spec.js within src/modles/modulename/ by copying over module.spec.js and removing unnecessary stuff.
  • Adjust the define statement to include the module you want to test. Add modulename.spec.js as ../modules/modulename/modulename.spec in test/test-main.js.
  • Run tests to ensure everything works just as you expect it.

Utility

There is a utility flag window.__test which is set to true if you run tests using Karma. This can come in handy if you want to make a private API testable.