Javascript unit-testing for Rails 3.1 and asset pipeline - javascript

I'm looking for the easiest possible Javascript unit-testing system for Rails 3.1 that is specifically capable of working with the asset pipeline out of the box.
I've tried various incarnations of the jasmine gem, including jasmine-rice, headless-jasmine-webkit. Jasmine gem does not appear to work with rails 3.1 out of the box, requiring tweaking of various config files. Jasmine-rice and headless-jasmine-webkit both have complicated dependencies and require more tweaking of the config file.
Any suggestions? Ideally I would need to include HTML/HAML fixtures, and be headless, but at this point I would be happy with anything that allows me to test my javascript with minimal configuration.

Jasmine is the best solution. We're using this to test all our JS code. It works beautifully with CoffeeScript as well.
But don't install the version of RubyGems.org (it's not been updated in a while), just get the latest from github, e.g. add to your Gemfile:
group :test do
gem 'jasmine', :git => 'https://github.com/pivotal/jasmine-gem.git'
end
Then you can run rake jasmine and go to http://localhost:8888 to run your tests. Headless webkit also works.

I was looking for something that would allow me to unit and functional test my javascript within MS Test in visual studio. Took me forever but I found WatiN. What WatiN will do is open internet explorer and run a web page. If you run your IDE as an administrator you can even open local HTML files.
I am currently using it with Visual Studio to run functional and unit tests on all my javascript. I think that so far this is the best solution out of the box for running javascript functional tests from your IDE. For my unit tests I used YUI test, but since you are opening a browser and running javascript within the browser you can use any javascript test framework you want (like qUnit).

I've been using QUnit, a simple yet effective Unit Testing library built on jQuery:
http://docs.jquery.com/QUnit

Hopefully late is better than never... I just wrote a small library that should solve your problem:
https://github.com/proxv/qlive-rails
It injects qunit and your qunit tests into live server responses. It also lets you set state server-side in advance of the tests (like logging in a user and setting content for the page) to reduce client-side mocking.
If you are using rspec, there's also an add-on that will run the qunit tests headlessly along with your other rspec examples.

Related

project structure for java web application with javascript providing dependency management, unit testing, ci and support for maven and eclipse?

I am writing a java webapp, but the bulk of the codebase is actually browser-side javascript, which depends on dojo, and uses qunit for unit testing.
With serious abuse of maven and git I was able to wire in javascript unit testing and javascript dependencies.
However I cannot get pom.xml into a shape where both maven command line and eclipse maven plugin would be able to compile and test the whole project.
Moreover neither sonar can realize the project have javascript components, nor eclipse can help much with the javascript parts.
For reference the project is here: https://github.com/magwas/worldmodel/
What would be the correct project structure to have one war in the end of the day which contains all the javascript with dependencies, unit tested, analized (with sonar), and ready to submit to jenkins?
I have found http://mojo.codehaus.org/javascript-maven-tools/, but I don't know whether
is putting the javascript part to a separate project better, and if so, how should it be included in the java one?
how should I handle the dependencies (dojo, dijit & quint)?
I realize it is a complex question, pointers to documentation or source code of projects which have already solved it would be welcome.
I have found this: http://mojo.codehaus.org/javascript-maven-tools/javascript-ria-archetype/index.html
This might be an answer to parts of the question.
Now I only have to figure out how to use it with dojo.

Concurrent/Continuous Testing for nodejs (ncrunch for nodejs)

I'm a big fan of the continuous testing setup offered by NCrunch in Visual Studio, and would love to have a similar setup with nodejs.
When writing JavaScript in node I use Sublime Text 2 as my editor, with tests written using Mocha.
I wondered if there was software (or a ST2 plugin) for achieving similar concurrent testing to that offered by NCrunch when writing .NET code?
After doing some digging around I've decided that the solution for the moment is:
Server-side: Mocha
mocha -w test
Using mocha's built-in watch functionality.
Client-side: Testacular
I'm now using testacular, which is truly awesome. It would be great it if had hooks for running the server-side watch progress in tandem, but not really a problem.
I haven't tried it myself yet but it looks promising: wallaby.js The description from the website has the following description
Wallaby.js is an intelligent test runner for JavaScript that
continuously runs your tests. It reports code coverage and other
results directly to your code editor immediately as you change your
code. Wallaby.js uses various tricks to run your tests as fast as
possible, such as dependency analysis to only execute tests affected
by your code changes and parallel test execution.

Unit testing in RequireJS and QUnit basics

I am just trying to get my head round unit testing in Javascript and RequireJS. I am building a web-app and obviously only want to have tests run in development not production builds.
Questions:
Do you just test when you want to, or do you have JS tests running
on every page load when in development?
If tests are only on demand
then how do you trigger your tests to run? Query strings (eg.
?testing=true) or something like that?
I just need an idea of how people go about testing in development. I am using BackboneJS, RequireJS and jQuery on the front end with a NodeJS/ExpressJS server on the backend.
For a Backbone project at work we have a maven build process that runs our automated javascript tests through jsTestDriver, and we read the results with Sonar. I usually run the tests manually (with 'mvn test'), but I could easily tell maven every time I save a file, for example. I wrote a post that shows how to integrate QUnit, Requirejs, and code coverage with JSTD that is independent of Maven: js-test-driver+qunit+coverage+requirejs. It also contains links to a QUnitAdapter that is way more up-to-date and developed than the one on the jsTestDriver site. I'll update this post when I manage to write about how I got jsTestDriver working with Maven and Sonar. Hope it helps.
Grunt is a popular JS build tool. There's something called grunt-watch that can monitor certain files for change, and execute tasks accordingly. You could easily run unit tests with something like this on every save.
Usually end-to-end tests take longer, and we use the CI for that. I've seen a presentation on Meteor TDD that does end-to-end tests after every save though.
There are many end-to-end test frameworks, and they can run in a headless browser like phantom js using a build tool like grunt. Some frameworks open an actual browser to run the tests, but run via command line and report results using XML.
If you break out your components enough, the tests could have a small enough scope to run on each save.
For some core code I use JsUnit + Rhino on build server. For more complex bits (usually interface) I use selenium (it also runs on build server). I don't test anything on page load, I only use not-compressed versions of scripts.
I don't any solution for integration tests.

How can we run JavaScript jasmine tests from Jenkins (Hudson) without loading a JSP?

We have a rich web client. Our controllers and service facades are written in coffeescript (JavaScript) and jquery. In the past they would have been java.
To run our JavaScript jasmine tests from Jenkins/Hudson, we use java's junit and htmlunit to load a test oriented jsp page which includes the jasmine specs.
When the Htmlunit tries to run, it blows up trying to getPage() probably because of an XML parser class path which is extremely challenging to track down in our world.
We just want to be able to run our JavaScript tests from Jenkins and have it report failure if a JavaScript test does not pass. We are just using jsp and htmlunit in order to run JavaScript tests. Can we load the JavaScript tests and javascript code into a JavaScript engine with Jenkins as the thing that kicks it off? If so, how?
Sounds like you're in a Java environment. My jasmine-maven-plugin might be a good fit.
Jasmine Reporters would also be a solution. It has instructions for running headlessly via PhantomJS for example, and it can generate JUnit XML so Jenkins can understand the test results natively, graphing test count, duration, and failure over time.
Also, the "xvfb-run" wrapper often provided with xvfb is a great help here, so you can do "xvfb-run phantomjs.runner.sh ..." in a truly headless environment.
I've previously solved this problem by running the tests with a node.js plugin called jasmine-node
This solution of course requires node.js and a few node modules to properly run the jasmine tests. There is no real browser running the tests, but an emulated one using a module called jsdom, which basically creates a headless browser, and more specifically, a DOM, which the tests can interact with.
There's node modules for jQuery, underscore and propably other too, so these can be tested too. You can even skip the whole browser emulation if you'd rather run the tests in a browser, though I find it too cumbersome compared to automated Jenkins testing.
jasmine-node generates jUnit test reports, which Jenkins can interpret just fine.
I just realized there is some jenkins-jasmine-node plugin that might ease this process.
Grunt is your friend
use grunt http://gruntjs.com/
with grunt jasimine https://github.com/gruntjs/grunt-contrib-jasmine
with nodejs http://nodejs.org/
on jenkins using https://wiki.jenkins-ci.org/display/JENKINS/NodeJS+Plugin
got this setup and it's really nice, plus this gives you a place to start making your build server do other nice things such as deployment, unit testing, etc you know, other nice things
Can you use selenium? That would actually use a real browser then and get as close to the real environment as possible.

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?
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.
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
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
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
Perhaps try jasmine-headless-webkit. This offers the capability to run your Jasmine specs within a headless WebKit browser.

Categories