From d384a9b71b78f9081cecbe3659fac95d94761e04 Mon Sep 17 00:00:00 2001
From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com>
Date: Fri, 25 Jul 2025 11:13:52 +0100
Subject: [PATCH] Work around jest bug that swallows console output (#30405)
* Work around jest bug that swallows console output
Hacky workaround for https://github.com/jestjs/jest/issues/15747
* Fix unit test
* Only write logs if there are some to write
* Another test fix
---
test/setupTests.ts | 21 +++++++++++++++-
.../views/dialogs/BugReportDialog-test.tsx | 17 +++----------
test/unit-tests/submit-rageshake-test.ts | 25 ++++++-------------
3 files changed, 32 insertions(+), 31 deletions(-)
diff --git a/test/setupTests.ts b/test/setupTests.ts
index 144f27e15a..7e68edf19d 100644
--- a/test/setupTests.ts
+++ b/test/setupTests.ts
@@ -6,12 +6,14 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Com
Please see LICENSE files in the repository root for full details.
*/
+import { env } from "process";
import "@testing-library/jest-dom";
import "blob-polyfill";
import { secureRandomString } from "matrix-js-sdk/src/randomstring";
import { mocked } from "jest-mock";
-import { PredictableRandom } from "./test-utils/predictableRandom"; // https://github.com/jsdom/jsdom/issues/2555
+import { PredictableRandom } from "./test-utils/predictableRandom";
+import * as rageshake from "../src/rageshake/rageshake";
declare global {
// eslint-disable-next-line no-var
@@ -37,6 +39,23 @@ beforeEach(() => {
});
});
+// Somewhat hacky workaround for https://github.com/jestjs/jest/issues/15747: if the GHA reporter is enabled,
+// capture logs using the rageshake infrastructure, then dump them out after the test.
+if (env["GITHUB_ACTIONS"] !== undefined) {
+ beforeEach(async () => {
+ await rageshake.init(/* setUpPersistence = */ false);
+ });
+
+ afterEach(async () => {
+ const logs = global.mx_rage_logger.flush(/* keeplogs = */ false);
+ if (logs) {
+ process.stderr.write(`::group::Console logs from test '${expect.getState().currentTestName}'\n\n`);
+ process.stderr.write(logs);
+ process.stderr.write("::endgroup::\n");
+ }
+ });
+}
+
// Very carefully enable the mocks for everything else in
// a specific order. We use this order to ensure we properly
// establish an application state that actually works.
diff --git a/test/unit-tests/components/views/dialogs/BugReportDialog-test.tsx b/test/unit-tests/components/views/dialogs/BugReportDialog-test.tsx
index 2cd6d609ea..f48db6ed91 100644
--- a/test/unit-tests/components/views/dialogs/BugReportDialog-test.tsx
+++ b/test/unit-tests/components/views/dialogs/BugReportDialog-test.tsx
@@ -27,6 +27,7 @@ describe("BugReportDialog", () => {
return render();
}
+ let prevLogger: ConsoleLogger;
beforeEach(() => {
jest.resetAllMocks();
SdkConfig.put({
@@ -48,24 +49,14 @@ describe("BugReportDialog", () => {
consume: jest.fn(),
warn: jest.fn(),
} as unknown as Mocked;
+ mockConsoleLogger.flush.mockReturnValue("line 1\nline 2\n");
- // @ts-ignore - mock the console logger
+ prevLogger = global.mx_rage_logger;
global.mx_rage_logger = mockConsoleLogger;
-
- // @ts-ignore
- mockConsoleLogger.flush.mockReturnValue([
- {
- id: "instance-0",
- line: "line 1",
- },
- {
- id: "instance-1",
- line: "line 2",
- },
- ]);
});
afterEach(() => {
+ global.mx_rage_logger = prevLogger;
jest.restoreAllMocks();
SdkConfig.reset();
fetchMock.restore();
diff --git a/test/unit-tests/submit-rageshake-test.ts b/test/unit-tests/submit-rageshake-test.ts
index 832cee3fca..f31102e3ba 100644
--- a/test/unit-tests/submit-rageshake-test.ts
+++ b/test/unit-tests/submit-rageshake-test.ts
@@ -524,25 +524,16 @@ describe("Rageshakes", () => {
consume: jest.fn(),
warn: jest.fn(),
} as unknown as Mocked;
+ mockConsoleLogger.flush.mockReturnValue("line 1\nline 2\n");
- // @ts-ignore - mock the console logger
+ const prevLogger = global.mx_rage_logger;
global.mx_rage_logger = mockConsoleLogger;
-
- // @ts-ignore
- mockConsoleLogger.flush.mockReturnValue([
- {
- id: "instance-0",
- line: "line 1",
- },
- {
- id: "instance-1",
- line: "line 2",
- },
- ]);
-
- const formData = await collectBugReport({ sendLogs: true });
-
- expect(formData.get("compressed-log")).toBeDefined();
+ try {
+ const formData = await collectBugReport({ sendLogs: true });
+ expect(formData.get("compressed-log")).toBeDefined();
+ } finally {
+ global.mx_rage_logger = prevLogger;
+ }
});
describe("A-Element-R label", () => {