Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use CoffeeScript specs with Jasmine-Rails

I am running Rails 3.2.13 with Ruby version 2.0.0-p195. I am using the Jasmine-Rails gem version 0.4.5 with the asset pipeline enabled. I would like to write my specs in CoffeeScript but I have not been able to figure out how to name the file and configure the jasmine.yml such that the specs will be picked up and parsed properly.

Here is the contents of my jasmine.yml file:

src_dir: "app/assets/javascripts"

src_files:
 - "application.{js,coffee}"

spec_dir: spec/javascripts

helpers:
  - "helpers/**/*.{js,coffee}"

spec_files:
  - "**/*[Ss]pec.{js,coffee}"

I've tried naming the file my_spec.js.coffee. That doesn't work. Of course the my_spec.js naming convention works just fine. I also tried messing with the spec_files value in jasmine.yml without any success.

Thanks in advance to anyone that has struggle with a similar issue, figured it out, and can help me along the path of writing slightly less tedious specs.

like image 252
Rebekah Waterbury Avatar asked Jun 18 '13 15:06

Rebekah Waterbury


People also ask

Does rails still use CoffeeScript?

CoffeeScript is no longer recommended by the Rails community.

What is Rails CoffeeScript?

CoffeeScript is a lightweight language that compiles into JavaScript. It provides simple and easy to learn syntax avoiding the complex syntax of JavaScript. CoffeeScript is influenced by JavaScript, Ruby, YAML, Haskell, Perl, Python and has influenced MoonScript, LiveScript, and JavaScript.


1 Answers

From Jasmine-Rails's Github homepage:

The jasmine-rails gem fully supports the Rails asset pipeline which means you can:

  • use coffee_script or other Javascript precompilers for source or test files
  • use sprockets directives to control inclusion/exclusion of dependent files
  • leverage asset pipeline search paths to include assets from various sources/gems

If you choose to use the asset pipeline support, many of the jasmine.yml configurations become unnecessary and you can rely on the Rails asset pipeline to do the hard work of controlling what files are included in your testsuite.

# minimalist jasmine.yml configuration when leveraging asset pipeline
spec_files:
  - "**/*[Ss]pec.{js,coffee}"

This defines the name pattern for the spec files. That is, zero or more characters, followed by Spec or spec, and with an js or coffee extension.

Add Jasmine-Rails routes configuration to config/routes.rb.

mount JasmineRails::Engine => "/specs" if defined?(JasmineRails)

Now you can write a spec to test Foo in spec/javascripts/my_spec.coffee.

describe 'Foo', ->
  it 'does something', ->
    expect(1 + 1).toBe(2)

You RoR project structure should be partly like this:

RailsApp/
|---app/
|---config/
|   |---routes.rb
|---...
|---spec/
    |---javascripts/
        |---my_spec.coffee
        |---support/
            |---jasmine.yml

You can test the specs with:

bundle exec rake spec:javascript

Or you can start rails server and check the path for Jasmine-Rails (/specs).

like image 169
Arie Xiao Avatar answered Sep 17 '22 20:09

Arie Xiao