编程语言
首页 > 编程语言> > javascript – Typemoq和Angular-cli不能一起工作

javascript – Typemoq和Angular-cli不能一起工作

作者:互联网

一个基本的Generated Angular 2 cli application有一些业力 – 茉莉花测试.

如果你然后npm安装typemoq –save-dev

并将一些使用typemoq的东西添加到其中一个测试文件中.

import * as Moq from 'typemoq';

...

it('pointless test is pointless', async(() => {
   let carMock = Moq.Mock.ofInstance(Car);
}));

class Car { ... }

然后在运行测试时,会发生以下错误.有没有人见过这样的事情,使用带有angular-cli的typemoq或者一般情况下将依赖关系引入angular-cli项目?这有什么问题?

TypeError: Cannot read property ‘substr’ of undefined
at Function. (C:\Source\ng-example-app\foo\node_modules\webpack-sources\node_modules\source-map\lib\source-node.js:115:26)
at Array.forEach (native)
at SourceMapConsumer_eachMapping [as eachMapping] (C:\Source\ng-example-app\foo\node_modules\webpack-sources\node_modules\source-map\lib\source-map-consumer.js:155:14)
at Function.SourceNode_fromStringWithSourceMap [as fromStringWithSourceMap] (C:\Source\ng-example-app\foo\node_modules\webpack-sources\node_modules\source-map\lib\source-node.js:80:24)
at SourceMapSource.node (C:\Source\ng-example-app\foo\node_modules\webpack-sources\lib\SourceMapSource.js:42:20)
at ReplaceSource.node (C:\Source\ng-example-app\foo\node_modules\webpack-sources\lib\ReplaceSource.js:66:29)
at C:\Source\ng-example-app\foo\node_modules\webpack-sources\lib\ConcatSource.js:40:49
at Array.map (native)
at ConcatSource.node (C:\Source\ng-example-app\foo\node_modules\webpack-sources\lib\ConcatSource.js:39:60)
at C:\Source\ng-example-app\foo\node_modules\webpack-sources\lib\ConcatSource.js:40:49
at Array.map (native)
at ConcatSource.node (C:\Source\ng-example-app\foo\node_modules\webpack-sources\lib\ConcatSource.js:39:60)
at C:\Source\ng-example-app\foo\node_modules\webpack-sources\lib\ConcatSource.js:40:49
at Array.map (native)
at ConcatSource.node (C:\Source\ng-example-app\foo\node_modules\webpack-sources\lib\ConcatSource.js:39:60)
at C:\Source\ng-example-app\foo\node_modules\webpack-sources\lib\ConcatSource.js:40:49
at Array.map (native)
at ConcatSource.node (C:\Source\ng-example-app\foo\node_modules\webpack-sources\lib\ConcatSource.js:39:60)
at ConcatSource.proto.sourceAndMap (C:\Source\ng-example-app\foo\node_modules\webpack-sources\lib\SourceAndMapMixin.js:28:18)
at CachedSource.sourceAndMap (C:\Source\ng-example-app\foo\node_modules\webpack-sources\lib\CachedSource.js:51:28)
at Compilation. (C:\Source\ng-example-app\foo\node_modules\webpack\lib\SourceMapDevToolPlugin.js:57:32)
at Array.map (native)
at Compilation. (C:\Source\ng-example-app\foo\node_modules\webpack\lib\SourceMapDevToolPlugin.js:43:84)
at Array.forEach (native)
at Compilation. (C:\Source\ng-example-app\foo\node_modules\webpack\lib\SourceMapDevToolPlugin.js:42:11)
at Compilation.applyPlugins (C:\Source\ng-example-app\foo\node_modules\tapable\lib\Tapable.js:25:14)
at Compilation. (C:\Source\ng-example-app\foo\node_modules\webpack\lib\Compilation.js:616:10)
at Compilation.applyPluginsAsync (C:\Source\ng-example-app\foo\node_modules\tapable\lib\Tapable.js:73:70)
at Compilation. (C:\Source\ng-example-app\foo\node_modules\webpack\lib\Compilation.js:612:9)
at Compilation.applyPluginsAsync (C:\Source\ng-example-app\foo\node_modules\tapable\lib\Tapable.js:73:70)
at Compilation. (C:\Source\ng-example-app\foo\node_modules\webpack\lib\Compilation.js:608:8)
at Compilation.applyPluginsAsync (C:\Source\ng-example-app\foo\node_modules\tapable\lib\Tapable.js:73:70)
at Compilation.seal (C:\Source\ng-example-app\foo\node_modules\webpack\lib\Compilation.js:554:7)
at Compiler. (C:\Source\ng-example-app\foo\node_modules\webpack\lib\Compiler.js:468:16)
at C:\Source\ng-example-app\foo\node_modules\tapable\lib\Tapable.js:152:11
at Compilation. (C:\Source\ng-example-app\foo\node_modules\webpack\lib\Compilation.js:452:10)
at C:\Source\ng-example-app\foo\node_modules\webpack\lib\Compilation.js:423:12
at C:\Source\ng-example-app\foo\node_modules\webpack\lib\Compilation.js:335:10
at C:\Source\ng-example-app\foo\node_modules\webpack\node_modules\async\lib\async.js:52:16
at done (C:\Source\ng-example-app\foo\node_modules\webpack\node_modules\async\lib\async.js:246:17)
at C:\Source\ng-example-app\foo\node_modules\webpack\node_modules\async\lib\async.js:44:16
at C:\Source\ng-example-app\foo\node_modules\webpack\lib\Compilation.js:335:10
at C:\Source\ng-example-app\foo\node_modules\webpack\node_modules\async\lib\async.js:52:16
at done (C:\Source\ng-example-app\foo\node_modules\webpack\node_modules\async\lib\async.js:246:17)
at C:\Source\ng-example-app\foo\node_modules\webpack\node_modules\async\lib\async.js:44:16
at C:\Source\ng-example-app\foo\node_modules\webpack\lib\Compilation.js:335:10
at C:\Source\ng-example-app\foo\node_modules\webpack\node_modules\async\lib\async.js:52:16
at done (C:\Source\ng-example-app\foo\node_modules\webpack\node_modules\async\lib\async.js:246:17)
at C:\Source\ng-example-app\foo\node_modules\webpack\node_modules\async\lib\async.js:44:16
at C:\Source\ng-example-app\foo\node_modules\webpack\lib\Compilation.js:335:10
at C:\Source\ng-example-app\foo\node_modules\webpack\node_modules\async\lib\async.js:52:16
at done (C:\Source\ng-example-app\foo\node_modules\webpack\node_modules\async\lib\async.js:246:17)
at C:\Source\ng-example-app\foo\node_modules\webpack\node_modules\async\lib\async.js:44:16
at C:\Source\ng-example-app\foo\node_modules\webpack\lib\Compilation.js:335:10
at C:\Source\ng-example-app\foo\node_modules\webpack\node_modules\async\lib\async.js:52:16
at Object.async.forEachOf.async.eachOf (C:\Source\ng-example-app\foo\node_modules\webpack\node_modules\async\lib\async.js:236:30)
at Object.async.forEach.async.each (C:\Source\ng-example-app\foo\node_modules\webpack\node_modules\async\lib\async.js:209:22)
at Compilation.addModuleDependencies (C:\Source\ng-example-app\foo\node_modules\webpack\lib\Compilation.js:192:8)
at Compilation.processModuleDependencies (C:\Source\ng-example-app\foo\node_modules\webpack\lib\Compilation.js:177:7)
at C:\Source\ng-example-app\foo\node_modules\webpack\lib\Compilation.js:317:12
at C:\Source\ng-example-app\foo\node_modules\webpack\lib\Compilation.js:124:4
at Array.forEach (native)
at callback (C:\Source\ng-example-app\foo\node_modules\webpack\lib\Compilation.js:123:12)
at C:\Source\ng-example-app\foo\node_modules\webpack\lib\Compilation.js:147:10
at C:\Source\ng-example-app\foo\node_modules\webpack\lib\NormalModule.js:209:10
at C:\Source\ng-example-app\foo\node_modules\webpack\lib\NormalModule.js:164:10
at C:\Source\ng-example-app\foo\node_modules\loader-runner\lib\LoaderRunner.js:365:3
at iterateNormalLoaders (C:\Source\ng-example-app\foo\node_modules\loader-runner\lib\LoaderRunner.js:206:10)
at iterateNormalLoaders (C:\Source\ng-example-app\foo\node_modules\loader-runner\lib\LoaderRunner.js:213:10)
at C:\Source\ng-example-app\foo\node_modules\loader-runner\lib\LoaderRunner.js:228:3
at runSyncOrAsync (C:\Source\ng-example-app\foo\node_modules\loader-runner\lib\LoaderRunner.js:125:11)
at iterateNormalLoaders (C:\Source\ng-example-app\foo\node_modules\loader-runner\lib\LoaderRunner.js:224:2)
at iterateNormalLoaders (C:\Source\ng-example-app\foo\node_modules\loader-runner\lib\LoaderRunner.js:213:10)
at C:\Source\ng-example-app\foo\node_modules\loader-runner\lib\LoaderRunner.js:228:3
at context.callback (C:\Source\ng-example-app\foo\node_modules\loader-runner\lib\LoaderRunner.js:106:13)
at C:\Source\ng-example-app\foo\node_modules\postcss-loader\index.js:67:13
at process._tickCallback (internal/process/next_tick.js:103:7)

解决方法:

按照您的步骤,我已完成以下操作:

>安装最新的angular-cli(ver 1.0.0-beta.21):npm install -g angular-cli
>安装最新的typemoq(ver 1.0.2):npm install –save-dev typemoq
>创建一个新项目:ng new PROJECT_NAME&& cd PROJECT_NAME
>添加到./src/app/app.component.spec.ts:

import * as Moq from 'typemoq';
...

it('pointless test is pointless', () => {
    let carMock = Moq.Mock.ofInstance(Car);
});

class Car {
}

> run command ng test –watch = false返回:

Executed 4 of 4 SUCCESS (0.163 secs / 0.157 secs)

更新到最新的angular-cli和typemoq后,你还会遇到错误吗?

标签:javascript,typescript,angular,angular-cli
来源: https://codeday.me/bug/20190702/1352479.html