Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you include a javascript files from a CDN in Jasmine?

When using Jasmine in a Rails project, to keep the dependencies consistent in my Jasmine specs, I want to pull jquery from a cdn as is done on the real page. I try to do that like so, in my Jasmine.yml file:

helpers:
  - http://code.jquery.com/jquery-1.9.1.js

However, this never works, as when viewing the source of the localhost:8888 I get:

<script src="/__spec__/http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>

How do you this correctly?

like image 718
Oved D Avatar asked Feb 18 '13 20:02

Oved D


2 Answers

as answered in https://github.com/pivotal/jasmine-gem/issues/135

I wrote a helper to load external javascripts. It's not the best solution (i would prefer config file use) but it works for me.

var head = document.getElementsByTagName('head')[0];
var jQueryScript = document.createElement('script');
jQueryScript.setAttribute('type', 'text/javascript');
jQueryScript.setAttribute('src', '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js');
head.appendChild(jQueryScript);
like image 152
emrox Avatar answered Nov 15 '22 09:11

emrox


@emrox's answer is correct and works for jasmine 2.0.

However in jasmine 2.1.3 it doesn't. This is due to the fact that when the tests are run(i'm using Chutzpah) "http:" isn't pre-pended to the CDN reference. It is in version 2.0 of jasmine.

The fix I implemented that worked involved the following line of code:

jQueryScript.setAttribute('src', 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js');

Hopefully this helps someone before they decide to downgrade to 2.0!

like image 29
Shayan Zafar Avatar answered Nov 15 '22 10:11

Shayan Zafar