Jasmine: Adding Lodash to Testing

淺唱寂寞╮ 提交于 2019-12-12 18:07:53

问题


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?


回答1:


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'
          ],
          //...



回答2:


Just import lodash like this in your component:

import * as _ from 'lodash'


来源:https://stackoverflow.com/questions/45801756/jasmine-adding-lodash-to-testing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!