Karma和Angular2 - 混合隔离和非隔离测试会导致运行时注入失败

时间:2017-06-29 20:03:37

标签: angular unit-testing typescript karma-runner karma-webpack

我正在尝试创建一个模型来支持Angular 2应用。我是Angular2的新手,所以请耐心等待。

该模型是我们想要移动到TypeScript的旧Java代码库的端口。在这样做时,我们正在重新创建将被归类为"isolated" test的测试。

问题:

添加隔离测试后,来自种子的所有 Angular2测试在Karma中失败并出现类似错误:

Chrome 59.0.3071 (Windows 10 0.0.0) Home should have a title FAILED
    Failed: Unexpected value 'HomeComponent' declared by the module 'DynamicTestModule'. Please add a @Pipe/@Directive/@Component annotation.
    Error: Unexpected value 'HomeComponent' declared by the module 'DynamicTestModule'. Please add a @Pipe/@Directive/@Component annotation.
        at syntaxError (webpack:///~/@angular/compiler/@angular/compiler.es5.js:1540:21 <- config/spec-bundle.js:33745:34) [ProxyZone]
        at webpack:///~/@angular/compiler/@angular/compiler.es5.js:14542:0 <- config/spec-bundle.js:46747:40 [ProxyZone]
        at Array.forEach (<anonymous>) [ProxyZone]
        at CompileMetadataResolver.getNgModuleMetadata (webpack:///~/@angular/compiler/@angular/compiler.es5.js:14524:0 <- config/spec-bundle.js:46729:54) [ProxyZone]
        at JitCompiler._loadModules (webpack:///~/@angular/compiler/@angular/compiler.es5.js:25630:25 <- config/spec-bundle.js:57835:66) [ProxyZone]
...snip...
        at Zone.run (webpack:///~/zone.js/dist/zone.js:125:0 <- config/spec-bundle.js:70398:43) [<root> => ProxyZone]
        at Object.<anonymous> (webpack:///~/zone.js/dist/jasmine-patch.js:102:0 <- config/spec-bundle.js:69881:34) [<root>]
        at ZoneQueueRunner.jasmine.QueueRunner.ZoneQueueRunner.execute (webpack:///~/zone.js/dist/jasmine-patch.js:132:0 <- config/spec-bundle.js:69911:42) [<root>]
        at Zone.runTask (webpack:///~/zone.js/dist/zone.js:165:0 <- config/spec-bundle.js:70438:47) [<root> => <root>]
        at drainMicroTaskQueue (webpack:///~/zone.js/dist/zone.js:593:0 <- config/spec-bundle.js:70866:35) [<root>]
        at <anonymous> [<root>]
    Error: Unexpected value 'HomeComponent' declared by the module 'DynamicTestModule'. Please add a @Pipe/@Directive/@Component annotation.
        at syntaxError (webpack:///~/@angular/compiler/@angular/compiler.es5.js:1540:21 <- config/spec-bundle.js:33745:34) [ProxyZone]
        at webpack:///~/@angular/compiler/@angular/compiler.es5.js:14542:0 <- config/spec-bundle.js:46747:40 [ProxyZone]
        at Array.forEach (<anonymous>) [ProxyZone]
        at CompileMetadataResolver.getNgModuleMetadata (webpack:///~/@angular/compiler/@angular/compiler.es5.js:14524:0 <- config/spec-bundle.js:46729:54) [ProxyZone]
...snip...
        at Function.TestBed.createComponent (webpack:///~/@angular/core/@angular/core/testing.es5.js:610:0 <- config/spec-bundle.js:17189:29) [ProxyZone]
        at Object.<anonymous> (webpack:///src/app/home/home.component.spec.ts:43:0 <- config/spec-bundle.js:73448:37) [ProxyZone]
        at ProxyZoneSpec.onInvoke (webpack:///~/zone.js/dist/proxy.js:79:0 <- config/spec-bundle.js:70169:39) [ProxyZone]
        at Zone.run (webpack:///~/zone.js/dist/zone.js:125:0 <- config/spec-bundle.js:70398:43) [<root> => ProxyZone]
        at Object.<anonymous> (webpack:///~/zone.js/dist/jasmine-patch.js:104:0 <- config/spec-bundle.js:69883:34) [<root>]
    TypeError: Cannot read property 'title' of undefined
        at Object.<anonymous> (webpack:///src/app/home/home.component.spec.ts:54:0 <- config/spec-bundle.js:73459:22)
        at ZoneDelegate.invoke (webpack:///~/zone.js/dist/zone.js:365:0 <- config/spec-bundle.js:70638:26)
        at ProxyZoneSpec.onInvoke (webpack:///~/zone.js/dist/proxy.js:79:0 <- config/spec-bundle.js:70169:39)
        at ZoneDelegate.invoke (webpack:///~/zone.js/dist/zone.js:364:0 <- config/spec-bundle.js:70637:32)
        at Zone.run (webpack:///~/zone.js/dist/zone.js:125:0 <- config/spec-bundle.js:70398:43)
        at Object.<anonymous> (webpack:///~/zone.js/dist/jasmine-patch.js:104:0 <- config/spec-bundle.js:69883:34)

我遗漏了更多错误,因为它们对于每个规范文件都是相似的:https://github.com/Nava2/angular-starter/tree/master/src/appthis gist中提供了完整的日志输出。

这是WebStorm树:

WebStorm Test Tree

如果我删除了隔离测试,Angular2测试就不会再出现错误了。

可重复的步骤:

  1. 克隆此分叉:https://github.com/Nava2/angular-starter
  2. 运行,npm install
  3. 运行,npm run test
  4. 如果我删除app/model/entity.no-testbed.spec.ts,错误就会消失。我不确定他们为什么会这样。如果这不是我应该发展的方式,请告诉我,因为我不想浪费时间在一条不太熟练的道路上。&#34;

    其他信息

    • 操作系统:Windows 10
    • 节点版本:v8.1.2

1 个答案:

答案 0 :(得分:0)

查看spec文件的源代码会有所帮助。听起来你还没有导入HomeComponent。