diff options
Diffstat (limited to '__test__/input-helper.test.ts')
| -rw-r--r-- | __test__/input-helper.test.ts | 119 |
1 files changed, 72 insertions, 47 deletions
diff --git a/__test__/input-helper.test.ts b/__test__/input-helper.test.ts index 25b6d18..b2f9074 100644 --- a/__test__/input-helper.test.ts +++ b/__test__/input-helper.test.ts @@ -1,10 +1,13 @@ -import * as core from '@actions/core' -import * as fsHelper from '../lib/fs-helper' -import * as github from '@actions/github' -import * as inputHelper from '../lib/input-helper' +import { + jest, + describe, + it, + expect, + beforeAll, + beforeEach, + afterAll +} from '@jest/globals' import * as path from 'path' -import * as workflowContextHelper from '../lib/workflow-context-helper' -import {IGitSourceSettings} from '../lib/git-source-settings' const originalGitHubWorkspace = process.env['GITHUB_WORKSPACE'] const gitHubWorkspace = path.resolve('/checkout-tests/workspace') @@ -12,42 +15,58 @@ const gitHubWorkspace = path.resolve('/checkout-tests/workspace') // Inputs for mock @actions/core let inputs = {} as any -// Shallow clone original @actions/github context -let originalContext = {...github.context} +// Mutable mock github context +const mockGithubContext: any = { + ref: 'refs/heads/some-ref', + sha: '1234567890123456789012345678901234567890', + repo: {owner: 'some-owner', repo: 'some-repo'}, + eventName: '', + payload: {} +} + +// Mock @actions/core before loading input-helper +jest.unstable_mockModule('@actions/core', () => ({ + getInput: jest.fn((name: string) => inputs[name]), + getBooleanInput: jest.fn((name: string) => inputs[name]), + getMultilineInput: jest.fn((name: string) => + inputs[name] ? String(inputs[name]).split('\n').filter(Boolean) : [] + ), + error: jest.fn(), + warning: jest.fn(), + info: jest.fn(), + debug: jest.fn(), + setFailed: jest.fn(), + setOutput: jest.fn(), + setSecret: jest.fn() +})) + +// Mock @actions/github before loading input-helper +jest.unstable_mockModule('@actions/github', () => ({ + context: mockGithubContext, + getOctokit: jest.fn() +})) + +// Mock fs-helper +const mockDirectoryExistsSync = jest.fn((p: string) => p === gitHubWorkspace) +jest.unstable_mockModule('../src/fs-helper.js', () => ({ + directoryExistsSync: mockDirectoryExistsSync, + fileExistsSync: jest.fn() +})) + +// Mock workflow-context-helper +const mockGetOrganizationId = jest.fn(async () => 123456) +jest.unstable_mockModule('../src/workflow-context-helper.js', () => ({ + getOrganizationId: mockGetOrganizationId +})) + +// Dynamic imports after mocking +const core = await import('@actions/core') +const inputHelper = await import('../src/input-helper.js') +type IGitSourceSettings = + import('../src/git-source-settings.js').IGitSourceSettings describe('input-helper tests', () => { beforeAll(() => { - // Mock getInput - jest.spyOn(core, 'getInput').mockImplementation((name: string) => { - return inputs[name] - }) - - // Mock error/warning/info/debug - jest.spyOn(core, 'error').mockImplementation(jest.fn()) - jest.spyOn(core, 'warning').mockImplementation(jest.fn()) - jest.spyOn(core, 'info').mockImplementation(jest.fn()) - jest.spyOn(core, 'debug').mockImplementation(jest.fn()) - - // Mock github context - jest.spyOn(github.context, 'repo', 'get').mockImplementation(() => { - return { - owner: 'some-owner', - repo: 'some-repo' - } - }) - github.context.ref = 'refs/heads/some-ref' - github.context.sha = '1234567890123456789012345678901234567890' - - // Mock ./fs-helper directoryExistsSync() - jest - .spyOn(fsHelper, 'directoryExistsSync') - .mockImplementation((path: string) => path == gitHubWorkspace) - - // Mock ./workflowContextHelper getOrganizationId() - jest - .spyOn(workflowContextHelper, 'getOrganizationId') - .mockImplementation(() => Promise.resolve(123456)) - // GitHub workspace process.env['GITHUB_WORKSPACE'] = gitHubWorkspace }) @@ -55,6 +74,15 @@ describe('input-helper tests', () => { beforeEach(() => { // Reset inputs inputs = {} + jest.clearAllMocks() + // Re-apply default mocks + ;(core.getInput as jest.Mock<any>).mockImplementation( + (name: string) => inputs[name] + ) + mockDirectoryExistsSync.mockImplementation( + (p: string) => p === gitHubWorkspace + ) + mockGetOrganizationId.mockResolvedValue(123456) }) afterAll(() => { @@ -65,11 +93,8 @@ describe('input-helper tests', () => { } // Restore @actions/github context - github.context.ref = originalContext.ref - github.context.sha = originalContext.sha - - // Restore - jest.restoreAllMocks() + mockGithubContext.ref = 'refs/heads/some-ref' + mockGithubContext.sha = '1234567890123456789012345678901234567890' }) it('sets defaults', async () => { @@ -95,15 +120,15 @@ describe('input-helper tests', () => { }) it('qualifies ref', async () => { - let originalRef = github.context.ref + let originalRef = mockGithubContext.ref try { - github.context.ref = 'some-unqualified-ref' + mockGithubContext.ref = 'some-unqualified-ref' const settings: IGitSourceSettings = await inputHelper.getInputs() expect(settings).toBeTruthy() expect(settings.commit).toBe('1234567890123456789012345678901234567890') expect(settings.ref).toBe('refs/heads/some-unqualified-ref') } finally { - github.context.ref = originalRef + mockGithubContext.ref = originalRef } }) |
