使用 Angular Karma Jasmine 时如何过滤 console.log

How to filter console.log when using Angular Karma Jasmine

我正在寻找一种方法来过滤掉在 运行ning 测试时将哪些控制台日志打印到控制台。控制台日志对项目非常有用,但是当我们 运行 测试时,它们没有提供太多帮助并使输出膨胀。

我已经在我们的 test.ts 文件中尝试 运行ning 这个(它在 out angular 配置文件中用于编译测试),但是它不起作用。

//...
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [
              "src/styles.less"
            ],
            "scripts": [],
            "assets": [
              "src/assets",
              "src/.well-known"
            ]
          }
        },
//...
// This file is required by karma.conf.js and loads recursively all the .spec and framework files

import "zone.js/dist/zone-testing";
import { getTestBed } from "@angular/core/testing";
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from "@angular/platform-browser-dynamic/testing";

//...

//bug: this errors with Error: Spies must be created in a before function or a spec
spyOn(window.console, "log").and.callFake(() => {});

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
// Then we find all the tests.
const context = require.context("./", true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);

我知道我可以做一个间谍并将其导入每个测试文件,但是我们有很多,这不是一个真正可扩展的方法。

有没有办法让我在每次测试时都看到这个?开个玩笑,这真的很容易通过使用他们的 setupFiles 功能来实现。

我可以通过将 test.ts 更新为:

来解决
// This file is required by karma.conf.js and loads recursively all the .spec and framework files

import "zone.js/dist/zone-testing";
import { getTestBed } from "@angular/core/testing";
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from "@angular/platform-browser-dynamic/testing";

//...

//bug: this errors with Error: Spies must be created in a before function or a spec
spyOn(window.console, "log").and.callFake(() => {});

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
// Then we find all the tests.
const context = require.context("./", true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);

beforeEach(() => {
  const ifTextMatchesThenIgnore = (text: string) => {
    return (
      text.toLowerCase().includes("configuring happyland") ||
      text.match(/^\[.*\]+/) ||
      text.toLowerCase().includes("stripe")
    );
  };
  spyOnConsole("log", ifTextMatchesThenIgnore);
});
const spyOnConsole = (property: "log" | "warn" | "info" | "error", ifTextMatchesThenIgnore: Function) => {
  const print = console[property];
  spyOn(console, property).and.callFake(function (...args) {
    let filteredArgs = [];
    for (let i = 0; i < args.length; i++) {
      const text = args?.[i];
      try {
        if (ifTextMatchesThenIgnore(text)) return;
      } catch {}
      filteredArgs.push(text);
    }
    if (filteredArgs.length > 0) {
      print(...filteredArgs);
    }
  });
};