Running Unit Tests - tsgrp/HPI GitHub Wiki
Overview
Unit testing in HPI is done with the Jasmine testing framework. Please visit their site and review the docs on how the API works. It's very simple.
Karma is the test runner. Specifically grunt-karma the grunt plugin for Karma.
For more information on writing unit tests, see this page
Running Tests
Browser
When you start up HPI via the grunt server command, karma should launch the configured testing browsers. If not, manually open http://localhost:9876
in Chrome or FireFox, which should show a ready message. A watch task monitors all the test/**/*Spec.js
files. To run the tests, save any *Spec.js file and the tests will run. If those files are changed the tests will run. The output goes to the console running grunt. (NOT the browser)
Command Line
Alternative to the browser method outlined above, you can also run tests using the shorthand npm task from the command line. For example:
npm run test
One benefit to this approach is that the above command is how our Continuous Integration server (Jenkins) runs the tests.
Running Specific Tests
As the unit test count grows higher (over 1500 plus as of Nov 2015!), running all of the unit tests is becoming slower. A command line option has been added to allow running a subset of the unit tests. All unit tests should still be run before committing code, but this option makes developing an individual module quicker to debug and rebuild. An example usage of the option is shown below:
grunt clean build uglify:assets karma:jenkins --project=hpi-demo-dctm --env=your-env --isTest --specFilepathRegex=/ocquery*Spec.js/
The above command runs unit tests for any unit tests whose filepath matches the /ocquery.*Spec.js/ regex.
For an expedited test, run:
grunt clean build:debug karma:jenkins --project=hpi-demo-dctm --isTest --specFilepathRegex=/ocquery.*Spec\.js/
Note the difference here is we omitted uglify:assets
and changed build
to build:debug
. This will process the assets files un-minified, drastically speeding up testing iterations.
If no --specFilepathRegex is specified, all unit tests are run.
Debugging Tests
Debugging is very easy. In Chrome connect to http://localhost:9876. (If that's not the browser auto launched) Click the debug button. A new tab will open. Open developer tools and search for your spec file or any HPI javascript file and set your breakpoints. Refresh the page to trigger the tests. You can edit your test/app files and refresh the page to run the updated tests.
Generating and Viewing Coverage Reports
The easiest way is just to run:
grunt karma:coverage --project=myproject
If you want the coverage to run everytime, in karma.conf:
- uncomment the line inside the "preprocessors" object
- add to "reporters" array a string called 'coverage' Then run your node server and trigger execution of unit tests. Now look in your HPI folder. There should be a folder "coverage". Inside the "coverage" folder should be another folder (named after the browser that your tests ran against). Open the folder and within, manually open the "index.html" file in your browser. Tada you have coverage reports.
Troubleshooting
General
- The first thing to try is bouncing your hpi server.
- Karma runs in the background, if you want to see errors in Karma start up you will need to move it to the foreground by editing the background property in the Karma task configuration in GruntFile.js
If no testing browser is launching:
- Check the browsers property in karma.conf.js. Is that browser installed?
- Do you have {BROWSER}_BIN environment variable configured? Karma needs to know where the executable for the browser is, if the exe is not where karma expects it will look for a variable with the path. For instance, Firefox need a FIREFOX_BIN environment variable. On windows you will use: set FIREFOX_BIN=C:\program files\firefox\firefox.exe where the path points to your firefox.exe.
- The default is Firefox, but you can try switching to chrome. That seems to work better on windows machines.
- If you close the browser or cannot get it to launch, you can always just navigate to http://localhost:9876 in a new tab of any browser.