How to use CoffeeScript specifications 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 pipeline turned on. I would like to write my specifications in CoffeeScript, but I was not able to figure out what to name the file and configure jasmine.yml so that the specifications are matched and correctly analyzed.

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 tried calling the file my_spec.js.coffee. This does not work. Of course, the naming convention my_spec.js is working fine. I also tried messing around with spec_files in jasmine.yml without any success.

Thanks in advance to everyone who is struggling with a similar problem, I realized this and can help me on the road to writing a little less tedious specs.

+6
source share
2 answers

From Jasmine-Rails Github Homepage :

The jasmine rail gem fully supports the Rails pipeline, which means you can:

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

If you decide to use asset pipeline support, many jasmine.yml configurations become unnecessary , and you can rely on the Rails resource pipeline to do the tricky job of controlling which files are included in your testuite.

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

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

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

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

Now you can write the specification for checking Foo in spec / javascripts / my_spec.coffee.

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

The RoR project structure should be partially as follows:

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

You can check the characteristics with:

 bundle exec rake spec:javascript 

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

+6
source

Just a trifle. The file naming convention uses two extensions: my_spec.js.coffee

The jasmine.yml file expects only a "spec" followed by a "coffee". These two extensions do not match this pattern.

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

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

If you want to use the my_spec.js.coffee file name form (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 coffee shop and it worked for me.

+2
source

Source: https://habr.com/ru/post/947568/


All Articles