JESii
JESii

Reputation: 4937

jasmine not loading my custom js file

I am unable to get jasmine to load my custom javascript file, even though it works perfectly in the browser. I've reduced the javascript to the minimum to avoid any possibility of errors and I still get a failing test on the simplest thing.

Custom ARB.js file:

$(document).ready(function() {
  alert('typeof ARB: ' + typeof ARB);
    });
ARB = {};
ARB.VERSION = "V1.01.00 2012-08-24";

jasmine configuration file snippet (I'm on Rails 3.0.9):

src_files:
  - "public/javascripts/**/*.js"
  - "public/javascripts/ARB.js"

This test:

  it('should define the custom javascript functions', function() {
    expect(typeof ARB).toEqual('object');
    });

fails with:

Expected 'undefined' to equal 'object'.

jQuery gets loaded and so does my application.js file. When running jasmine, I get no alert message, but I do when I run my app.

What am I missing?

UPDATE: If I remove the $(document).ready function, jasmine passes all the tests - what's that all about?

Upvotes: 1

Views: 1225

Answers (1)

JESii
JESii

Reputation: 4937

shioyama gave me the pointer that I needed to figure this out: my custom ARB.js file was getting loaded before the jquery files so it didn't have access to the $(document).ready function. What I had to do was explicitly spell out the order in which my javascript files were to be loaded. Here's what I put in my jasmine config file at /spec/javascripts/support/jasmine.yml:

src_files:
  - "public/javascripts/**/jquery.js"
  - "public/javascripts/**/jq*.js"
  - "public/javascripts/**/application.js"
  - "app/assets/javascripts/**/*.js"
  - "public/javascripts/ARB.js"

I first force the main jquery.js file to load, then all the other jquery files, then application.js, then any other javascript files that are located in the assets directory, and finally my custom javascript file.

This works for me because I'm still on Rails 3.0.9 and starting the migration to 3.1+ and the asset pipeline.

Upvotes: 2

Related Questions