简体   繁体   English

Angular2 单元测试。 “Npm 测试”仅成功运行一次,再也不会成功

[英]Angular2 unit tests. 'Npm test' runs successfully only once and never again

I'm trying to setup unit test for my Angular2 project following the directions here: https://angular.io/docs/ts/latest/testing/#!#setup Using this project first time I run npm test everything works fine.我正在尝试按照此处的说明为我的 Angular2 项目设置单元测试: https ://angular.io/docs/ts/latest/testing/#!#setup 在我第一次运行npm test时使用这个项目一切正常。 Than I press ctrl-c to stop testing session.比我按ctrl-c停止测试会话。 Next time I execute npm test without any changes to the project it fails with the following error:下次我执行npm test而不对项目进行任何更改时,它会失败并显示以下错误:

[1] Chrome 53.0.2785 (Windows 10 0.0.0) LOG: Error{originalErr: Error{}}
[1]
    Chrome 53.0.2785 (Windows 10 0.0.0): Executed 0 of 4 SUCCESS (0 secs / 0 secs)
[1] Missing error handler on `socket`.
[1] TypeError: Cannot set property '49007121' of null
[1]     at createHtmlResults (C:\Projects\Tmp\ng-karma-test\node_modules\karma-htmlfile-reporter\index.js:57:32)
[1]     at initializeHtmlForBrowser (C:\Projects\Tmp\ng-karma-test\node_modules\karma-htmlfile-reporter\index.js:78:5)
[1]     at HTMLReporter.onBrowserStart (C:\Projects\Tmp\ng-karma-test\node_modules\karma-htmlfile-reporter\index.js:91:5)
[1]     at Server.<anonymous> (C:\Projects\Tmp\ng-karma-test\node_modules\karma\lib\events.js:13:22)
[1]     at emitTwo (events.js:111:20)
[1]     at Server.emit (events.js:191:7)
[1]     at Browser.onStart (C:\Projects\Tmp\ng-karma-test\node_modules\karma\lib\browser.js:126:13)
[1]     at Socket.<anonymous> (C:\Projects\Tmp\ng-karma-test\node_modules\karma\lib\events.js:13:22)
[1]     at emitOne (events.js:101:20)
[1]     at Socket.emit (events.js:188:7)
[1]     at Socket.onevent (C:\Projects\Tmp\ng-karma-test\node_modules\socket.io\lib\socket.js:335:8)
[1]     at Socket.onpacket (C:\Projects\Tmp\ng-karma-test\node_modules\socket.io\lib\socket.js:295:12)
[1]     at Client.ondecoded (C:\Projects\Tmp\ng-karma-test\node_modules\socket.io\lib\client.js:193:14)
[1]     at Decoder.Emitter.emit (C:\Projects\Tmp\ng-karma-test\node_modules\component-emitter\index.js:134:20)
[1]     at Decoder.add (C:\Projects\Tmp\ng-karma-test\node_modules\socket.io-parser\index.js:247:12)
[1]     at Client.ondata (C:\Projects\Tmp\ng-karma-test\node_modules\socket.io\lib\client.js:175:18)
    Chrome 53.0.2785 (Windows 10 0.0.0) ERROR
[1]   Disconnectedundefined
    Chrome 53.0.2785 (Windows 10 0.0.0): Executed 0 of 4 DISCONNECTED (5.004 secs / 0 secs)
[1] 18 09 2016 18:34:21.946:ERROR [karma]: TypeError: Cannot read property '49007121' of null
[1]     at HTMLReporter.onBrowserComplete (C:\Projects\Tmp\ng-karma-test\node_modules\karma-htmlfile-reporter\index.js:95:23)
[1]     at Server.<anonymous> (C:\Projects\Tmp\ng-karma-test\node_modules\karma\lib\events.js:13:22)
[1]     at emitOne (events.js:101:20)
[1]     at Server.emit (events.js:188:7)
[1]     at Timeout._onTimeout (C:\Projects\Tmp\ng-karma-test\node_modules\karma\lib\browser.js:166:17)
[1]     at tryOnTimeout (timers.js:232:11)
[1]     at Timer.listOnTimeout (timers.js:202:5)
[1] karma start karma.conf.js exited with code 1

The number in quotes is different every time.引号中的数字每次都不同。 If I reboot my PC the unit test runs again but also only one time and than the same error again.如果我重新启动我的电脑,单元测试会再次运行,但也只运行一次,然后再次出现相同的错误。

Thanks.谢谢。

PS : Bringing all packages to the latest version gives the following errors: node_modules/@types/jasmine/index.d.ts(73,9): error TS2375: Duplicate number index signature. PS :将所有包升级到最新版本会出现以下错误:node_modules/@types/jasmine/index.d.ts(73,9): error TS2375: Duplicate number index signature。 node_modules/@types/jasmine/index.d.ts(127,9): error TS2374: Duplicate string index signature. node_modules/@types/jasmine/index.d.ts(127,9): 错误 TS2374: 重复的字符串索引签名。 node_modules/@types/node/index.d.ts(99,6): error TS2300: Duplicate identifier 'BufferEncoding'. node_modules/@types/node/index.d.ts(99,6): 错误 TS2300: 重复标识符“BufferEncoding”。 node_modules/@types/node/index.d.ts(544,26): error TS2300: Duplicate identifier 'Buffer'. node_modules/@types/node/index.d.ts(544,26): 错误 TS2300: 重复标识符“缓冲区”。 node_modules/@types/node/index.d.ts(544,50): error TS2300: Duplicate identifier 'SlowBuffer'. node_modules/@types/node/index.d.ts(544,50): 错误 TS2300: 重复标识符“SlowBuffer”。 node_modules/@types/node/index.d.ts(565,18): error TS2300: Duplicate identifier 'EventEmitter'. node_modules/@types/node/index.d.ts(565,18): 错误 TS2300: 重复标识符“EventEmitter”。 node_modules/@types/node/index.d.ts(724,18): error TS2300: Duplicate identifier 'Agent'. node_modules/@types/node/index.d.ts(724,18): 错误 TS2300: 重复标识符“代理”。 node_modules/@types/node/index.d.ts(769,18): error TS2300: Duplicate identifier 'Worker'. node_modules/@types/node/index.d.ts(769,18): 错误 TS2300: 重复标识符“工人”。 node_modules/@types/node/index.d.ts(1070,18): error TS2300: Duplicate identifier 'Script'. node_modules/@types/node/index.d.ts(1070,18): 错误 TS2300: 重复标识符“脚本”。 node_modules/@types/node/index.d.ts(1912,18): error TS2300: Duplicate identifier 'TLSSocket'. node_modules/@types/node/index.d.ts(1912,18): 错误 TS2300: 重复标识符“TLSSocket”。 node_modules/@types/node/index.d.ts(2203,18): error TS2300: Duplicate identifier 'Stream'. node_modules/@types/node/index.d.ts(2203,18): 错误 TS2300: 重复标识符“流”。 node_modules/@types/node/index.d.ts(2214,18): error TS2300: Duplicate identifier 'Readable'. node_modules/@types/node/index.d.ts(2214,18): 错误 TS2300: 重复标识符“可读”。 node_modules/@types/node/index.d.ts(2237,18): error TS2300: Duplicate identifier 'Writable'. node_modules/@types/node/index.d.ts(2237,18): 错误 TS2300: 重复标识符“可写”。 node_modules/@types/node/index.d.ts(2255,18): error TS2300: Duplicate identifier 'Duplex'. node_modules/@types/node/index.d.ts(2255,18): 错误 TS2300: 重复标识符“双工”。 node_modules/@types/node/index.d.ts(2276,18): error TS2300: Duplicate identifier 'Transform'. node_modules/@types/node/index.d.ts(2276,18): 错误 TS2300: 重复标识符“转换”。 node_modules/@types/node/index.d.ts(2298,18): error TS2300: Duplicate identifier 'PassThrough'. node_modules/@types/node/index.d.ts(2298,18): 错误 TS2300: 重复标识符“PassThrough”。 node_modules/@types/node/index.d.ts(2369,5): error TS2300: Duplicate identifier 'export='. node_modules/@types/node/index.d.ts(2369,5): 错误 TS2300: 重复标识符 'export='。 node_modules/@types/node/index.d.ts(2391,18): error TS2300: Duplicate identifier 'Domain'. node_modules/@types/node/index.d.ts(2391,18): 错误 TS2300: 重复标识符“域”。 typings/globals/node/index.d.ts(78,6): error TS2300: Duplicate identifier 'BufferEncoding'.打字/全局/节点/index.d.ts(78,6): 错误 TS2300: 重复标识符 'BufferEncoding'。 typings/globals/node/index.d.ts(516,9): error TS2502: 'BuffType' is referenced directly or indirectly in its own type annotation. Typings/globals/node/index.d.ts(516,9): 错误 TS2502: 'BuffType' 在它自己的类型注释中被直接或间接引用。 typings/globals/node/index.d.ts(517,9): error TS2502: 'SlowBuffType' is referenced directly or indirectly in its own type annotation. Typings/globals/node/index.d.ts(517,9): 错误 TS2502: 'SlowBuffType' 在它自己的类型注释中被直接或间接引用。 typings/globals/node/index.d.ts(518,26): error TS2300: Duplicate identifier 'Buffer'.打字/全局/节点/index.d.ts(518,26):错误TS2300:重复标识符“缓冲区”。 typings/globals/node/index.d.ts(518,50): error TS2300: Duplicate identifier 'SlowBuffer'. Typings/globals/node/index.d.ts(518,50): 错误 TS2300: 重复标识符“SlowBuffer”。 typings/globals/node/index.d.ts(539,18): error TS2300: Duplicate identifier 'EventEmitter'.打字/全局/节点/index.d.ts(539,18):错误TS2300:重复标识符'EventEmitter'。 typings/globals/node/index.d.ts(698,18): error TS2300: Duplicate identifier 'Agent'. Typings/globals/node/index.d.ts(698,18): 错误 TS2300: 重复标识符“代理”。 typings/globals/node/index.d.ts(743,18): error TS2300: Duplicate identifier 'Worker'. Typings/globals/node/index.d.ts(743,18): 错误 TS2300: 重复标识符“工人”。 typings/globals/node/index.d.ts(1044,18): error TS2300: Duplicate identifier 'Script'. Typings/globals/node/index.d.ts(1044,18): 错误 TS2300: 重复标识符“脚本”。 typings/globals/node/index.d.ts(1886,18): error TS2300: Duplicate identifier 'TLSSocket'. Typings/globals/node/index.d.ts(1886,18): 错误 TS2300: 重复标识符“TLSSocket”。 typings/globals/node/index.d.ts(2177,18): error TS2300: Duplicate identifier 'Stream'.打字/全局/节点/index.d.ts(2177,18): 错误 TS2300: 重复标识符“流”。 typings/globals/node/index.d.ts(2188,18): error TS2300: Duplicate identifier 'Readable'. Typings/globals/node/index.d.ts(2188,18): 错误 TS2300: 重复标识符“可读”。 typings/globals/node/index.d.ts(2211,18): error TS2300: Duplicate identifier 'Writable'. Typings/globals/node/index.d.ts(2211,18): 错误 TS2300: 重复标识符“可写”。 typings/globals/node/index.d.ts(2229,18): error TS2300: Duplicate identifier 'Duplex'. Typings/globals/node/index.d.ts(2229,18): 错误 TS2300: 重复标识符“双工”。 typings/globals/node/index.d.ts(2250,18): error TS2300: Duplicate identifier 'Transform'. Typings/globals/node/index.d.ts(2250,18): 错误 TS2300: 重复标识符“转换”。 typings/globals/node/index.d.ts(2272,18): error TS2300: Duplicate identifier 'PassThrough'. Typings/globals/node/index.d.ts(2272,18): 错误 TS2300: 重复标识符“PassThrough”。 typings/globals/node/index.d.ts(2343,5): error TS2300: Duplicate identifier 'export='.打字/全局/节点/index.d.ts(2343,5):错误TS2300:重复标识符'export='。 typings/globals/node/index.d.ts(2365,18): error TS2300: Duplicate identifier 'Domain'. Typings/globals/node/index.d.ts(2365,18): 错误 TS2300: 重复标识符“域”。 npm ERR! npm 错误! Test failed.测试失败。 See above for more details.有关更多详细信息,请参见上文。

I've been facing this issue today.我今天一直面临这个问题。 It seems that it occurs when there is an underlying error that needs to be fixed.它似乎发生在存在需要修复的潜在错误时。 I haven't been unable to fix my error so far, so this may not fully resolve your problem.到目前为止,我一直无法修复我的错误,因此这可能无法完全解决您的问题。

If you add/change these settings in your karma.conf.js file you should be able to see what's causing the errors when your tests are running:如果您在karma.conf.js文件中添加/更改这些设置,您应该能够在测试运行时看到导致错误的原因:

config.set({
    ...
    autoWatch: false,
    singleRun: true
});

This should also prevent karma from exiting when the error occurs.这也应该防止 karma 在发生错误时退出。

Is setup problem, you need add, for example:是设置问题,需要添加,例如:

autoWatchBatchDelay: 1000

in karma.conf.js在 karma.conf.js 中

autoWatchBatchDelay: autoWatchBatchDelay:
Type: Number类型:数字
Default: 250默认值:250
Description :说明
When Karma is watching the files for changes, it tries to batch multiple changes into a single run so that the test runner doesn't try to start and restart running tests more than it should.当 Karma 正在监视文件的更改时,它会尝试将多个更改批处理到单个运行中,以便测试运行器不会尝试启动和重新启动运行测试的次数过多。 The configuration setting tells Karma how long to wait (in milliseconds) after any changes have occurred before starting the test process again.配置设置告诉 Karma 在发生任何更改后在再次开始测试过程之前等待多长时间(以毫秒为单位)。

Regards问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM