Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 - Use underscore (or any external library) while testing

I am using Angular2 RC 1 and Angular cli to run my tests.

I have a test that calls a function from my component that uses underscore. The test fails, saying

ReferenceError: _ is not defined

I have the typings for underscore - it works fine as long as I'm not running tests. I understand that it imports underscore in my index file along with all of the other vendor JS. However, when it's running a test, I don't know how to inject the dependency. I've tried importing it with import statements, but no such luck. Any ideas?

like image 671
Rob Avatar asked Sep 11 '26 14:09

Rob


1 Answers

Do you work with Karma?

Then you have to edit your karma.conf.js

Push the lodash/underscore file into the files array:

files: [
    { pattern: 'dist/vendor/lodash/lodash.js', included: true, watched: false }
]
like image 117
JV3 Avatar answered Sep 13 '26 17:09

JV3