【问题标题】:Karma and Angular2 - Mixing Isolated and non-isolated tests causes runtime injection failureKarma 和 Angular2 - 混合隔离和非隔离测试会导致运行时注入失败
【发布时间】:2017-12-03 16:18:15
【问题描述】:

我正在尝试创建一个模型来支持 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/app。完整的日志输出可在this gist 中获得。

这是 WebStorm 树:

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

可重现的步骤:

  1. 克隆这个分叉:https://github.com/Nava2/angular-starter
  2. 运行,npm install
  3. 运行,npm run test

如果我删除app/model/entity.no-testbed.spec.ts,错误就会消失。我不确定它们为什么会发生。如果这不是我应该开发的方式,请告诉我,因为我不想把时间浪费在“实践不足的道路”上。

其他信息

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    • 2019-02-05
    • 1970-01-01
    相关资源
    最近更新 更多