How do I debug errors from karma-server?

2k views Asked by At

I'm using karma along with karma-typescript (but this isn't an Angular project, so I'm not using angular-cli).

About half of my test runs generate an error after all of the tests have passed, and I'm stumped as to how to proceed. Here's the output.

$ karma start karma.conf.js --single-run --jenkins
21 02 2019 09:43:38.104:INFO [compiler.karma-typescript]: Compiling project using Typescript 3.2.4
21 02 2019 09:43:53.795:INFO [compiler.karma-typescript]: Compiled 21 files in 15446 ms.
21 02 2019 09:43:57.633:INFO [bundler.karma-typescript]: Bundled imports for 21 file(s) in 3328 ms.
21 02 2019 09:43:59.695:INFO [karma-server]: Karma v4.0.0 server started at http://0.0.0.0:9876/
21 02 2019 09:43:59.720:INFO [launcher]: Launching browsers ChromeHeadless with concurrency unlimited
21 02 2019 09:43:59.757:INFO [launcher]: Starting browser ChromeHeadless
21 02 2019 09:44:01.220:INFO [HeadlessChrome 72.0.3626 (Windows 10.0.0)]: Connected on socket 16JL3Xy0xm-ri3aBAAAA with id 59122393
HeadlessChrome 72.0.3626 (Windows 10.0.0): Executed 0 of 110 SUCCESS (0 secs / 0 secs)
[1A[2KHeadlessChrome 72.0.3626 (Windows 10.0.0): Executed 1 of 110 SUCCESS (0 secs / 0.061 secs)
...
[1A[2KHeadlessChrome 72.0.3626 (Windows 10.0.0): Executed 110 of 110 SUCCESS (2.695 secs / 2.341 secs)
TOTAL: 110 SUCCESS
21 02 2019 09:44:05.251:ERROR [karma-server]: { Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:111:27) errno: 'ECONNRESET', code: 'ECONNRESET', syscall: 'read' }
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command

(The --jenkins flag is used in my karma.conf.js primarily to determine whether ChromeHeadless is used instead of Chrome -- see below).

I'm fairly certain that this error isn't coming from my code.

Relevant packages:

    "@types/jasmine": "^3.3.8",
    "@types/jasminewd2": "^2.0.6",
    "awesome-typescript-loader": "^5.2.1",
    "jasmine": "^3.3.1",
    "jasmine-core": "^3.3.0",
    "karma": "^4.0.0",
    "karma-chrome-launcher": "^2.2.0",
    "karma-cli": "^2.0.0",
    "karma-jasmine": "^2.0.1",
    "karma-jasmine-html-reporter": "^1.4.0",
    "karma-junit-reporter": "^1.2.0",
    "karma-typescript": "^4.0.0",
    "ts-loader": "^5.3.3",
    "typescript": "^3.2.4"

Finally, here's my karma.conf.js:

module.exports = function(config) {
  const jenkins = config.jenkins;
  config.set({
    basePath: '',
    frameworks: ["jasmine", "karma-typescript"],
    plugins: [
      require('karma-jasmine'),
      require("karma-chrome-launcher"),
      require("karma-typescript"),
      jenkins ? require('karma-junit-reporter') : require('karma-jasmine-html-reporter')
    ],
    client:{
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    files: [
      "src/**/*.ts"
    ],
    preprocessors: {
      "src/**/*.ts": "karma-typescript"
    },
    reporters: [
      'progress', (jenkins ? 'junit' : 'kjhtml'), 'karma-typescript'
    ],
    browsers: [jenkins ? 'ChromeHeadless' : 'Chrome'],
    karmaTypescriptConfig: {
      tsconfig: "./tsconfig.json",
      coverageOptions: {
        exclude: [/mock/, /\.spec\.ts$/, /testing-utils/]
      }
    },
    colors: !jenkins,
    logLevel: config.LOG_INFO,
    autoWatch: !jenkins,
    singleRun: jenkins,
    junitReporter: {
      outputDir: 'karma-results',
      outputFile: 'karma-results.xml'
    }
  });
};
2

There are 2 answers

0
icc97 On

From a related answer I used the javascript-karma-verbose-reporter plugin to solve this.

The setup seems to differ quite a lot between different answers/comments. For me, running locally (not in Jenkins, Gulp etc), I ran

npm install --save-dev karma-verbose-reporter

By installing this my current version of karma (v4.4.1) recognises that there is a plugin and I didn't need to use the plugins section of karma.conf.js.

Then add karma-verbose-reporter to the reporters array in karma.conf.js:

module.exports = function(config) {
  config.set({ 
    // reporters configuration
    reporters: ['verbose']
  });
};

How this is relevant to your question is the output it gives (which isn't in the related answer). This is a section of my output, I'm using a screenshot as the colours are very helpful:

enter image description here

You can see that the ERROR gets repeated twice. The second time it is neatly bound to the name of the test where the error occurs:

27 02 2023 11:52:08.143:ERROR [HeadlessChrome 110.0.5481 (Linux 0.0.0) | react-input-mask | should show empty value when input switches from uncontrolled to controlled]: 'Warning: A component is changing an uncontrolled input of type %s to be controlled. Input elements should not switch from uncontrolled to controlled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component. More info: https://reactjs.org/docs/forms.html#controlled-components%s', 'undefined', '
    in input (created by InputMask)
    in InputMask'
0
Chris Malek On

I apologize that I can't help with debugging errors in general from the karma-server. However, if anybody is experiencing a similar error message I can maybe provide a little extra insight based on my experience with this error.

I recently got a very similar error (with karma 6.3.4, and jasmine 3.9.0):

04 01 2022 17:37:02.327:ERROR [karma-server]: UncaughtException: Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:209:20)
04 01 2022 17:37:02.328:ERROR [karma-server]: Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:209:20) {
  errno: -4077,
  code: 'ECONNRESET',
  syscall: 'read'
}

While the error message sadly doesn't point to a specific unit test. I determined that some of our unit tests were using a provider for WEBSOCKETs:

{ provide: WEBSOCKET, useFactory: (): typeof WebSocket => WebSocket },

Simply changing these to use a mocked up provider instead of a real WebSocket seems to have solved the problem. Something along the lines of:

{ provide: WebSocketService, useClass: WebSocketServiceMock},

Since we still have some web sockets using the real provider that don't seem to cause this problem. It is also just a likely that we just need to make sure we are properly closing our web sockets during these particular units tests rather than waiting and letting Karma deal with our garbage.

On a side note, there is a pending pull request in the Karma repository to hide this error message. However, it appears that the submitter has abandoned making the changes recommended in the review so it might be a long time before the code makes it into the master.