Jasmine spec and Rails 3.1 asset pipeline

1,673 views
Skip to first unread message

Chris Nelson

unread,
Jun 11, 2011, 9:32:46 PM6/11/11
to Jasmine
Has anyone else been using jasmine in a rails 3.1 project? It seems
really convenient to me to have my jasmine specs served up from the
asset pipeline, this means I can reload a page to run my specs and and
have any dependencies get rebuilt. Particularly as I'm writing all my
code in coffeescript this seems quite handy. Is anyone else already
doing this, or interested? If so, I'll package what I have into a gem
and put it out on the git hubs.

Mike Gehard

unread,
Jun 12, 2011, 11:24:04 AM6/12/11
to Jasmine
Chris,

I've been thinking about this as well and would like to see what you
have.

Going to spend some time today looking into this so if you have a
chance to push to github it would be appreciated.

Mike

Chris Nelson

unread,
Jun 12, 2011, 12:46:12 PM6/12/11
to jasmi...@googlegroups.com
Hi Mike,

I have a working example here: git://github.com/superchris/backbone_coffeescript_demo.git

I added spec/assets/javascripts to the asset pipeline and put the jasmine code in vendor/assets.  I then wrote a spec.js.coffee which requires the application code, spec helper and specs (there is only one in this project, but it would grab any others as well).  I adapted the SpecRunner.html to reference the code from the right paths and it "just worked".  I'd like to take what I have here and package it up so it's easy to get this going on a rails 3.1 project.  I'd welcome any help on this.

--Chris

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To post to this group, send email to jasmi...@googlegroups.com.
To unsubscribe from this group, send email to jasmine-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jasmine-js?hl=en.


Mike Gehard

unread,
Jun 12, 2011, 4:38:23 PM6/12/11
to Jasmine
Interesting...good to see that it works.

This approach works great for Rails3.1 apps but it's got me thinking
about what to do for non-Rails3.1 apps. Why should they get all of
the fun. :-)

I'm thinking about trying to patch the jasmine-gem to serve up
coffeescript based specs and app files by using rack-coffee (https://
github.com/mattly/rack-coffee) so that you don't need to be running
Rails3.1 to be able to use coffeescript based specs.

Thanks for sharing your app/ideas.

On Jun 12, 10:46 am, Chris Nelson <ch...@gaslightsoftware.com> wrote:
> Hi Mike,
>
> I have a working example here: git://
> github.com/superchris/backbone_coffeescript_demo.git
>
> I added spec/assets/javascripts to the asset pipeline and put the jasmine
> code in vendor/assets.  I then wrote a spec.js.coffee which requires the
> application code, spec helper and specs (there is only one in this project,
> but it would grab any others as well).  I adapted the SpecRunner.html to
> reference the code from the right paths and it "just worked".  I'd like to
> take what I have here and package it up so it's easy to get this going on a
> rails 3.1 project.  I'd welcome any help on this.
>
> --Chris
>

Chris Nelson

unread,
Jun 12, 2011, 5:20:16 PM6/12/11
to jasmi...@googlegroups.com
Yeah, I'm more focused on how do we let specs integrate into the the asset pipeline right now.  There are so many advantages, compiling coffeescript is just one, but managing dependencies and letting us package assets into gems are even more compelling to me.  Kinda thinking of putting out what I have as a specific gem: jasmine_rails31 or something.  

--Chris

Brad Phelan

unread,
Jun 25, 2011, 5:41:01 AM6/25/11
to jasmi...@googlegroups.com
Hi Chris,

I've put a gem together that I think partially solves what you need. 


I've got jasmin working with the Rails 3.1 and the asset pipeline and it's possible
to pull assets into the specs that are gem packaged.

Perhaps if the features are not quite what you need you could use it as a starting point.
I only threw it together yesterday after getting frustrated with the current jasmine gem.


Regards

Brad

Justin Searls

unread,
Aug 19, 2011, 9:50:59 AM8/19/11
to Jasmine
I'm reviving this thread after independently playing with Chris's demo
& also Brad's gem.

Has there been any movement on this recently? I'm really interested to
learn what the current options are out there for integrating Jasmine
with Rails. Leveraging the asset pipeline seems like the smartest
thing to do moving forward.

Things I'm interested in figuring out the best approach that gets me
as many of these as possible:

1. Headless execution for CI (worst case, one could write a Cuke that
looks at a spec runner page and looks for green)
2. Dynamic coffee compilation
3. Picks up new assets/files as they're added (preventing a high
maintenance SpecRunner page)
4. SpecRunner individually sources each script as opposed to
concatenating them for easier debugging.
5. RSpec integration would be nifty

I doubt the official jasmine gem is immediately headed in this
direction. The asset pipeline represents such a major break from the
past that it would be difficult to maintain BC. (related:
http://groups.google.com/group/jasmine-js/browse_thread/thread/f094765024bb85e8
)

Maybe a new gem to address these and other niceties is called for. I
never thought I'd expect my Jasmine workflow on Java projects would
feel tighter than on Rails. I'm suspicious enough of that conclusion
that I'm willing to bet I'm just ignorant of a better way to do it.

Thoughts?

-Justin

Rajan Agaskar

unread,
Aug 23, 2011, 10:10:10 AM8/23/11
to jasmi...@googlegroups.com
I think 2 and 3 (and whatever work we need to actually use the pipeline) are probably near-term priorities for the Jasmine gem, but I think you're correct in that it's going to take awhile to get there. We haven't been great about timely acceptance of pull requests (we're trying to get that better), but I'd love to see patches go into jasmine-gem rather than fragmented into another Rails 3.1-only gem. 

It's possible we're eventually going to be pushed into splitting up the gem regardless. The original intent of having a single gem that meets many purposes was so people could simply type `gem install jasmine` and get going; as Rails 3.1 changes many things about how JS works and Rails 2 falls farther behind Rails 3 this is getting tricky to keep in place.  

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.

Justin Searls

unread,
Aug 26, 2011, 12:46:20 PM8/26/11
to Jasmine
In the meantime I had a pretty good experience with jasmine-headless-
webkit last week. I'm looking at adding a simple custom spec runner
ERB template that loads in the same stuff the jasmine.yml calls for so
you can also load the specs in browser when you want to. (Not to work
around or circumvent your great work, Rajan; just in the interest in
getting a good working solution for 3.1 today).

My sample project is at:

https://github.com/searls/jasmine-headless-webkit-rails-skeleton

Cheers,

Justin
> >http://groups.google.com/group/jasmine-js/browse_thread/thread/f09476...

Rajan Agaskar

unread,
Aug 27, 2011, 10:13:55 AM8/27/11
to jasmi...@googlegroups.com
Awesome! We are gearing up to take on the challenges of working with 3.1 and stuff like RequireJS, so these sorts of things will be great examples for us to follow. I am hoping we have a solution for Rails 3.1 folks before it gets officially released. 

Colin Jack

unread,
Sep 4, 2011, 6:06:42 PM9/4/11
to Jasmine
> We are gearing up to take on the challenges of working with 3.1 and
> stuff like RequireJS

Sounds great, I'd definitely be interested in trying out/using this
functionality when it exists.

Thanks,

Colin

Rajan Agaskar

unread,
Sep 10, 2011, 1:37:57 PM9/10/11
to jasmi...@googlegroups.com
I spent a few hours this morning adding extremely basic asset pipeline
support. The changes are pushed to the 1.2.rc1 branch of jasmine-gem
(http://github.com/pivotal/jasmine-gem).

Essentially the jasmine.yml file now takes asset_pipeline_paths, each
path you pass here will be mapped through the Sprockets asset
pipeline. We have a bit of a 'hack' in order to permit running files
in the spec directory through Sprockets -- if one of your
asset_pipeline_paths matches your spec_dir, we'll assume you want to
use Sprockets for specs (there's a long story about why the spec dir
gets mapped as __spec__ inside of relative to root, something we might
re-examine in Jasmine 2).

Files passed in will now have any Sprocket compatible engine
extensions stripped off so that we correctly request them from
Sprockets -- ie, app/assets/my_file.js.coffee will be requested by
Jasmine as /app/assets/my_file.js.

I believe Jasmine should do the right thing if you hand it scss files
in your stylesheets and coffee files in your helpers, but I haven't
yet tested it.

This is a pretty minimal spike, so there may be other issues; hoping
this meets the 80% case for most people -- please feel free to offer
your feedback!

Thanks!

Rajan


PS:
Here's a sample configuration file to use coffeescript for your src and specs:

# src_files
#
# Return an array of filepaths relative to src_dir to include before
jasmine specs.
# Default: []
#
# EXAMPLE:
#
# src_files:
# - lib/source1.js
# - lib/source2.js
# - dist/**/*.js
#
src_files:
- "app/assets/**/*.coffee"
# stylesheets
#
# Return an array of stylesheet filepaths relative to src_dir to
include before jasmine specs.
# Default: []
#
# EXAMPLE:
#
# stylesheets:
# - css/style.css
# - stylesheets/*.css
#
stylesheets:
- stylesheets/**/*.css

# helpers
#
# Return an array of filepaths relative to spec_dir to include before
jasmine specs.
# Default: ["helpers/**/*.js"]
#
# EXAMPLE:
#
# helpers:
# - helpers/**/*.js
#
helpers:
- helpers/**/*.js

# spec_files
#
# Return an array of filepaths relative to spec_dir to include.
# Default: ["**/*[sS]pec.js"]
#
# EXAMPLE:
#
# spec_files:
# - **/*[sS]pec.js
#
spec_files:
- '**/*[sS]pec.js.coffee'

# src_dir
#
# Source directory path. Your src_files must be returned relative to
this path. Will use root if left blank.
# Default: project root
#
# EXAMPLE:
#
# src_dir: public
#
src_dir:

# spec_dir
#
# Spec directory path. Your spec_files must be returned relative to this path.
# Default: spec/javascripts
#
# EXAMPLE:
#
# spec_dir: spec/javascripts
#
spec_dir: spec/javascripts

# asset_pipeline_paths
#
# Paths you would like to be served by the Sprockets asset pipeline.
#
# If you include your spec_dir (eg: - spec/javascripts ) here,
# Jasmine will use the Sprockets asset pipeline to build your spec files.
#
# Default: []
#
# EXAMPLE:
# asset_pipeline_paths:
# - app/assets
# - spec/javascripts
asset_pipeline_paths:
- app/assets
- spec/javascripts

Rajan Agaskar

unread,
Sep 10, 2011, 2:21:22 PM9/10/11
to jasmi...@googlegroups.com
I should note that this update doesn't yet support loading jquery from the assets path because the file doesn't exist on the local filesystem -- for now it'll need to be loaded from helpers, I suppose. 

Lille

unread,
Sep 17, 2011, 4:43:54 PM9/17/11
to Jasmine
Rajan,

Can you sketch out a little how to 'load jQuery from helpers'? I'm
hesitant to seek to discover this issue where I employ jQuery in my
specs, before maybe someone can indicate the trick.

(BTW - this is all very timely. To me, it has felt like UI behavior
coding was always an afterthought, if at all, in Rails land. Now, with
Node.js, the distinction is beginning to lose practical meaning, but
it'd be nice to see Rails catch up where it can.)

Lille

Rajan Agaskar

unread,
Sep 17, 2011, 6:31:33 PM9/17/11
to jasmi...@googlegroups.com
By default Jasmine will load files you place in spec/javascripts/helpers. You'll want to put the version of jquery that Rails provides (and jquery-ui, if you're using it) in there in order for Jasmine to be able to load it for your tests. Rails normally provides these to the asset pipeline via the jquery-rails gem, which jasmine doesn't yet interact with. 

Hope this helps!

Thanks for using Jasmine!

Rajan

Brad Phelan

unread,
Sep 18, 2011, 12:46:18 AM9/18/11
to jasmi...@googlegroups.com

As an alternative look at the jasminerice gem which does work with the rails 3.1 asset pipeline.

https://github.com/bradphelan/jasminerice

Lille

unread,
Sep 18, 2011, 8:37:36 PM9/18/11
to Jasmine
Thanks Rajan, for your post and change to jasmine.

I can confirm positive results from use of Rajan's update to jasmine
for my Rails 3.1 project involving tests of backbone.js using
CoffeeScript.

Rajan, for my part, I'd like to encourage you good folks at pivotal to
make any reasonable additional efforts to fully inculcate Rails 3.1
compatibility in the gem, as I expect it will be very useful to the
community.

Here is my version of Rajan's jasmine.yml:

# src_files
#
# Return an array of filepaths relative to src_dir to include before
jasmine specs.
# Default: []
#
# EXAMPLE:
#
# src_files:
# - lib/source1.js
# - lib/source2.js
# - dist/**/*.js
#
src_files:
- spec/javascripts/helpers/jquery-1.6.2.js
- spec/javascripts/helpers/jasmine_jquery-1.3.1.js
- vendor/assets/javascripts/underscore.js
- vendor/assets/javascripts/backbone.js
- vendor/assets/javascripts/backbone.localStorage-min.js
- vendor/assets/javascripts/handlebars.js
- app/assets/javascripts/backbone/fha2.js.coffee
# - helpers/jquery.js
# - helpers/jquery-ui.js
# - helpers/jquery_ujs.js
# - helpers/jasmine_jquery-1.3.1.js


# spec_files
#
# Return an array of filepaths relative to spec_dir to include.
# Default: ["**/*[sS]pec.js"]
#
# EXAMPLE:
#
# spec_files:
# - **/*[sS]pec.js
#
spec_files:
- '**/*[sS]pec.js'
asset_pipeline_paths:
- app/assets
- spec/javascripts

I used the backbone-rails gem to generate backbone stuff in this app.
In particular, app/assets/javascripts/backbone/fha2.js.coffee is as
follows:

#= require_self
#= require_tree ./templates
#= require_tree ./models
#= require_tree ./views
#= require_tree ./routers

window.TodoApp =
Models: {}
Collections: {}
Routers: {}
Views: {}

Thanks,

Lille

On Sep 18, 12:46 am, Brad Phelan <bradphe...@xtargets.com> wrote:
> As an alternative look at the jasminerice gem which does work with the rails
> 3.1 asset pipeline.
>
> https://github.com/bradphelan/jasminerice
> On Sep 18, 2011 12:31 AM, "Rajan Agaskar" <ra...@pivotallabs.com> wrote:
>
>
>
>
>
>
>
> > By default Jasmine will load files you place in spec/javascripts/helpers.
> > You'll want to put the version of jquery that Rails provides (and
> jquery-ui,
> > if you're using it) in there in order for Jasmine to be able to load it
> for
> > your tests. Rails normally provides these to the asset pipeline via the
> > jquery-rails gem, which jasmine doesn't yet interact with.
>
> > Hope this helps!
>
> > Thanks for using Jasmine!
>
> > Rajan
>
> ...
>
> read more »

Aaron McLeod

unread,
Sep 27, 2011, 8:24:27 AM9/27/11
to jasmi...@googlegroups.com
Setup my project with this branch the other day. I'm getting an error where createSpyObj is not found. I can plainly see it in the code of http://localhost:8888/__JASMINE_ROOT__/jasmine.js. Here's my spec file where it's complaining (line 27). http://pastie.org/2599511

Anyone else having this issue?


--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To post to this group, send email to jasmi...@googlegroups.com.
To unsubscribe from this group, send email to jasmine-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jasmine-js?hl=en.




--
Aaron McLeod
http://agmprojects.com

Aaron McLeod

unread,
Sep 28, 2011, 9:06:53 AM9/28/11
to jasmi...@googlegroups.com
Turns out changing it to jasmine.createSpyObj did the trick :). Still working on getting this test to pass, but ill figure it out eventually.

Kevin Fitzpatrick

unread,
Sep 28, 2011, 8:45:18 PM9/28/11
to Jasmine
I just tried this out with this jasmine.yml:

----------------------------------------------------------------------------------------
src_files:
- app/assets/**/*.js

spec_files:
- '**/*[sS]pec.js'
- '**/*[sS]pec.js.coffee'

src_dir:

spec_dir: spec/javascripts

asset_pipeline_paths:
- app/assets
- spec/javascripts
----------------------------------------------------------------------------------------
... and it worked fine for all JS files.

When I changed the src files to
----------------------------------------------------------------------------------------
src_files:
- app/assets/**/*.js
- app/assets/**/*.js.coffee
----------------------------------------------------------------------------------------

The server starts, but hangs when I refresh the browser.

Any ideas? I've been digging through the source, but I can't seem to
find where the trouble starts.

Thanks!
- K

Aaron McLeod

unread,
Sep 30, 2011, 2:28:49 PM9/30/11
to jasmi...@googlegroups.com
Hmm that's odd. Mine is essentially the same. Only different being is i specify the files i need in a particular order cause of dependencies. Here's mine: http://pastie.org/2618361
Aaron McLeod
http://agmprojects.com

Kevin Fitzpatrick

unread,
Oct 1, 2011, 7:28:05 PM10/1/11
to jasmi...@googlegroups.com, jasmi...@googlegroups.com
Yep I found that if I specified my files individually then it worked fine. It was the glob syntax that failed.

------

Kevin Fitzpatrick

@kfitzpatrick

camdub

unread,
Oct 4, 2011, 10:44:16 AM10/4/11
to Jasmine
I did the same, included the files I needed in specified order.
However, I'm still getting an error in the jasmine-html.js file.

The error is "Uncaught TypeError: Cannot call method 'complete' of
undefined" on line 87 of jasmine-html.js

Anyone know what's going on here?

On Oct 1, 5:28 pm, Kevin Fitzpatrick <ke...@pivotalsf.com> wrote:
> Yep I found that if I specified my files individually then it worked fine. It was the glob syntax that failed.
>
> ------
>
> Kevin Fitzpatrick
>
> @kfitzpatrick
> 267.254.6225
>
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages