より良い方法があれば、私は不思議無効コンソールのエラー 内部の特定の冗談テスト(すなわち、本来のコンソールを復元し、各試験後/前)。
これが私の現在のアプローチです:
describe("Some description", () => {
  let consoleSpy;
  beforeEach(() => {
    if (typeof consoleSpy === "function") {
      consoleSpy.mockRestore();
    }
  });
  test("Some test that should not output errors to jest console", () => {
    expect.assertions(2);
    consoleSpy = jest.spyOn(console, "error").mockImplementation();
    // some function that uses console error
    expect(someFunction).toBe("X");
    expect(consoleSpy).toHaveBeenCalled();
  });
  test("Test that has console available", () => {
    // shows up during jest watch test, just as intended
    console.error("test");
  });
});
同じことを達成するためのよりクリーンな方法はありますか?私は避けたいspyOnが、mockRestoreそれだけで動作するようです。
ありがとう!
setupTestFrameworkScriptFileを優先して非推奨になりましたsetupFilesAfterEnv。