diff options
| author | eric sciple <ericsciple@users.noreply.github.com> | 2020-02-13 13:25:46 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-13 13:25:46 -0500 |
| commit | aa53c9e2635a2ba1a74f58631719ad73c5ded361 (patch) | |
| tree | 7608dddd2273528080f9bcc135d064cc001548de /__test__/input-helper.test.ts | |
| parent | 68e277f20178a549b370dd1ba2f3d8200cbe252f (diff) | |
switch to spyOn for mocks (#152)
Diffstat (limited to '__test__/input-helper.test.ts')
| -rw-r--r-- | __test__/input-helper.test.ts | 76 |
1 files changed, 38 insertions, 38 deletions
diff --git a/__test__/input-helper.test.ts b/__test__/input-helper.test.ts index 778c6eb..be4b166 100644 --- a/__test__/input-helper.test.ts +++ b/__test__/input-helper.test.ts @@ -1,47 +1,44 @@ import * as assert from 'assert' +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 * as path from 'path' import {ISourceSettings} from '../lib/git-source-provider' const originalGitHubWorkspace = process.env['GITHUB_WORKSPACE'] const gitHubWorkspace = path.resolve('/checkout-tests/workspace') -// Late bind -let inputHelper: any - -// Mock @actions/core +// Inputs for mock @actions/core let inputs = {} as any -const mockCore = jest.genMockFromModule('@actions/core') as any -mockCore.getInput = (name: string) => { - return inputs[name] -} - -// Mock @actions/github -const mockGitHub = jest.genMockFromModule('@actions/github') as any -mockGitHub.context = { - repo: { - owner: 'some-owner', - repo: 'some-repo' - }, - ref: 'refs/heads/some-ref', - sha: '1234567890123456789012345678901234567890' -} - -// Mock ./fs-helper -const mockFSHelper = jest.genMockFromModule('../lib/fs-helper') as any -mockFSHelper.directoryExistsSync = (path: string) => path == gitHubWorkspace + +// Shallow clone original @actions/github context +let originalContext = {...github.context} describe('input-helper tests', () => { beforeAll(() => { + // Mock @actions/core getInput() + jest.spyOn(core, 'getInput').mockImplementation((name: string) => { + return inputs[name] + }) + + // Mock @actions/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) + // GitHub workspace process.env['GITHUB_WORKSPACE'] = gitHubWorkspace - - // Mocks - jest.setMock('@actions/core', mockCore) - jest.setMock('@actions/github', mockGitHub) - jest.setMock('../lib/fs-helper', mockFSHelper) - - // Now import - inputHelper = require('../lib/input-helper') }) beforeEach(() => { @@ -50,14 +47,18 @@ describe('input-helper tests', () => { }) afterAll(() => { - // Reset GitHub workspace + // Restore GitHub workspace delete process.env['GITHUB_WORKSPACE'] if (originalGitHubWorkspace) { process.env['GITHUB_WORKSPACE'] = originalGitHubWorkspace } - // Reset modules - jest.resetModules() + // Restore @actions/github context + github.context.ref = originalContext.ref + github.context.sha = originalContext.sha + + // Restore + jest.restoreAllMocks() }) it('sets defaults', () => { @@ -76,16 +77,15 @@ describe('input-helper tests', () => { }) it('qualifies ref', () => { - let originalContext = mockGitHub.context + let originalRef = github.context.ref try { - mockGitHub.context = {...originalContext} // Shallow clone - mockGitHub.context.ref = 'some-unqualified-ref' + github.context.ref = 'some-unqualified-ref' const settings: ISourceSettings = inputHelper.getInputs() expect(settings).toBeTruthy() expect(settings.commit).toBe('1234567890123456789012345678901234567890') expect(settings.ref).toBe('refs/heads/some-unqualified-ref') } finally { - mockGitHub.context = originalContext + github.context.ref = originalRef } }) |
