Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 322
Taranjit Kang Avatar asked Jul 29 '26 13:07

Taranjit Kang


2 Answers

Just import lodash like this in your component:

import * as _ from 'lodash'
like image 99
Alexandr Polyakov Avatar answered Aug 01 '26 01:08

Alexandr Polyakov


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'
          ],
          //...
like image 28
D. Smurygin Avatar answered Aug 01 '26 03:08

D. Smurygin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!