伊斯坦布尔代码覆盖Mocha和AngularJS

时间:2016-06-09 14:33:59

标签: angularjs istanbul

我有一个非常大的AngularJS应用程序,我使用Mocha和Chai(和JSDom)测试浏览器。完美的工作。但是,我现在正在尝试使用Istanbul添加代码覆盖率,并且我一直遇到Angular错误的问题。

我的测试命令:mocha src/**/*.spec.js
覆盖率命令:istanbul cover _mocha src/**/*.spec.js

我得到的错误是:

TypeError: Cannot read property '1' of null
    at Function.annotate (node_modules/angular/angular.js:3810:24)
    at Function.angular.injector.$$annotate (node_modules/angular-mocks/angular-mocks.js:2300:36)
    at Object.invoke (node_modules/angular/angular.js:4514:36)
    at Context.workFn (node_modules/angular-mocks/angular-mocks.js:2517:20)
Error: Declaration Location
    at window.inject.angular.mock.inject (node_modules/angular-mocks/angular-mocks.js:2488:25)
    at Suite.describe (src/components/filters/emoticonize.spec.js:9:407)
    at Object.<anonymous> (src/components/filters/emoticonize.spec.js:9:183)
    at Object.Module._extensions.(anonymous function) [as .js] (node_modules/istanbul/lib/hook.js:107:24)
    at require (internal/module.js:20:19)
    at Array.forEach (native)
    at Object.Module._extensions.(anonymous function) [as .js] (node_modules/istanbul/lib/hook.js:109:37)
    at runFn (node_modules/istanbul/lib/command/common/run-with-cover.js:122:16)
    at node_modules/istanbul/lib/command/common/run-with-cover.js:251:17
    at node_modules/istanbul/lib/util/file-matcher.js:68:16
    at node_modules/async/lib/async.js:52:16
    at node_modules/async/lib/async.js:361:13
    at node_modules/async/lib/async.js:52:16
    at done (node_modules/async/lib/async.js:246:17)
    at node_modules/async/lib/async.js:44:16
    at node_modules/async/lib/async.js:358:17
    at FSReqWrap.oncomplete (fs.js:117:15)

这是错误发生的地方:https://github.com/angular/angular.js/blob/v1.4.x/src/auto/injector.js#L101。我已在那里记录fnText,并且在使用伊斯坦布尔运行测试时,该值为

_emoticonizeFilter_=>{__cov_OCFuCYwLZAl7nm5crSo1eg.s['7']++;emoticonizeFilter=_emoticonizeFilter_;}

我的测试:

require('../../../test-helper');
const expect = require('chai').expect;

describe('filter', () => {
  let emoticonizeFilter;

  beforeEach(angular.mock.module('components.filters'));
  beforeEach(inject((_emoticonizeFilter_) => {
    emoticonizeFilter = _emoticonizeFilter_;
  }));

  describe('emoticonize', () => {
    it('should replace a smiley :) with emoji ', () => {
      expect(emoticonizeFilter('Hello :)', true)).to.equal('Hello ');
    });

    it('should not replace smileys when given a falsy parameter', () => {
      expect(emoticonizeFilter('Hello :)', false)).to.equal('Hello :)');
    });
  });
});

因此,beforeEach注入了_emoticonizeFilter_。伊斯坦布尔正在添加Angular在其注释功能中不知道如何处理的东西。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

通过从Angular 1.4.x升级到1.5.x来修复。