# jasmine-istanbul-phantom
Client side JavaScript unit tests and code coverage with fixtures, ajax mock, Gulp and Grunt support combining Jasmine, Istanbul and Phantom in one module
- Jasmine v2.3.4 for unit tests and Istanbul for test coverage
- Tests run on Phantom 1.9.8 (included) or 2.0.0
- Fixtures auto load
- Easy Gulp integration
- Works as a Grunt plugin
- Includes Jasmine ajax mock
- Istanbul outputs summary to console and full reports to file
- Jasmine reports to console and to JUnit XML file for CI
- Spec runner can also run in a full browser
- Polyfill Function.bind for Phantom 1.9
With focus on performance, everything runs on file://. No server start/stop and no net socket communication between Jasmine, Phantom or Node. It uses stdio.
Install
npm install --save-dev jasmine-istanbul-phantom
Usage
var jip = ; ;
Example:
;
See example project
Gulp example
See a gulpfile.js that runs only changed specs
Grunt example
This module also works as a Grunt 0.4 plugin. See a gruntfile.js
Options
[options]
is an object with the following properties and default values:
- base (path): It's the base path for test files. Will be the parent
directory of
spec
,tmp
,fixture
andreport
folders when using defaults. Otherwise one must set all paths individually.
Defaults try to find./test
or./tests
directory and use it as base path - src (glob): JavaScript source files. The ones that will be instrumented
and tested.
Defaults to['src/**/*.js','app/**/*.js']
- lib (glob): Other JavaScript files. Will not be tested nor instrumented.
Usually this property should be overridden to ensure libs loading order.
Defaults to['lib/**/*.js', 'vendor?(s)/**/*.js']
- tmp (path): Path to write instrumented source code, fixtures file and
final generated spec runner.
Defaults to[base]/tmp
- spec (glob): Jasmine spec files. The ones that will test the src files.
Defaults to[base]/spec?(s)/**/*.js
- runner (file): Jasmine spec runner template. Will be used to generate
the final HTML spec runner with calls to src, lib and spec files.
Defaults tojasmine-istanbul-phantom/lib/jasmine/SpecRunner.html
- fixture (glob): Files to be available as fixtures inside spec files.
Access fixtures with
getFixture([filename])
.
Defaults to[base]/fixture/*
- callback (function): Callback function is called when all tests end and
reports are written.
Defaults tonull
- jasmine
- report (path): Path to write Jasmine JUnit XML reports.
Default to[base]/report/unit
- report (path): Path to write Jasmine JUnit XML reports.
- istanbul
- report (path): Path to write Istanbul file reports.
Default to[base]/report/coverage
- reporters (array): List of Istanbul reporters.
Defaults to['text-summary', 'lcov', 'clover']
- report (path): Path to write Istanbul file reports.
- phantom
- bin (file): Phantom binary file location.
Default uses the included npm phantom - verbose (boolean): Show resource errors e.g. file not found.
Defaults tofalse
- params (object): Phantom additional runtime parameters. See some
useful parameters
Defaults to{}
.
- bin (file): Phantom binary file location.
Example project structure
Works with this module's default options
project/
├── lib/
│ ├── jquery
│ │ └── jquery-2.1.4.min.js
│ └── etc...
├── src/
│ ├── app.js
│ ├── util.js
│ └── etc...
└── test/
├── fixture/ (optional)
│ └── all fixture files...
├── report/ (auto-created)
│ ├── coverage
│ │ └── Istanbul reports...
│ └── unit
│ └── Jasmine reports...
├── spec/ (required)
│ └── all spec files...
└── tmp/ (auto-created)
├── isrc.js (generated instrumented src)
├── fixture.js (generated fixtures file)
└── SpecRunner.html (generated spec runner)
Using fixtures
All files from the fixtures glob are loaded and made available as an object to the spec files. Properties are filenames and values are strings. See FixtureSpec example
// on any spec file, get the song.json fixture as a JSONvar song = JSON
// show object with all fixturesconsole;
Run Jasmine tests in a browser
After running some tests, open the generated spec runner
[base]/[tmp]/SpecRunner.html
in a browser with GUI (not Phantom).
It is useful for test development and debug of Jasmine specs
Istanbul coverage reports
Console shows only a summary report. Open
[base]/[report]/coverage/lcov-report/index.html
in a browser for
full coverage report
Editing the spec runner
Copy the original spec runner's template from
jasmine-istanbul-phantom/lib/jasmine/SpecRunner.html
to another path and run with option {runner:'path/to/new/runner.html'}
.
Edit the new runner as needed but keep the Jasmine and fixture script/style
includes
Jasmine ajax tests
XHR tests use the included Jasmine Mock ajax.
Example at /example/test/spec/AjaxSpec.js
Remember to run jasmine.Ajax.install()
before any ajax request and
don't bother to turn it off. Usually it's turned on and off because fixtures are
fetched using ajax. This module preload fixtures and do not use ajax
to load them.
Phantom 2.0
This module comes with npm phantom v1.9.8 (as of may 2015). To run with Phantom 2.x, install it and use the option below.
Phantom dependency causes installation to fail
Read the phantomjs module troubleshooting
Useful Phantom parameters