Skip to content

Commit c6c8236

Browse files
authored
fix: mock github checks in tests (#724)
* fix: load fetch polyfill before tests * refactor: extract cloud runner test helpers * fix: load fetch polyfill before tests
1 parent 9e91ca9 commit c6c8236

File tree

6 files changed

+124
-51
lines changed

6 files changed

+124
-51
lines changed

.github/workflows/cloud-runner-ci-pipeline.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ name: Cloud Runner CI Pipeline
33
on:
44
push: { branches: [cloud-runner-develop, cloud-runner-preview, main] }
55
workflow_dispatch:
6+
inputs:
7+
runGithubIntegrationTests:
8+
description: 'Run GitHub Checks integration tests'
9+
required: false
10+
default: 'false'
611

712
permissions:
813
checks: write
@@ -207,3 +212,20 @@ jobs:
207212
name: ${{ matrix.providerStrategy }} Build (${{ matrix.targetPlatform }})
208213
path: ${{ steps.unity-build.outputs.BUILD_ARTIFACT }}
209214
retention-days: 14
215+
216+
githubChecksIntegration:
217+
name: GitHub Checks Integration
218+
runs-on: ubuntu-latest
219+
if: github.event_name == 'workflow_dispatch' && github.event.inputs.runGithubIntegrationTests == 'true'
220+
env:
221+
RUN_GITHUB_INTEGRATION_TESTS: true
222+
steps:
223+
- uses: actions/checkout@v4
224+
- uses: actions/setup-node@v4
225+
with:
226+
node-version: 20
227+
cache: 'yarn'
228+
- run: yarn install --frozen-lockfile
229+
- run: yarn test cloud-runner-github-checks-integration-test --detectOpenHandles --forceExit --runInBand
230+
env:
231+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jest.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ module.exports = {
2525
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
2626
modulePathIgnorePatterns: ['<rootDir>/lib/', '<rootDir>/dist/'],
2727

28-
// A list of paths to modules that run some code to configure or set up the testing framework before each test
28+
// Files that will be run before Jest is loaded to set globals like fetch
29+
setupFiles: ['<rootDir>/src/jest.globals.ts'],
30+
// A list of paths to modules that run some code to configure or set up the testing framework after the environment is ready
2931
setupFilesAfterEnv: ['<rootDir>/src/jest.setup.ts'],
3032
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Integration test for exercising real GitHub check creation and updates.
2+
import CloudRunner from '../model/cloud-runner/cloud-runner';
3+
import UnityVersioning from '../model/unity-versioning';
4+
import GitHub from '../model/github';
5+
import { TIMEOUT_INFINITE, createParameters } from '../test-utils/cloud-runner-test-helpers';
6+
7+
const runIntegration = process.env.RUN_GITHUB_INTEGRATION_TESTS === 'true';
8+
const describeOrSkip = runIntegration ? describe : describe.skip;
9+
10+
describeOrSkip('Cloud Runner Github Checks Integration', () => {
11+
it(
12+
'creates and updates a real GitHub check',
13+
async () => {
14+
const buildParameter = await createParameters({
15+
versioning: 'None',
16+
projectPath: 'test-project',
17+
unityVersion: UnityVersioning.read('test-project'),
18+
asyncCloudRunner: `true`,
19+
githubChecks: `true`,
20+
});
21+
await CloudRunner.setup(buildParameter);
22+
const checkId = await GitHub.createGitHubCheck(`integration create`);
23+
expect(checkId).not.toEqual('');
24+
await GitHub.updateGitHubCheck(`1 ${new Date().toISOString()}`, `integration`);
25+
await GitHub.updateGitHubCheck(`2 ${new Date().toISOString()}`, `integration`, `success`, `completed`);
26+
},
27+
TIMEOUT_INFINITE,
28+
);
29+
});

src/jest.globals.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { fetch as undiciFetch, Headers, Request, Response } from 'undici';
2+
3+
Object.assign(globalThis, { fetch: undiciFetch, Headers, Request, Response });
Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,65 @@
1-
import { BuildParameters } from '../..';
21
import CloudRunner from '../cloud-runner';
32
import UnityVersioning from '../../unity-versioning';
4-
import { Cli } from '../../cli/cli';
5-
import CloudRunnerOptions from '../options/cloud-runner-options';
63
import setups from './cloud-runner-suite.test';
7-
import { OptionValues } from 'commander';
84
import GitHub from '../../github';
9-
export const TIMEOUT_INFINITE = 1e9;
10-
async function CreateParameters(overrides: OptionValues | undefined) {
11-
if (overrides) Cli.options = overrides;
12-
13-
return BuildParameters.create();
14-
}
5+
import { TIMEOUT_INFINITE, createParameters } from '../../../test-utils/cloud-runner-test-helpers';
156
describe('Cloud Runner Github Checks', () => {
167
setups();
178
it('Responds', () => {});
189

19-
if (CloudRunnerOptions.cloudRunnerDebug) {
20-
it(
21-
'Check Handling Direct',
22-
async () => {
23-
// Setup parameters
24-
const buildParameter = await CreateParameters({
25-
versioning: 'None',
26-
projectPath: 'test-project',
27-
unityVersion: UnityVersioning.read('test-project'),
28-
asyncCloudRunner: `true`,
29-
githubChecks: `true`,
30-
});
31-
await CloudRunner.setup(buildParameter);
32-
CloudRunner.buildParameters.githubCheckId = await GitHub.createGitHubCheck(`direct create`);
33-
await GitHub.updateGitHubCheck(`1 ${new Date().toISOString()}`, `direct`);
34-
await GitHub.updateGitHubCheck(`2 ${new Date().toISOString()}`, `direct`, `success`, `completed`);
35-
},
36-
TIMEOUT_INFINITE,
37-
);
38-
it(
39-
'Check Handling Via Async Workflow',
40-
async () => {
41-
// Setup parameters
42-
const buildParameter = await CreateParameters({
43-
versioning: 'None',
44-
projectPath: 'test-project',
45-
unityVersion: UnityVersioning.read('test-project'),
46-
asyncCloudRunner: `true`,
47-
githubChecks: `true`,
48-
});
49-
GitHub.forceAsyncTest = true;
50-
await CloudRunner.setup(buildParameter);
51-
CloudRunner.buildParameters.githubCheckId = await GitHub.createGitHubCheck(`async create`);
52-
await GitHub.updateGitHubCheck(`1 ${new Date().toISOString()}`, `async`);
53-
await GitHub.updateGitHubCheck(`2 ${new Date().toISOString()}`, `async`, `success`, `completed`);
54-
GitHub.forceAsyncTest = false;
55-
},
56-
TIMEOUT_INFINITE,
57-
);
58-
}
10+
beforeEach(() => {
11+
// Mock GitHub API requests to avoid real network calls
12+
jest.spyOn(GitHub as any, 'createGitHubCheckRequest').mockResolvedValue({
13+
status: 201,
14+
data: { id: '1' },
15+
});
16+
jest.spyOn(GitHub as any, 'updateGitHubCheckRequest').mockResolvedValue({
17+
status: 200,
18+
data: {},
19+
});
20+
jest.spyOn(GitHub as any, 'runUpdateAsyncChecksWorkflow').mockResolvedValue(undefined);
21+
});
22+
23+
afterEach(() => {
24+
jest.restoreAllMocks();
25+
});
26+
27+
it(
28+
'Check Handling Direct',
29+
async () => {
30+
// Setup parameters
31+
const buildParameter = await createParameters({
32+
versioning: 'None',
33+
projectPath: 'test-project',
34+
unityVersion: UnityVersioning.read('test-project'),
35+
asyncCloudRunner: `true`,
36+
githubChecks: `true`,
37+
});
38+
await CloudRunner.setup(buildParameter);
39+
CloudRunner.buildParameters.githubCheckId = await GitHub.createGitHubCheck(`direct create`);
40+
await GitHub.updateGitHubCheck(`1 ${new Date().toISOString()}`, `direct`);
41+
await GitHub.updateGitHubCheck(`2 ${new Date().toISOString()}`, `direct`, `success`, `completed`);
42+
},
43+
TIMEOUT_INFINITE,
44+
);
45+
it(
46+
'Check Handling Via Async Workflow',
47+
async () => {
48+
// Setup parameters
49+
const buildParameter = await createParameters({
50+
versioning: 'None',
51+
projectPath: 'test-project',
52+
unityVersion: UnityVersioning.read('test-project'),
53+
asyncCloudRunner: `true`,
54+
githubChecks: `true`,
55+
});
56+
GitHub.forceAsyncTest = true;
57+
await CloudRunner.setup(buildParameter);
58+
CloudRunner.buildParameters.githubCheckId = await GitHub.createGitHubCheck(`async create`);
59+
await GitHub.updateGitHubCheck(`1 ${new Date().toISOString()}`, `async`);
60+
await GitHub.updateGitHubCheck(`2 ${new Date().toISOString()}`, `async`, `success`, `completed`);
61+
GitHub.forceAsyncTest = false;
62+
},
63+
TIMEOUT_INFINITE,
64+
);
5965
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { BuildParameters } from '../model';
2+
import { Cli } from '../model/cli/cli';
3+
import { OptionValues } from 'commander';
4+
5+
export const TIMEOUT_INFINITE = 1e9;
6+
7+
export async function createParameters(overrides?: OptionValues) {
8+
if (overrides) Cli.options = overrides;
9+
10+
return BuildParameters.create();
11+
}

0 commit comments

Comments
 (0)