Skip to content

Commit 5e11964

Browse files
authored
Merge pull request #1244 from dscho/only-show-console.log-output-in-case-of-test-failures
ci-helper tests: only show the `console.log()` output upon failure
2 parents ec00c95 + 2594998 commit 5e11964

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

tests/ci-helper.test.ts

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,26 @@ import { testCreateRepo, TestRepo } from "./test-lib";
99

1010
jest.setTimeout(180000);
1111

12+
// Only show the `console.log()` output when a test failed
13+
declare type AsyncFn = () => Promise<void>;
14+
function testQ(label: string, fn: AsyncFn) {
15+
test(label, async () => {
16+
const originalConsoleLog = console.log;
17+
const mockLog = jest.fn();
18+
try {
19+
console.log = mockLog;
20+
await fn();
21+
} catch (e) {
22+
mockLog.mock.calls.forEach(call => {
23+
originalConsoleLog(call[0]);
24+
});
25+
throw e;
26+
} finally {
27+
console.log = originalConsoleLog;
28+
}
29+
});
30+
}
31+
1232
getConfig();
1333

1434
const eMailOptions = {
@@ -162,7 +182,7 @@ async function checkMsgId(messageId: string): Promise<boolean> {
162182
return false;
163183
}
164184

165-
test("identify merge that integrated some commit", async () => {
185+
testQ("identify merge that integrated some commit", async () => {
166186
const repo = await testCreateRepo(__filename);
167187

168188
/*
@@ -193,7 +213,7 @@ test("identify merge that integrated some commit", async () => {
193213
expect(await ci.identifyMergeCommit("seen", commitH)).toEqual(commitD);
194214
});
195215

196-
test("identify upstream commit", async () => {
216+
testQ("identify upstream commit", async () => {
197217
// initialize test worktree and gitgitgadget remote
198218
const worktree = await testCreateRepo(__filename, "-worktree");
199219
const gggRemote = await testCreateRepo(__filename, "-gitgitgadget");
@@ -249,7 +269,7 @@ test("identify upstream commit", async () => {
249269
expect(bMetaNew?.commitInGitGit).toEqual(commitBNew);
250270
});
251271

252-
test("handle comment allow basic test", async () => {
272+
testQ("handle comment allow basic test", async () => {
253273
const { worktree, gggLocal } = await setupRepos("a1");
254274

255275
// Ready to start testing
@@ -277,7 +297,7 @@ test("handle comment allow basic test", async () => {
277297
.toMatch(/is now allowed to use GitGitGadget/);
278298
});
279299

280-
test("handle comment allow fail invalid user", async () => {
300+
testQ("handle comment allow fail invalid user", async () => {
281301
const { worktree, gggLocal } = await setupRepos("a2");
282302

283303
const ci = new TestCIHelper(gggLocal.workDir, false, worktree.workDir);
@@ -297,7 +317,7 @@ test("handle comment allow fail invalid user", async () => {
297317
.toMatch(/is not a valid GitHub username/);
298318
});
299319

300-
test("handle comment allow no public email", async () => {
320+
testQ("handle comment allow no public email", async () => {
301321
const { worktree, gggLocal } = await setupRepos("a3");
302322

303323
const ci = new TestCIHelper(gggLocal.workDir, false, worktree.workDir);
@@ -325,7 +345,7 @@ test("handle comment allow no public email", async () => {
325345
.toMatch(/no public email address set/);
326346
});
327347

328-
test("handle comment allow already allowed", async () => {
348+
testQ("handle comment allow already allowed", async () => {
329349
const { worktree, gggLocal } = await setupRepos("a4");
330350

331351
const ci = new TestCIHelper(gggLocal.workDir, false, worktree.workDir);
@@ -352,7 +372,7 @@ test("handle comment allow already allowed", async () => {
352372
.toMatch(/already allowed to use GitGitGadget/);
353373
});
354374

355-
test("handle comment allow no name specified (with trailing white space)",
375+
testQ("handle comment allow no name specified (with trailing white space)",
356376
async () => {
357377
const { worktree, gggLocal } = await setupRepos("a5");
358378

@@ -396,7 +416,7 @@ test("handle comment allow no name specified (with trailing white space)",
396416
.toMatch(/already allowed to use GitGitGadget/);
397417
});
398418

399-
test("handle comment disallow basic test", async () => {
419+
testQ("handle comment disallow basic test", async () => {
400420
const { worktree, gggLocal } = await setupRepos("d1");
401421

402422
const ci = new TestCIHelper(gggLocal.workDir, false, worktree.workDir);
@@ -423,7 +443,7 @@ test("handle comment disallow basic test", async () => {
423443
.toMatch(/is no longer allowed to use GitGitGadget/);
424444
});
425445

426-
test("handle comment disallow was not allowed", async () => {
446+
testQ("handle comment disallow was not allowed", async () => {
427447
const { worktree, gggLocal } = await setupRepos("d2");
428448

429449
const ci = new TestCIHelper(gggLocal.workDir, false, worktree.workDir);
@@ -443,7 +463,7 @@ test("handle comment disallow was not allowed", async () => {
443463
.toMatch(/already not allowed to use GitGitGadget/);
444464
});
445465

446-
test("handle comment submit not author", async () => {
466+
testQ("handle comment submit not author", async () => {
447467
const { worktree, gggLocal } = await setupRepos("s1");
448468

449469
const ci = new TestCIHelper(gggLocal.workDir, false, worktree.workDir);
@@ -487,7 +507,7 @@ test("handle comment submit not author", async () => {
487507
.toMatch(/Only the owner of a PR can submit/);
488508
});
489509

490-
test("handle comment submit not mergeable", async () => {
510+
testQ("handle comment submit not mergeable", async () => {
491511
const { worktree, gggLocal } = await setupRepos("s2");
492512

493513
const ci = new TestCIHelper(gggLocal.workDir, false, worktree.workDir);
@@ -531,7 +551,7 @@ test("handle comment submit not mergeable", async () => {
531551
.toMatch(/does not merge cleanly/);
532552
});
533553

534-
test("handle comment submit email success", async () => {
554+
testQ("handle comment submit email success", async () => {
535555
const { worktree, gggLocal, gggRemote } = await setupRepos("s3");
536556

537557
const ci = new TestCIHelper(gggLocal.workDir, false, worktree.workDir);
@@ -616,7 +636,7 @@ test("handle comment submit email success", async () => {
616636
}
617637
});
618638

619-
test("handle comment preview email success", async () => {
639+
testQ("handle comment preview email success", async () => {
620640
const { worktree, gggLocal, gggRemote } = await setupRepos("p1");
621641

622642
const ci = new TestCIHelper(gggLocal.workDir, false, worktree.workDir);
@@ -721,7 +741,7 @@ test("handle comment preview email success", async () => {
721741
}
722742
});
723743

724-
test("handle push/comment too many commits fails", async () => {
744+
testQ("handle push/comment too many commits fails", async () => {
725745
const { worktree, gggLocal, gggRemote } = await setupRepos("pu1");
726746

727747
const ci = new TestCIHelper(gggLocal.workDir, false, worktree.workDir);
@@ -841,7 +861,7 @@ test("handle push/comment too many commits fails", async () => {
841861
expect(ci.addPRLabelsCalls[0][1]).toEqual(["new user"]);
842862
});
843863

844-
test("handle push/comment merge commits fails", async () => {
864+
testQ("handle push/comment merge commits fails", async () => {
845865
const { worktree, gggLocal, gggRemote} = await setupRepos("pu2");
846866

847867
const ci = new TestCIHelper(gggLocal.workDir, false, worktree.workDir);
@@ -988,7 +1008,7 @@ test("handle push/comment merge commits fails", async () => {
9881008

9891009
});
9901010

991-
test("disallow no-reply emails", async () => {
1011+
testQ("disallow no-reply emails", async () => {
9921012
const { worktree, gggLocal, gggRemote} = await setupRepos("pu2");
9931013

9941014
const ci = new TestCIHelper(gggLocal.workDir, false, worktree.workDir);
@@ -1067,7 +1087,7 @@ test("disallow no-reply emails", async () => {
10671087

10681088
// Basic tests for ci-helper - lint tests are in commit-lint.tests.ts
10691089

1070-
test("basic lint tests", async () => {
1090+
testQ("basic lint tests", async () => {
10711091
const { worktree, gggLocal, gggRemote} = await setupRepos("pu4");
10721092

10731093
const ci = new TestCIHelper(gggLocal.workDir, false, worktree.workDir);
@@ -1197,7 +1217,7 @@ test("basic lint tests", async () => {
11971217

11981218
});
11991219

1200-
test("Handle comment cc", async () => {
1220+
testQ("Handle comment cc", async () => {
12011221
const {worktree, gggLocal} = await setupRepos("cc");
12021222

12031223
const ci = new TestCIHelper(gggLocal.workDir, false, worktree.workDir);

0 commit comments

Comments
 (0)