Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Unit Testing in Rails 3.1

I am wondering what is the easiest way to do the JavaScript Unit testing as part of Rails 3.1 application.

I like Jasmine a lot and it works pretty well (although some tricks are required for it to pick up .coffee files).

The only problem I have with Jasmine is that it runs all the tests examples inside one huge page which is very problematic as it requires loading ALL of the scripts.

The thing I really want is Jasmine + multiple test suites in multiple files (so that it generates multiple html files including spec files).

In addition to that, I want to run tests (hopefully easily) in the browsers, headless or inside a JS engine (when possible).

Any recommendations?

like image 641
Dmytrii Nagirniak Avatar asked Jun 09 '11 00:06

Dmytrii Nagirniak


3 Answers

Teaspoon does pretty much what you're looking for.

I wrote most of it, and it's based on my experience with writing javascript specs and using Rails 3.1 / coffeescript. Your question includes some of the very things that made me want to contribute it in the first place.

EDIT:

To clarify, Teaspoon supports defining multiple suites, has a rake task, supports using Selenium Webdriver or PhantomJS as drivers, Jasmine, Mocha, or QUnit test frameworks, allows running from the command line (eg. bundle exec teaspoon spec/javascripts/my_spec.coffee), and several other nice to haves.

like image 141
jejacks0n Avatar answered Oct 20 '22 08:10

jejacks0n


Where i work, we wanted to find a solution to cover pretty much what you are mentioning.

We examined the following frameworks:

  • teaspoon
  • konacha
  • karma
  • bunyip
  • jasmine-rails

We finally picked teaspoon. It required minimal setup and it was easy to integrate with our CI. It provides suites, asset pipeline support (so that you can test .coffee without hacks) and it can run in RAILS_ENV=test

like image 22
Dimitris Zorbas Avatar answered Oct 20 '22 09:10

Dimitris Zorbas


You might want to try the evergreen (https://github.com/jnicklas/evergreen). It allows you to write testcases with jasmine and run your tests in the browsers, headless or inside a JS engine.

You can found the usage of this gem on the readme section https://github.com/jnicklas/evergreen#readme

Unfortunately, evergreen doesn't play well with new rails 3.1 feature yet (at the time this answer is made). So I try to create some monkey patch to get it play well.

# config/evergreen.rb
unless defined?(CONFIG_EVERGREEN_LOADED)
  CONFIG_EVERGREEN_LOADED = true

  require ::File.expand_path('../environment',  __FILE__)

  unless "".respond_to?(:each) # this monkey patch make the old capybara play well with ruby 1.9.2
    String.class_eval do
      def each &block
        self.lines &block
      end
    end
  end

  module Evergreen

    class << self
      def application_with_additions(suite)
        app = application_without_additions(suite)

        app.map "/assets" do
          assets = Rails.application.config.assets
          if assets.enabled
            require 'sprockets'
            sprockets = Sprockets::Environment.new(suite.root)
            sprockets.static_root = File.join(suite.root, 'public', assets.prefix)
            sprockets.paths.concat assets.paths
            sprockets.js_compressor = nil
            run sprockets
          end
        end
        app
      end

      alias_method :application_without_additions, :application
      alias_method :application, :application_with_additions
    end

end
like image 35
Jacob Dam Avatar answered Oct 20 '22 09:10

Jacob Dam