Skip to content

Commit

Permalink
passing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GrantBirki committed Mar 25, 2023
1 parent d63e7de commit 7b897a1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
22 changes: 20 additions & 2 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
import * as core from '@actions/core'
import {run} from '../src/main'
import * as jsonValidator from '../src/functions/json-validator'

const infoMock = jest.spyOn(core, 'info').mockImplementation(() => {})
const setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation(() => {})

beforeEach(() => {
jest.clearAllMocks()
jest.spyOn(jsonValidator, 'jsonValidator').mockImplementation(() => {
return {success: true}
})
})

test('successfully runs the action', async () => {
expect(await run()).toBe(true)
expect(infoMock).toHaveBeenCalledWith('✅ all JSON files are valid')
})

test('fails the action due to json errors', async () => {
jest.spyOn(jsonValidator, 'jsonValidator').mockImplementation(() => {
return undefined
return {success: false, failed: 3, passed: 8}
})
expect(await run()).toBe(undefined)
expect(await run()).toBe(false)
expect(infoMock).toHaveBeenCalledWith(
'JSON Validation Results:\n - Passed: 8\n - Failed: 3'
)
expect(setFailedMock).toHaveBeenCalledWith(
'❌ 3 JSON files failed validation'
)
})
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 17 additions & 10 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

13 changes: 10 additions & 3 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// import * as core from '@actions/core'
import * as core from '@actions/core'
// import * as github from '@actions/github'
// import {context} from '@actions/github'
// import dedent from 'dedent-js'
Expand All @@ -7,8 +7,15 @@ import {jsonValidator} from './functions/json-validator'
export async function run() {
const jsonResult = await jsonValidator()

if (jsonResult) {
core.info('✅ All JSON files are valid')
if (jsonResult.success === true) {
core.info('✅ all JSON files are valid')
return true
} else {
core.info(
`JSON Validation Results:\n - Passed: ${jsonResult.passed}\n - Failed: ${jsonResult.failed}`
)
core.setFailed(`❌ ${jsonResult.failed} JSON files failed validation`)
return false
}
}

Expand Down

0 comments on commit 7b897a1

Please sign in to comment.