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
This commit is contained in:
Richard van der Hoff
2025-07-25 11:13:52 +01:00
committed by GitHub
parent fc04ad26ce
commit d384a9b71b
3 changed files with 32 additions and 31 deletions

View File

@@ -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.

View File

@@ -27,6 +27,7 @@ describe("BugReportDialog", () => {
return render(<BugReportDialog onFinished={onFinished} />);
}
let prevLogger: ConsoleLogger;
beforeEach(() => {
jest.resetAllMocks();
SdkConfig.put({
@@ -48,24 +49,14 @@ describe("BugReportDialog", () => {
consume: jest.fn(),
warn: jest.fn(),
} as unknown as Mocked<ConsoleLogger>;
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();

View File

@@ -524,25 +524,16 @@ describe("Rageshakes", () => {
consume: jest.fn(),
warn: jest.fn(),
} as unknown as Mocked<ConsoleLogger>;
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", () => {