百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
G

guard-jasmine

> 编程语言
开源

用于在控制台中自动生成 Jasmine 测试规范的瑞士军刀。

303 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

用于在控制台中自动生成 Jasmine 测试规范的瑞士军刀。

Guard::Jasmine

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).

Highlights

  • 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.

ScreenCast

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.

Installation

Guard 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:

ruby
group :development, :test do
  gem 'guard-jasmine'
end

Add the default Guard::Jasmine template to your Guardfile by running:

bash
$ guard init jasmine

Please have a look at the CHANGELOG when upgrading to a newer Guard::Jasmine version.

PhantomJS

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:

bash
$ brew install phantomjs

If you are using Ubuntu 12.04 or above, phantomjs is in the official repositories and can be installed with apt:

bash
$ sudo apt-get install phantomjs

For older versions of Ubuntu, you will need to add a repository first:

bash
$ sudo add-apt-repository ppa:jerome-etienne/neoip
$ sudo apt-get update
$ sudo apt-get install phantomjs

You can also build it from source for several other operating systems, please consult the [PhantomJS build instructions][].

Rails with the asset pipeline setup

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.

How it works

  1. Guard is triggered by a file modification.
  2. Guard::Jasmine executes the [PhantomJS script][].
  3. The PhantomJS script requests the Jasmine test runner via HTTP.
  4. Rails uses the asset pipeline to get the Jasmine runner, the code to be tested and the specs.
  5. The asset pipeline prepares the assets, compiles the CoffeeScripts if necessary.
  6. The asset pipeline has finished to prepare the needed assets.
  7. Rails returns the Jasmine runner HTML.
  8. PhantomJS requests linked assets and runs the Jasmine tests headless.
  9. The PhantomJS script collects the Jasmine runner results and returns a JSON report.
  10. Guard::Jasmine reports the results to the console and system notifications.

Configuring Guard Jasmine to integrate with the asset pipeline.

  • 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.

    • Change the 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

Usage

Please read the Guard usage documentation.

Guardfile

Guard::Jasmine can be adapted to all kind of projects. Please read the Guard documentation for more information about the Guardfile DSL.

ruby
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]}" }
end

Options

There are many options that can customize Guard::Jasmine to your needs. Options are simply supplied as hash when defining the Guard in your Guardfile:

ruby
guard 'jasmine', all_on_start: false, specdoc: :always do
  ...
end

Server options

The 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:

ruby
jasmine_url:  'http://192.168.1.5:1234/jasmine'  # URL where Jasmine is served.
                                                   # default: nil

You 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.

Use a custom server

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.

Spec runner options

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.

Specdoc options

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.

Overwrite options when running all specs

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.

ruby
run_all: { specdoc:  :never }                 # Run all options,
                                              # Takes any valid option
                                              # default: {}

Console logs

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.log
  • console.info
  • console.warn
  • console.error
  • console.debug

The 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
  • %o

You can further customize the log output by implement one of these methods:

  • toString() - must return a string that describes the object
  • toJSON() - 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.

Custom Report options

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:

yaml
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:

javascript
(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

Making your own reporter write to the filesystem

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:

  1. use window.__phantom_writeFile(filename, text) (the way larrymyers/jasmine-reporters works)
  2. use window.callPhantom({event: 'writeFile', filename: filename, text: text}) (the way shepmaster/jasmine-junitreporter works)

Coverage 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

ruby
$ npm install -g istanbul

You also need to explicit enable the coverage support in the options:

ruby
coverage: true                                # Enable/disable JavaScript coverage support
                                              # default: false

Instrumentation

Istanbul 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

Issues· 4 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言