Skip to content

Commit 7b997ac

Browse files
richvdhDileep Bandla
authored andcommitted
Work around jest bug that swallows console output (element-hq#30405)
* Work around jest bug that swallows console output Hacky workaround for jestjs/jest#15747 * Fix unit test * Only write logs if there are some to write * Another test fix
1 parent 3213870 commit 7b997ac

3 files changed

Lines changed: 32 additions & 31 deletions

File tree

test/setupTests.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Com
66
Please see LICENSE files in the repository root for full details.
77
*/
88

9+
import { env } from "process";
910
import "@testing-library/jest-dom";
1011
import "blob-polyfill";
1112
import { secureRandomString } from "matrix-js-sdk/src/randomstring";
1213
import { mocked } from "jest-mock";
1314

14-
import { PredictableRandom } from "./test-utils/predictableRandom"; // https://github.com/jsdom/jsdom/issues/2555
15+
import { PredictableRandom } from "./test-utils/predictableRandom";
16+
import * as rageshake from "../src/rageshake/rageshake";
1517

1618
declare global {
1719
// eslint-disable-next-line no-var
@@ -37,6 +39,23 @@ beforeEach(() => {
3739
});
3840
});
3941

42+
// Somewhat hacky workaround for https://github.com/jestjs/jest/issues/15747: if the GHA reporter is enabled,
43+
// capture logs using the rageshake infrastructure, then dump them out after the test.
44+
if (env["GITHUB_ACTIONS"] !== undefined) {
45+
beforeEach(async () => {
46+
await rageshake.init(/* setUpPersistence = */ false);
47+
});
48+
49+
afterEach(async () => {
50+
const logs = global.mx_rage_logger.flush(/* keeplogs = */ false);
51+
if (logs) {
52+
process.stderr.write(`::group::Console logs from test '${expect.getState().currentTestName}'\n\n`);
53+
process.stderr.write(logs);
54+
process.stderr.write("::endgroup::\n");
55+
}
56+
});
57+
}
58+
4059
// Very carefully enable the mocks for everything else in
4160
// a specific order. We use this order to ensure we properly
4261
// establish an application state that actually works.

test/unit-tests/components/views/dialogs/BugReportDialog-test.tsx

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ describe("BugReportDialog", () => {
2727
return render(<BugReportDialog onFinished={onFinished} />);
2828
}
2929

30+
let prevLogger: ConsoleLogger;
3031
beforeEach(() => {
3132
jest.resetAllMocks();
3233
SdkConfig.put({
@@ -48,24 +49,14 @@ describe("BugReportDialog", () => {
4849
consume: jest.fn(),
4950
warn: jest.fn(),
5051
} as unknown as Mocked<ConsoleLogger>;
52+
mockConsoleLogger.flush.mockReturnValue("line 1\nline 2\n");
5153

52-
// @ts-ignore - mock the console logger
54+
prevLogger = global.mx_rage_logger;
5355
global.mx_rage_logger = mockConsoleLogger;
54-
55-
// @ts-ignore
56-
mockConsoleLogger.flush.mockReturnValue([
57-
{
58-
id: "instance-0",
59-
line: "line 1",
60-
},
61-
{
62-
id: "instance-1",
63-
line: "line 2",
64-
},
65-
]);
6656
});
6757

6858
afterEach(() => {
59+
global.mx_rage_logger = prevLogger;
6960
jest.restoreAllMocks();
7061
SdkConfig.reset();
7162
fetchMock.restore();

test/unit-tests/submit-rageshake-test.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -524,25 +524,16 @@ describe("Rageshakes", () => {
524524
consume: jest.fn(),
525525
warn: jest.fn(),
526526
} as unknown as Mocked<ConsoleLogger>;
527+
mockConsoleLogger.flush.mockReturnValue("line 1\nline 2\n");
527528

528-
// @ts-ignore - mock the console logger
529+
const prevLogger = global.mx_rage_logger;
529530
global.mx_rage_logger = mockConsoleLogger;
530-
531-
// @ts-ignore
532-
mockConsoleLogger.flush.mockReturnValue([
533-
{
534-
id: "instance-0",
535-
line: "line 1",
536-
},
537-
{
538-
id: "instance-1",
539-
line: "line 2",
540-
},
541-
]);
542-
543-
const formData = await collectBugReport({ sendLogs: true });
544-
545-
expect(formData.get("compressed-log")).toBeDefined();
531+
try {
532+
const formData = await collectBugReport({ sendLogs: true });
533+
expect(formData.get("compressed-log")).toBeDefined();
534+
} finally {
535+
global.mx_rage_logger = prevLogger;
536+
}
546537
});
547538

548539
describe("A-Element-R label", () => {

0 commit comments

Comments
 (0)