Luke Flournoy Luke Flournoy - 20 days ago 5
Javascript Question

Can't find variable: _ when using jasmine to test angularjs service

I am testing an angularjs service that uses the lodash library. when I run the tests through karma-jasmine I get an error

ReferenceError: Can't find variable: _

Is there a way to configure karma/jasmine to use lodash?

Answer

Since you won't have a HTML file in your tests to load the script for you, you'll need to include the library in your karma config file in the files property. You can read more about it here: http://karma-runner.github.io/1.0/config/files.html

It will look something like this

In karma.conf.js

...
files: [
  'path/to/lodash.js'
]
...

Hope that helps!