Karma: [Support] How do you load CDN scripts?

Created on 22 Mar 2013  路  8Comments  路  Source: karma-runner/karma

Hello,

I'd like to unit test some functions which depends on jquery, YUI, and other libs available on CDN.
Is it possible to get them used by karma from their CDN url without having to download and maintain those files?

cheers,

Most helpful comment

@AndyBrownlie It may be a CORS issue - I just faced this myself with an identical situation. I declared the files in the karma config, but it wasn't actually loading the source due to CORS issues.

Adding this to the karma config file fixed it for me:

crossOriginAttribute: false,

All 8 comments

Yep, you can add an url into files. However, having local files would be faster (Karma uses heavy caching, with CDN you still need the request per file per run, even if it's 304, it still needs to hit the CDN server).

// in karma.conf.js
files = [
  // using angular from cdn
  'htpp://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js'
];

And if I want to use it with RequireJS?

I can't get it to work. I have tried this, and added it with different configuration options. And I still get "There is no timestamp for ".

Unless the htpp type is intentional, it won't work when trying to load files from the official Angular CDN

oleg@oleg-Lenovo:/var/www/front/ng-clip$ !!
karma start config/karma.conf.js 

/var/www/front/ng-clip/config/karma.conf.js:11
      'http://code.angularjs.org/1.2.9/angular.min.js',
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ERROR [config]: Invalid config file!
  SyntaxError: Unexpected string
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (/usr/lib/node_modules/karma/node_modules/coffee-script/lib/coffee-script/coffee-script.js:211:36)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.parseConfig (/usr/lib/node_modules/karma/lib/config.js:308:22)
    at Object.exports.start (/usr/lib/node_modules/karma/lib/server.js:176:20)
    at Object.<anonymous> (/usr/lib/node_modules/karma/bin/karma:19:39)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3

@akarl818 the requirejs adapter should ignore missing timestamps for urls https://github.com/karma-runner/karma-requirejs/issues/16

@BeOleg Sounds like you have a syntax error in your config file, can you post its content?

@vojtajina I have a similar issue with gapi.
I am using google api platform, where I load the js on index.html.

For karma, I have done as you suggested above and added the url (https://apis.google.com/js/platform.js) via files into the karma.conf.js.

However, when I execute my tests I get "ReferenceError: gapi is not defined". Is this because the script has not yet finished loading?

thanks in advance

@AndyBrownlie It may be a CORS issue - I just faced this myself with an identical situation. I declared the files in the karma config, but it wasn't actually loading the source due to CORS issues.

Adding this to the karma config file fixed it for me:

crossOriginAttribute: false,

hey @xveganxxxedgex this worked! great, thanks!

watch out with the above solution. it adds the script url once for each file.
I had an issue with https://unpkg.com/@webcomponents/custom-elements
calling the callbacks of the custom element multiple times...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

anius picture anius  路  3Comments

macjohnny picture macjohnny  路  5Comments

charpour picture charpour  路  3Comments

danielsiwiec picture danielsiwiec  路  5Comments

simonh1000 picture simonh1000  路  3Comments