github-pages-deploy-action/__tests__/main.test.ts

67 lines
1.5 KiB
TypeScript
Raw Normal View History

2020-01-20 05:16:28 +08:00
// Initial env variable setup for tests.
2020-03-05 21:19:45 +08:00
process.env['INPUT_FOLDER'] = 'build'
process.env['GITHUB_SHA'] = '123'
process.env['INPUT_DEBUG'] = 'debug'
2020-01-20 05:16:28 +08:00
2020-03-05 21:19:45 +08:00
import '../src/main'
import {action} from '../src/constants'
import run from '../src/lib'
import {execute} from '../src/execute'
import {setFailed} from '@actions/core'
2020-01-20 05:16:28 +08:00
2020-03-05 21:19:45 +08:00
const originalAction = JSON.stringify(action)
2020-01-20 05:16:28 +08:00
2020-03-05 21:19:45 +08:00
jest.mock('../src/execute', () => ({
2020-01-20 05:16:28 +08:00
execute: jest.fn()
2020-03-05 21:19:45 +08:00
}))
2020-01-20 05:16:28 +08:00
2020-03-05 21:19:45 +08:00
jest.mock('@actions/core', () => ({
2020-01-20 05:16:28 +08:00
setFailed: jest.fn(),
getInput: jest.fn(),
2020-03-28 22:35:26 +08:00
exportVariable: jest.fn(),
isDebug: jest.fn(),
info: jest.fn()
2020-03-05 21:19:45 +08:00
}))
2020-01-20 05:16:28 +08:00
2020-03-05 21:19:45 +08:00
describe('main', () => {
2020-01-20 05:16:28 +08:00
afterEach(() => {
2020-03-05 21:19:45 +08:00
Object.assign(action, JSON.parse(originalAction))
})
2020-01-20 05:16:28 +08:00
2020-03-05 21:19:45 +08:00
it('should run through the commands', async () => {
2020-01-20 05:16:28 +08:00
Object.assign(action, {
2020-03-05 21:19:45 +08:00
repositoryPath: 'JamesIves/github-pages-deploy-action',
folder: 'build',
branch: 'branch',
gitHubToken: '123',
2020-01-20 05:16:28 +08:00
pusher: {
2020-03-05 21:19:45 +08:00
name: 'asd',
email: 'as@cat'
2020-01-20 05:16:28 +08:00
},
isTest: false,
debug: true
2020-03-05 21:19:45 +08:00
})
await run(action)
expect(execute).toBeCalledTimes(19)
})
2020-01-20 05:16:28 +08:00
2020-03-05 21:19:45 +08:00
it('should throw if an error is encountered', async () => {
2020-01-20 05:16:28 +08:00
Object.assign(action, {
2020-03-05 21:19:45 +08:00
folder: 'build',
branch: 'branch',
baseBranch: 'master',
2020-01-20 05:16:28 +08:00
gitHubToken: null,
2020-03-29 01:22:39 +08:00
ssh: false,
2020-01-20 05:16:28 +08:00
accessToken: null,
pusher: {
2020-03-05 21:19:45 +08:00
name: 'asd',
email: 'as@cat'
2020-01-20 05:16:28 +08:00
},
isTest: true
2020-03-05 21:19:45 +08:00
})
await run(action)
expect(execute).toBeCalledTimes(0)
expect(setFailed).toBeCalledTimes(1)
})
})