简体   繁体   中英

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.

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 ).

Just a little thing. Your file naming convention uses two extensions: my_spec.js.coffee

The jasmine.yml file is expecting just "spec" followed by "coffee". The two extensions are not matching this pattern.

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

Aire Shaw's excellent answer used the filename my_spec.coffee which matches the expectation of the jasmine.yml file.

If you wish to use the filename form my_spec.js.coffee (as well as .js and .coffee), you can change the jasmine.yml file to:

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

I use your file name convention for my coffeescript and this worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM