Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tag: 0.3.0
Fetching contributors…

Cannot retrieve contributors at this time

130 lines (79 sloc) 4.889 kB

Evergreen

“Because green is the new Blue(Ridge)”

Evergreen is a tool to run javascript unit tests for client side JavaScript. It combines a runner which allows you to serve up and run your specs in a browser, as well as a headless runner based on envjs. Evergreen uses the Jasmine unit testing framework for JavaScript.

github.com/jnicklas/evergreen

Philosophy

Evergreen is a unit testing tool. It's purpose is to test JavaScript in isolation from your application. If you need a tool that tests how your JavaScript integrates with your application you should use an integration testing framework, such as Capybara.

Installation

Install as a Ruby gem:

gem install evergreen

Usage

Evergreen assumes a file and directory structure, place all your javascript code inside ./public and all spec files inside ./spec/javascripts. All spec files should end in _spec.js. For example:

public/widget.js
spec/javascripts/widget_spec.js

You can require files from the public directory inside your spec file:

require('/widget.js')

describe('a widget', function() {
  ...
});

You can now look at your spec files inside a browser by starting up the Evergreen server:

evergreen serve

Alternatively you can run the specs headlessly by running:

evergreen run

Integrating with Rails 3

Add Evergreen to your Gemfile:

gem 'evergreen', :require => 'evergreen/rails'

Start your rails application and navigate to /evergreen/list. You should now see a list of all spec files, click on one to run it.

There's a rake task provided for you that you can use to run your specs:

rake spec:javascripts

Transactions

One problem often faced when writing unit tests for client side code is that changes to the page are not reverted for the next example, so that successive examples become dependent on each other. Evergreen adds a special div to your page with an id of test. This div is automatically emptied before each example. You should avoid appending markup to the page body and instead append it to this test div:

describe('transactions', function() {
  it("should add stuff in one test...", function() {
    $('#test').append('<h1 id="added">New Stuff</h1>');
    expect($('#test h1#added').length).toEqual(1);
  });

  it("... should have been removed before the next starts", function() {
    expect($('#test h1#added').length).toEqual(0);
  });
});

Templates

Even more powerful than that, Evergreen allows you to create HTML templates to go along with your specs. Put the templates in their own folder like this:

spec/javascripts/templates/one_template.html
spec/javascripts/templates/another_template.html

You can then load the template into the test div, by calling the template function in your specs:

describe('transactions', function() {
  template('one_template.html')

  it("should load the template in this test", function() {
    ...
  });
});

Spec Helper

If you add a spec_helper file like so:

spec/javascripts/spec_helper.js

It will automatically be loaded. This is a great place for adding custom matchers and the like.

CoffeeScript

Evergreen supports specs written in CoffeeScript. Just name your spec file _spec.coffee and it will automatically be translated for you.

You can also add a CoffeeScript spec helper, but remember that CoffeeScript encloses individual files in a closure, if you need something you define in the spec helper to be available in your spec files, attach it to the window object:

# spec/javascripts/spec_helper.coffee

MyThing: "foo"          # local to spec helper
window.MyThing: "foo"   # global

License:

(The MIT License)

Copyright © 2009 Jonas Nicklas

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Jump to Line
Something went wrong with that request. Please try again.