Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Karma: uncaught ReferenceError $ is not defined

I have seen many other answers on this site but none of them worked for me.

The problem: I need to load jQuery with an URL, not with a local path; and I think I can't add thoose to "files" on karma.conf.js.

Everything seems to be fine, but when I try to use karma to test it, it returns: Uncaught ReferenceError: $ is not defined.

The order of the scripts is fine in the two .html we use.

like image 649
dquijada Avatar asked Mar 10 '15 20:03

dquijada


People also ask

What does uncaught ReferenceError mean?

The most common reason behind the error "Uncaught ReferenceError: $ is not defined" is executing the jQuery code before the jQuery library file has loaded. Therefore make sure that you're executing the jQuery code only after jQuery library file has finished loading.

How do I fix uncaught ReferenceError is not defined in jQuery?

To solve this error, we simply add the jQuery CDN link or downloaded jQuery path link inside the head section. Example: This example resolves the error by adding the required CDN link inside the <script> tag.

What is uncaught ReferenceError in Javascript?

The Javascript ReferenceError occurs when referencing a variable that does not exist or has not yet been initialized in the current scope.


1 Answers

Simple include the jQuery path in the karma.conf.js (as for Karma 0.12 at least):

module.exports = function(config) {
  config.set({

    files: [
      'https://code.jquery.com/jquery-1.11.2.min.js'
      ...
    ],

    ...
  });
};

I've tested it yesterday because I needed a similar thing for AngularJS.

like image 147
MarcoL Avatar answered Oct 15 '22 07:10

MarcoL