Search code examples
angularunit-testingkarma-runnerlodashkarma-webpack

Lodash does not work in testing environment


I've got a problem with importing lodash into my testing environment. I'm testing service which uses lodash. In test suite I imported lodash this way:

import * as _ from "lodash";

Then in test I'm able to execute successfully:

console.log('lodash version:', _.VERSION);

But when I run service method, which uses lodash - I'm getting an error:

TypeError: Cannot read property 'find' of undefined

How is it possible?

karma.config.js:

files: [
  { pattern: './src/test.ts', watched: false },
  'node_modules/lodash/lodash.js'
],

Solution

  • Is the service method that you are testing using a "_" defined globally or perhaps imports it's own , overwriting your '_' with undefined (for whatever reason) ?