The Swiss Army knife for automated Jasmine specs in your console.
The Swiss Army knife for automated Jasmine specs in your console.
Guard::Jasmine automatically tests your Jasmine specs when files are modified.
Tested on MRI Ruby 1.9.3, 2.0.0, 2.0.1 and the latest versions of JRuby and Rubinius.
If you have any questions please join us on our Google group or on #guard
(irc.freenode.net).
Continuous testing based on file modifications by [Guard][], manifold configuration by writing rules with RegExp and Ruby.
Fast headless testing on [PhantomJS][], a full featured WebKit browser with native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.
Runs the standard Jasmine test runner, which can optionally integrate [Jasmine][] into the [Rails asset pipeline][], enabling you to write your specs in [CoffeeScript][].
Integrates Istanbul to instrument your code in the asset pipeline and generate coverage reports.
Custom console logger implementation for pretty printing JavaScript objects and DOM elements.
Can be used to run Jasmine-species acceptance tests provided by Jasmine Stories.
Thor and Rake command line helper for CI server integration.
Runs on Mac OS X, Linux and Windows.
If you are a RailsCast Pro subscriber, I recommend to watch #261 Testing JavaScript with Jasmine (revised) for an introduction to Jasmine, Jasminerice and Guard::Jasmine.
The simplest way to install Guard is to use Bundler. Please make sure to have [Guard][] installed.
Add Guard::Jasmine to your Gemfile:
group :development, :test do
gem 'guard-jasmine'
endAdd the default Guard::Jasmine template to your Guardfile by running:
$ guard init jasminePlease have a look at the CHANGELOG when upgrading to a newer Guard::Jasmine version.
You need the PhantomJS browser installed on your system. You can download binaries for Mac OS X and Windows from [the PhantomJS download section][].
Alternatively you can install [Homebrew][] on Mac OS X and install it with:
$ brew install phantomjsIf you are using Ubuntu 12.04 or above, phantomjs is in the official repositories and can be installed with apt:
$ sudo apt-get install phantomjsFor older versions of Ubuntu, you will need to add a repository first:
$ sudo add-apt-repository ppa:jerome-etienne/neoip
$ sudo apt-get update
$ sudo apt-get install phantomjsYou can also build it from source for several other operating systems, please consult the [PhantomJS build instructions][].
With Rails 3.1 and later you can write your Jasmine specs in addition to JavaScript with CoffeeScript, fully integrated into the Rails asset pipeline. You have full access to your running Rails app, but it's a good practice to fake the server response. Check out the excellent [Sinon.JS][] documentation to learn more about this topic.
Guard::Jasmine will start a Rails Rack server to run your specs. Step by step instructions on configuring this are below.
Install Guard Jasmine into the Rails project's Gemfile:
gem "guard-jasmine", "~> 2.0"
Install Jasmine: rails generate jasmine:install
(Optionally) install Jasmine examples: rails generate jasmine:examples
(Optionally) Edit spec/javascripts/support/jasmine.yml to enable running CoffeeScript.
spec_files: section to '**/*[sS]pec.{js,coffee}'Install GuardJasmine's sample Guardfile rails generate guard_jasmine:install
Run GuardJasmine: guard
A sample Rails project configured in this way is at https://github.com/nathanstitt/guard-jasmine-rails-test
Please read the Guard usage documentation.
Guard::Jasmine can be adapted to all kind of projects. Please read the Guard documentation for more information about the Guardfile DSL.
guard 'jasmine' do
watch(%r{spec/javascripts/spec\.(js\.coffee|js|coffee)$}) { "spec/javascripts" }
watch(%r{spec/javascripts/.+_spec\.(js\.coffee|js|coffee)$})
watch(%r{app/assets/javascripts/(.+?)\.(js\.coffee|js|coffee)$}) { |m| "spec/javascripts/#{m[1]}_spec.#{m[2]}" }
endThere are many options that can customize Guard::Jasmine to your needs. Options are simply supplied as hash when
defining the Guard in your Guardfile:
guard 'jasmine', all_on_start: false, specdoc: :always do
...
endThe server options configures the server environment that is needed to run Guard::Jasmine:
…If you're setting the :server option to :none or need to access your specs on a other host than localhost, you can
supply the Jasmine runner url manually:
jasmine_url: 'http://192.168.1.5:1234/jasmine' # URL where Jasmine is served.
# default: nilYou may want to have also a fixed port instead of the random generated one.
The reason why the Server environment is set to development by default is that in development mode
the asset pipeline doesn't concatenate the JavaScripts and you'll see the line number in the real file,
instead of a ridiculous high line number in a single, very large JavaScript.
If you supply an unknown server name as the :server option, then Guard::Jasmine will execute
a rake task with the given server name as task in a child process. For example, if you configure
server: 'start_my_server', then the command rake start_my_server will be executed and
you have to make sure the server starts on the port that you can get from the JASMINE_PORT
environment variable.
The spec runner options configures the behavior driven development (or BDD) cycle:
…The :keep_failed failed option remembers failed suites and not failed specs. The reason for this decision is to
avoid additional round trip time to request the Jasmine test runner for each single spec, which is mostly more expensive
than running a whole suite.
In general you want to leave the :clean flag on, which ensures that only Jasmine specs (files ending with _spec.js,
_spec.coffee and _spec.js.coffee inside your project are passed to the runner. If you have a custom project
structure or spec naming convention, you can set :clean to false to skip that file filter.
Guard::Jasmine can generate an RSpec like specdoc in the console after running the specs and you can set when it will be shown in the console:
…With the option set to :always, the specdoc is shown with and without errors in your spec, whereas on with the option
set to :never, there is no output at all, instead just a summary of the spec run is shown. The default option
:failure shows the specdoc when at least one spec failed.
When :focus is enabled, only the failing specs are shown in the specdoc when at least one spec is failing.
The :errors option is partially working when using at least PhantomJS version 1.5. Please see
Issue #166 for the actual status of retreiving the JavaScript
stack trace.
You may want to have different options when the spec runner runs all specs. You can specify the :run_all option
as a Hash that contains any valid runner option and will overwrite the general options.
run_all: { specdoc: :never } # Run all options,
# Takes any valid option
# default: {}The :console options adds captured console logs from the spec runner and adds them to the specdoc. Guard:Jasmine
contains its own minimalistic console implementation. The following console methods are supported:
console.logconsole.infoconsole.warnconsole.errorconsole.debugThe difference for each of this log methods is simply a prefix that is added to the log statement. The log also supports the common format placeholders:
%s%i, %d%f%oYou can further customize the log output by implement one of these methods:
toString() - must return a string that describes the objecttoJSON() - must return an object that is used instead of the actual object.In addition, the console can log jQuery collections and outputs the HTML representation of the element by using the
jQuery html() method.
Guard::Jasmine supports custom jasmine reporters. In order to use them you have to configure jasmine and make sure that you add the javascript files to the available assets, in this case I use the ones from larrymyers/jasmine-reporters:
reporters:
junit:
- "junit_reporter.js"
- "junit_reporter.boot.js"junit_reporter.boot.js is the file where you'll have to intialize and configure the reporter:
(function() {
var reporter = new jasmineReporters.JUnitXmlReporter({
// Here you can set any options that the custom reporter accepts
savePath: 'reports/junit'
});
jasmine.getEnv().addReporter(reporter);
})();Once you got everything in the right place, run guard-jasmine with --reporters
If none of the existing reporters work for you, you are writing your own one and you want it to write it's output to the filesystem, you have two options:
Guard::Jasmine supports coverage reports generated by Istanbul. You need to
have istanbul in your path in order to make this feature work. You can install it with NPM
$ npm install -g istanbulYou also need to explicit enable the coverage support in the options:
coverage: true # Enable/disable JavaScript coverage support
# default: falseIstanbul needs to instrument the implementation files so that the execution path can be detected. Guard::Jasmine comes with a tilt template that generates instrumented implementation files when using in the asset pipeline. If you do not use asset p
No open issues yet, or sync has not completed.