daw
daw

Reputation: 2049

How to capture console.error with Karma unit test runner?

Karma will capture any messages written to console.log but not anything written to console.error. I understand that karma uses log4js under the hood.

Is there any way to configure karma to capture console.error?

Upvotes: 7

Views: 8928

Answers (2)

Carlos Morales
Carlos Morales

Reputation: 5916

You could capture the console.error as described in this blog post

describe('TestSuite', function() {
  var oldError = console.error;
  beforeEach(function() {
    console.error = function(message) {
      throw new Error(message);
    };
  });
  return afterEach(function() {
    return console.error = oldError;
  });

  it('should fail', function() {
    console.error("Oops!")
  });

Upvotes: 4

Vojta
Vojta

Reputation: 23051

Karma does capture console.error (see https://github.com/karma-runner/karma/blob/415d0257c23ff7be07e240648bfff0bdefa9b0b6/client/karma.js#L70)

Make sure you set config client.captureConsole: true and use the latest Karma.

Upvotes: 12

Related Questions