Taranjit Kang
Taranjit Kang

Reputation: 2580

Jasmine: Adding Lodash to Testing

I'm using lodash in my application/component. Jasmine is complaining:

Failed: Cannot read property 'eq' of undefined

I've added, lodash to my karma.conf

such as:{ pattern: './node_modules/lodash/lodash.min.js', included: true, watched: false }

Still getting the issue, what is the correct way of including lodash?

Upvotes: 2

Views: 3836

Answers (2)

D. Smurygin
D. Smurygin

Reputation: 61

I solved this problem by adding these lines to "main.ts":

main.ts

//...

import _ from 'lodash';

declare global {
  const _: _.LoDashStatic;
}

//...

Also, I added lodash to karma.conf.js:

karma.conf.js

module.exports = function(config) {
    config.set({
          basePath: '',
          frameworks: ['jasmine', '@angular/cli'],
          files: [
            'node_modules/lodash/lodash.js'
          ],
          //...

Upvotes: 3

Alexandr Polyakov
Alexandr Polyakov

Reputation: 71

Just import lodash like this in your component:

import * as _ from 'lodash'

Upvotes: 7

Related Questions