简体   繁体   中英

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. Than I press ctrl-c to stop testing session. Next time I execute npm test without any changes to the project it fails with the following error:

[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. node_modules/@types/jasmine/index.d.ts(127,9): error TS2374: Duplicate string index signature. node_modules/@types/node/index.d.ts(99,6): error TS2300: Duplicate identifier 'BufferEncoding'. node_modules/@types/node/index.d.ts(544,26): error TS2300: Duplicate identifier 'Buffer'. node_modules/@types/node/index.d.ts(544,50): error TS2300: Duplicate identifier 'SlowBuffer'. node_modules/@types/node/index.d.ts(565,18): error TS2300: Duplicate identifier 'EventEmitter'. node_modules/@types/node/index.d.ts(724,18): error TS2300: Duplicate identifier 'Agent'. node_modules/@types/node/index.d.ts(769,18): error TS2300: Duplicate identifier 'Worker'. node_modules/@types/node/index.d.ts(1070,18): error TS2300: Duplicate identifier 'Script'. node_modules/@types/node/index.d.ts(1912,18): error TS2300: Duplicate identifier 'TLSSocket'. node_modules/@types/node/index.d.ts(2203,18): error TS2300: Duplicate identifier 'Stream'. node_modules/@types/node/index.d.ts(2214,18): error TS2300: Duplicate identifier 'Readable'. node_modules/@types/node/index.d.ts(2237,18): error TS2300: Duplicate identifier 'Writable'. node_modules/@types/node/index.d.ts(2255,18): error TS2300: Duplicate identifier 'Duplex'. node_modules/@types/node/index.d.ts(2276,18): error TS2300: Duplicate identifier 'Transform'. node_modules/@types/node/index.d.ts(2298,18): error TS2300: Duplicate identifier 'PassThrough'. node_modules/@types/node/index.d.ts(2369,5): error TS2300: Duplicate identifier 'export='. node_modules/@types/node/index.d.ts(2391,18): error TS2300: Duplicate identifier 'Domain'. typings/globals/node/index.d.ts(78,6): error TS2300: Duplicate identifier '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(517,9): error TS2502: 'SlowBuffType' is referenced directly or indirectly in its own type annotation. typings/globals/node/index.d.ts(518,26): error TS2300: Duplicate identifier 'Buffer'. typings/globals/node/index.d.ts(518,50): error TS2300: Duplicate identifier 'SlowBuffer'. typings/globals/node/index.d.ts(539,18): error TS2300: Duplicate identifier 'EventEmitter'. typings/globals/node/index.d.ts(698,18): error TS2300: Duplicate identifier 'Agent'. typings/globals/node/index.d.ts(743,18): error TS2300: Duplicate identifier 'Worker'. typings/globals/node/index.d.ts(1044,18): error TS2300: Duplicate identifier 'Script'. typings/globals/node/index.d.ts(1886,18): error TS2300: Duplicate identifier 'TLSSocket'. typings/globals/node/index.d.ts(2177,18): error TS2300: Duplicate identifier 'Stream'. typings/globals/node/index.d.ts(2188,18): error TS2300: Duplicate identifier 'Readable'. typings/globals/node/index.d.ts(2211,18): error TS2300: Duplicate identifier 'Writable'. typings/globals/node/index.d.ts(2229,18): error TS2300: Duplicate identifier 'Duplex'. typings/globals/node/index.d.ts(2250,18): error TS2300: Duplicate identifier 'Transform'. typings/globals/node/index.d.ts(2272,18): error TS2300: Duplicate identifier 'PassThrough'. typings/globals/node/index.d.ts(2343,5): error TS2300: Duplicate identifier 'export='. typings/globals/node/index.d.ts(2365,18): error TS2300: Duplicate identifier 'Domain'. npm ERR! 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:

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

This should also prevent karma from exiting when the error occurs.

Is setup problem, you need add, for example:

autoWatchBatchDelay: 1000

in karma.conf.js

autoWatchBatchDelay:
Type: Number
Default: 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. The configuration setting tells Karma how long to wait (in milliseconds) after any changes have occurred before starting the test process again.

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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