Sandip Subedi
Sandip Subedi

Reputation: 1077

jasmine-gem doesn't load jquery properly

My jasmine specs are running fine. But when I have to use jquery syntax like $ I am getting following error :

ReferenceError: Can't find variable: $ in http://localhost:57132/__spec__/

So, I added gem "jasmine-jquery-rails"

After bundle install still the same error.

Now I decided to add jasmine-jquery on the jasmine.yml file, but still the same error.

Then I decided to download jasmine-jquery.js manually and put that on app/assets/javascripts and add that to jasmine.yml. Then I get error on all specs.

TypeError: Right-hand side of 'instanceof' is not an object

TypeError: $ is not a function

I don't understand what I am doing wrong here.

I think the order we load file is the issue here. But just couldn't figure this out.

Upvotes: 0

Views: 166

Answers (1)

Florian Kopp
Florian Kopp

Reputation: 73

I just started using using the Jasmine gem yesterday, but after fighting with it for a while, I got it to work for me.

What you need is to get JQuery to register the $ handler. Just adding the JQuery library to the src_files in yasmine.yml doesn't seem to help with that.

What DID work for me was to include assets/application.js in jasmine.yml as the first entry under src_files:

src_files:
  - assets/application.js
  - '../vendor/javascripts/*.js'
  - 'javascript/**/*.js'

(I got my actual source files in the javascript/ folder and use the ..vender/javascripts/ folder for third party libraries, this will be different for you.)

Make sure JQuery is required in assets/javascripts/application.js like this:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .

This is pretty much how your assets/javascripts/application.js should look when you generate a new application via the rails new command line tool.

The jasmine-jquery-rails gem doesn't help with your issue because it adds additional testing functionality, not fundamental JQuery support.

Good success with getting Jasmine set up!

Upvotes: 1

Related Questions