如何使用Karma单元测试运行程序捕获console.error

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

本文关键字:console error 程序 测试运行 何使用 Karma 单元      更新时间:2024-04-02

Karma将捕获任何写入console.log的消息,但不会捕获任何写入到console.error的消息。我知道因果报应在幕后使用log4js。

有什么方法可以配置因果报应来捕获console.error吗?

因果报应确实捕获了console.error(请参阅https://github.com/karma-runner/karma/blob/415d0257c23ff7be07e240648bfff0bdefa9b0b6/client/karma.js#L70)

确保您设置了配置client.captureConsole: true并使用最新的Karma。

您可以捕获本博客文章中描述的console.error

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!")
  });