From 44586b33886c2c2d72b52b6e743d39d4ff9f9ce6 Mon Sep 17 00:00:00 2001 From: Frostebite Date: Sat, 2 Aug 2025 19:38:47 +0100 Subject: [PATCH] fix: mock github checks in tests --- src/jest.setup.ts | 19 +++++++++++++++++++ .../tests/cloud-runner-github-checks.test.ts | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/jest.setup.ts b/src/jest.setup.ts index 40325e3b..1263e5bc 100644 --- a/src/jest.setup.ts +++ b/src/jest.setup.ts @@ -1,5 +1,24 @@ import failOnConsole from 'jest-fail-on-console'; +// Polyfill fetch for the Node.js test environment. +// Jest runs tests inside a VM context where the global `fetch` is not +// automatically provided, even on Node 18+. Octokit requires a `fetch` +// implementation, so we provide one using undici's implementation. +// This ensures tests that interact with Octokit do not throw when +// constructing the client. +import { fetch, Headers, Request, Response } from 'undici'; + +if (!global.fetch) { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (global as any).fetch = fetch; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (global as any).Headers = Headers; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (global as any).Request = Request; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (global as any).Response = Response; +} + // Fail when console logs something inside a test - use spyOn instead failOnConsole({ shouldFailOnWarn: true, diff --git a/src/model/cloud-runner/tests/cloud-runner-github-checks.test.ts b/src/model/cloud-runner/tests/cloud-runner-github-checks.test.ts index adb5b4b6..440eec20 100644 --- a/src/model/cloud-runner/tests/cloud-runner-github-checks.test.ts +++ b/src/model/cloud-runner/tests/cloud-runner-github-checks.test.ts @@ -17,6 +17,25 @@ describe('Cloud Runner Github Checks', () => { it('Responds', () => {}); if (CloudRunnerOptions.cloudRunnerDebug) { + beforeEach(() => { + // Mock GitHub API requests to avoid real network calls + jest.spyOn(GitHub as any, 'createGitHubCheckRequest').mockResolvedValue({ + status: 201, + data: { id: '1' }, + }); + jest.spyOn(GitHub as any, 'updateGitHubCheckRequest').mockResolvedValue({ + status: 200, + data: {}, + }); + jest + .spyOn(GitHub as any, 'runUpdateAsyncChecksWorkflow') + .mockResolvedValue(undefined); + }); + + afterEach(() => { + jest.restoreAllMocks(); + }); + it( 'Check Handling Direct', async () => {