From cded9f01bcaceb51e699ffde25498fe69d1dd534 Mon Sep 17 00:00:00 2001 From: Ryan Ling Date: Thu, 14 Apr 2022 11:25:27 +1000 Subject: [PATCH] Log last check run on error (#841) This should help to troubleshoot validation failures. Suggested by @samchungy in #832. Co-authored-by: Sam Chung --- src/cli/test/reporters/github/index.test.ts | 23 ++++++++++++++- src/cli/test/reporters/github/index.ts | 32 ++++++++++++++------- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/cli/test/reporters/github/index.test.ts b/src/cli/test/reporters/github/index.test.ts index 747939b4b..db5a12656 100644 --- a/src/cli/test/reporters/github/index.test.ts +++ b/src/cli/test/reporters/github/index.test.ts @@ -378,5 +378,26 @@ it('should log a warning when it fails to create annotations', async () => { await reporter.onRunComplete(context, failResults); - expect(log.warn).toBeCalled(); + const logs = [ + null, + jest.mocked(log.warn).mock.calls, + jest + .mocked(log.subtle) + .mock.calls[0]?.join('\n') + .replace(/(at Object\.\)[\s\S]+$/, '$1...'), + jest.mocked(log.subtle).mock.calls.slice(1), + null, + ] + .flat() + .join('\n'); + + expect(logs).toMatchInlineSnapshot(` + " + Failed to report test results to GitHub. + Error: Badness! + at Object.... + Last request: + {\\"name\\":\\"skuba/test\\",\\"annotations\\":[{\\"annotation_level\\":\\"failure\\",\\"path\\":\\"src/test.test.ts\\",\\"start_line\\":2,\\"end_line\\":2,\\"start_column\\":15,\\"end_column\\":15,\\"message\\":\\"Error: expect(received).toBe(expected) // Object.is equality\\\\n\\\\nExpected: \\\\\\"a\\\\\\"\\\\nReceived: \\\\\\"b\\\\\\"\\\\n at Object. (/workdir/skuba/src/test.test.ts:2:15)\\\\n at Promise.then.completed (/workdir/skuba/node_modules/jest-circus/build/utils.js:390:28)\\\\n at new Promise ()\\\\n at callAsyncCircusFn (/workdir/skuba/node_modules/jest-circus/build/utils.js:315:10)\\\\n at _callCircusTest (/workdir/skuba/node_modules/jest-circus/build/run.js:218:40)\\\\n at processTicksAndRejections (node:internal/process/task_queues:96:5)\\\\n at _runTest (/workdir/skuba/node_modules/jest-circus/build/run.js:155:3)\\\\n at _runTestsForDescribeBlock (/workdir/skuba/node_modules/jest-circus/build/run.js:66:9)\\\\n at run (/workdir/skuba/node_modules/jest-circus/build/run.js:25:3)\\\\n at runAndTransformResultsToJestFormat (/workdir/skuba/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:167:21)\\",\\"title\\":\\"Jest\\"}],\\"conclusion\\":\\"failure\\",\\"summary\\":\\"\`skuba test\` found issues that require triage.\\",\\"title\\":\\"Test #123 failed\\"} + " + `); }); diff --git a/src/cli/test/reporters/github/index.ts b/src/cli/test/reporters/github/index.ts index 1ce20cc0d..5023ed642 100644 --- a/src/cli/test/reporters/github/index.ts +++ b/src/cli/test/reporters/github/index.ts @@ -22,6 +22,10 @@ export default class GitHubReporter implements Pick { return; } + type CheckRun = Parameters[0]; + + let lastCheckRun: CheckRun | undefined; + try { const entries = generateAnnotationEntries(testResults); @@ -38,20 +42,28 @@ export default class GitHubReporter implements Pick { ? '`skuba test` passed.' : '`skuba test` found issues that require triage.'; - await throwOnTimeout( - GitHub.createCheckRun({ - name, - annotations, - conclusion: isOk ? 'success' : 'failure', - summary, - title: `${build} ${isOk ? 'passed' : 'failed'}`, - }), - { s: 30 }, - ); + const checkRun: CheckRun = { + name, + annotations, + conclusion: isOk ? 'success' : 'failure', + summary, + title: `${build} ${isOk ? 'passed' : 'failed'}`, + }; + + lastCheckRun = checkRun; + + await throwOnTimeout(GitHub.createCheckRun(checkRun), { + s: 30, + }); } } catch (err) { log.warn('Failed to report test results to GitHub.'); log.subtle(inspect(err)); + + if (lastCheckRun) { + log.subtle('Last request:'); + log.subtle(JSON.stringify(lastCheckRun)); + } } } }