6
0
Fork 0
mirror of https://github.com/wagoid/commitlint-github-action.git synced 2025-12-28 13:38:18 +00:00

test: add output tests for PR scenario

This commit is contained in:
Wagner Santos 2020-08-01 11:27:40 -03:00
parent 231d02e17f
commit 0c7da1b0a2

View file

@ -247,15 +247,19 @@ describe('Commit Linter action', () => {
td.verify(core.setFailed(contains('wrong commit from another branch'))) td.verify(core.setFailed(contains('wrong commit from another branch')))
}) })
it('should lint all commits from a pull request', async () => { describe('when there are multiple commits failing in the pull request', () => {
let expectedResultsOutput
const firstMessage = 'wrong message 1'
const secondMessage = 'wrong message 2'
beforeEach(async () => {
cwd = await git.bootstrap('fixtures/conventional') cwd = await git.bootstrap('fixtures/conventional')
td.when(core.getInput('configFile')).thenReturn('./commitlint.config.js') td.when(core.getInput('configFile')).thenReturn('./commitlint.config.js')
await gitEmptyCommit(cwd, 'message from before push') await gitEmptyCommit(cwd, 'message from before push')
await gitEmptyCommit(cwd, 'wrong message 1') await gitEmptyCommit(cwd, firstMessage)
await gitEmptyCommit(cwd, 'wrong message 2') await gitEmptyCommit(cwd, secondMessage)
await gitEmptyCommit(cwd, 'wrong message 3')
await createPullRequestEventPayload(cwd) await createPullRequestEventPayload(cwd)
const [, first, second, to] = await getCommitHashes(cwd) const [, first, to] = await getCommitHashes(cwd)
updatePullRequestEnvVars(cwd, to) updatePullRequestEnvVars(cwd, to)
td.when( td.when(
listCommits({ listCommits({
@ -264,21 +268,57 @@ describe('Commit Linter action', () => {
pull_number: '1', pull_number: '1',
}), }),
).thenResolve({ ).thenResolve({
data: [first, second, to].map(sha => ({ sha })), data: [first, to].map(sha => ({ sha })),
}) })
td.replace(process, 'cwd', () => cwd) td.replace(process, 'cwd', () => cwd)
expectedResultsOutput = [
{
hash: to,
message: secondMessage,
valid: false,
errors: ['subject may not be empty', 'type may not be empty'],
warnings: [],
},
{
hash: first,
message: firstMessage,
valid: false,
errors: ['subject may not be empty', 'type may not be empty'],
warnings: [],
},
]
})
it('should NOT show errors for a message from before the push', async () => {
await runAction() await runAction()
td.verify(core.setFailed(contains('message from before push')), { td.verify(core.setFailed(contains('message from before push')), {
times: 0, times: 0,
}) })
td.verify(core.setFailed(contains('wrong message 1')))
td.verify(core.setFailed(contains('wrong message 2')))
td.verify(core.setFailed(contains('wrong message 3')))
}) })
it('should show an error message when failing to fetch commits', async () => { it('should show errors for the first wrong message', async () => {
await runAction()
td.verify(core.setFailed(contains(firstMessage)))
})
it('should show errors for the second wrong message', async () => {
await runAction()
td.verify(core.setFailed(contains(secondMessage)))
})
it('should generate a JSON output of the errors', async () => {
await runAction()
td.verify(core.setOutput(resultsOutputId, expectedResultsOutput))
})
})
describe('when it fails to fetch commits', () => {
beforeEach(async () => {
cwd = await git.bootstrap('fixtures/conventional') cwd = await git.bootstrap('fixtures/conventional')
td.when(core.getInput('configFile')).thenReturn('./commitlint.config.js') td.when(core.getInput('configFile')).thenReturn('./commitlint.config.js')
await gitEmptyCommit(cwd, 'commit message') await gitEmptyCommit(cwd, 'commit message')
@ -293,7 +333,9 @@ describe('Commit Linter action', () => {
}), }),
).thenReject(new Error('HttpError: Bad credentials')) ).thenReject(new Error('HttpError: Bad credentials'))
td.replace(process, 'cwd', () => cwd) td.replace(process, 'cwd', () => cwd)
})
it('should show an error message', async () => {
await runAction() await runAction()
td.verify( td.verify(
@ -301,8 +343,14 @@ describe('Commit Linter action', () => {
contains("error trying to get list of pull request's commits"), contains("error trying to get list of pull request's commits"),
), ),
) )
})
it('should show the original error message', async () => {
await runAction()
td.verify(core.setFailed(contains('HttpError: Bad credentials'))) td.verify(core.setFailed(contains('HttpError: Bad credentials')))
}) })
})
describe("when there's a single commit with correct message", () => { describe("when there's a single commit with correct message", () => {
let commitHash let commitHash