13

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?

5 Answers 5

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Jeremy. That looks really good. I'm yet to try how it works since I'm currently using Jasminerice.
Jasminerice is what I was using prior to writing Teabag. I liked it but wanted more in terms of running via the commandline and on CI.
3

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

We examined the following frameworks:

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

Comments

2

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

5 Comments

Gosh. Guys, start reading the question. I don't think evergreen supports multiple files/suites
I think @evergreen does support multiple files? Also you can require files for each test rather than all resources at once. Although I'm not a great fan of evergreen, it would be an option... as you can specify a subset of tests to run and don't need to load every file.
Evergreen is abandonware at this point anyway, so I'd suggest avoiding it. We had to get rid of it in order to upgrade our capybara gem, so while it's a nice Gem, it's not a good idea going forward.
@StingeyB What's your alternative that you recommend instead as of late 2013?
@Trip We went with teaspoon. Migrating ended up being pretty easy. We had to define individual suites for each spec, but after that, running it in phantom and selenium was a breeze. My long term solution will be to get our javascript written in modules, so the tests can load only what's needed, but teaspoon is a good alternative to evergreen.
0

As of now, I haven't found a reasonable answer to this. But the issue #24 of jasminerice is probably the closes to the answer if it will be implemented

Comments

0

Perhaps try jasmine-headless-webkit. This offers the capability to run your Jasmine specs within a headless WebKit browser.

2 Comments

It still uses one page for all tests. Exactly what I don't need.
The link to the readme now says "This project is dead. You should use Karma instead. I do."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.