Skip to content

Commit 8480620

Browse files
committed
test: use github mock instead of fixture for context
Signed-off-by: CrazyMax <[email protected]>
1 parent 8e71445 commit 8480620

2 files changed

Lines changed: 28 additions & 38 deletions

File tree

__tests__/context.test.ts

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,12 @@
1-
import {afterEach, beforeEach, describe, expect, test, it, vi} from 'vitest';
2-
import * as dotenv from 'dotenv';
3-
import * as fs from 'fs';
4-
import * as path from 'path';
1+
import {beforeEach, describe, expect, test, it, vi} from 'vitest';
52
import {Context} from '@actions/github/lib/context.js';
63
import {Git} from '@docker/actions-toolkit/lib/git.js';
7-
import {GitHub} from '@docker/actions-toolkit/lib/github.js';
84
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit.js';
95

106
import * as context from '../src/context.js';
117

128
const toolkit = new Toolkit({githubToken: 'fake-github-token'});
139

14-
beforeEach(() => {
15-
vi.clearAllMocks();
16-
vi.spyOn(GitHub, 'context', 'get').mockImplementation((): Context => {
17-
const ctx = new Context();
18-
const payload = ctx.payload as {
19-
commits?: Array<{id: string; timestamp: string}>;
20-
head_commit?: {id: string; timestamp: string};
21-
};
22-
if (ctx.sha && !payload.commits?.length && !payload.head_commit) {
23-
payload.head_commit = {
24-
id: ctx.sha,
25-
timestamp: '2024-11-13T13:42:28.000Z'
26-
};
27-
}
28-
return ctx;
29-
});
30-
});
31-
3210
describe('getInputs', () => {
3311
beforeEach(() => {
3412
process.env = Object.keys(process.env).reduce((object, key) => {
@@ -113,24 +91,12 @@ describe('getInputs', () => {
11391
});
11492

11593
describe('getContext', () => {
116-
const originalEnv = process.env;
117-
beforeEach(() => {
118-
process.env = {
119-
...originalEnv,
120-
...dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures/event_create_branch.env')))
121-
};
122-
});
123-
afterEach(() => {
124-
process.env = originalEnv;
125-
});
126-
12794
it('workflow', async () => {
12895
const ctx = await context.getContext(context.ContextSource.workflow, toolkit);
12996
expect(ctx.ref).toEqual('refs/heads/dev');
13097
expect(ctx.sha).toEqual('5f3331d7f7044c18ca9f12c77d961c4d7cf3276a');
13198
expect(ctx.commitDate).toEqual(new Date('2024-11-13T13:42:28.000Z'));
13299
});
133-
134100
it('git', async () => {
135101
vi.spyOn(Git, 'context').mockImplementation((): Promise<Context> => {
136102
return Promise.resolve({

__tests__/setup.unit.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'node:fs';
2+
import {createRequire} from 'node:module';
23
import os from 'node:os';
34
import path from 'node:path';
45
import {vi} from 'vitest';
@@ -12,13 +13,17 @@ process.env = Object.assign({}, process.env, {
1213
RUNNER_TOOL_CACHE: path.join(tmpDir, 'runner-tool-cache')
1314
});
1415

15-
vi.mock('@actions/github', () => ({
16+
const require = createRequire(import.meta.url);
17+
type RequireCacheEntry = NonNullable<(typeof require.cache)[string]>;
18+
19+
const githubMock = {
1620
context: {
1721
repo: {
1822
owner: 'docker',
1923
repo: 'actions-toolkit'
2024
},
21-
ref: 'refs/heads/master',
25+
ref: 'refs/heads/dev',
26+
sha: '5f3331d7f7044c18ca9f12c77d961c4d7cf3276a',
2227
runId: 2188748038,
2328
runNumber: 15,
2429
payload: {
@@ -232,4 +237,23 @@ vi.mock('@actions/github', () => ({
232237
}
233238
}
234239
})
235-
}));
240+
};
241+
242+
vi.mock('@actions/github', () => githubMock);
243+
244+
for (const mod of ['@actions/github', '@docker/actions-toolkit/node_modules/@actions/github']) {
245+
try {
246+
const resolved = require.resolve(mod);
247+
vi.doMock(resolved, () => githubMock);
248+
require.cache[resolved] = {
249+
id: resolved,
250+
filename: resolved,
251+
loaded: true,
252+
exports: githubMock,
253+
children: [],
254+
paths: []
255+
} as RequireCacheEntry;
256+
} catch {
257+
// Ignore unresolved optional paths; vi.mock handles module-level mocking.
258+
}
259+
}

0 commit comments

Comments
 (0)