Skip to content

Commit

Permalink
test: complete test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
willgeorgetaylor committed Dec 29, 2023
1 parent 1e9fd12 commit 0003dcf
Show file tree
Hide file tree
Showing 9 changed files with 307 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ jobs:
- name: Test Local Action
uses: ./
with:
include: ./fixtures/*.xml
include: ./__tests__/fixtures/*.xml
File renamed without changes.
File renamed without changes.
159 changes: 159 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/**
* Unit tests for the action's main functionality, src/main.ts
*
* These should be run as if the action was called from a workflow.
* Specifically, the inputs listed in `action.yml` should be set as environment
* variables following the pattern `INPUT_<INPUT_NAME>`.
*/

import * as core from '@actions/core'
import * as main from '../src/main'

// Mock the action's main function
const runMock = jest.spyOn(main, 'run')

// Mock the GitHub Actions core library
let infoMock: jest.SpyInstance
let errorMock: jest.SpyInstance
let getInputMock: jest.SpyInstance
let setFailedMock: jest.SpyInstance

describe('action', () => {
beforeEach(() => {
jest.clearAllMocks()
process.env.RUNNER_TEMP = '/tmp'
infoMock = jest.spyOn(core, 'info').mockImplementation()
errorMock = jest.spyOn(core, 'error').mockImplementation()
getInputMock = jest.spyOn(core, 'getInput').mockImplementation()
setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation()
})

it('successfully completes', async () => {
await main.run()
expect(runMock).toHaveReturned()
expect(errorMock).not.toHaveBeenCalled()
expect(setFailedMock).not.toHaveBeenCalled()
expect(infoMock).toHaveBeenLastCalledWith(
'junit-reducer exited successfully'
)
})

it('successfully completes with a specific version', async () => {
getInputMock.mockImplementation((name: string): string => {
switch (name) {
case 'version':
return 'v1.2.0'
default:
return ''
}
})

await main.run()
expect(runMock).toHaveReturned()
expect(errorMock).not.toHaveBeenCalled()
expect(setFailedMock).not.toHaveBeenCalled()
expect(infoMock).toHaveBeenLastCalledWith(
'junit-reducer exited successfully'
)
})

describe('on mock Windows', () => {
beforeEach(() => {
process.env.OVERRIDE_PLATFORM = 'win32'
})
it('successfully downloads the Windows binary', async () => {
await main.run()
expect(runMock).toHaveReturned()
expect(infoMock).toHaveBeenCalledWith('Windows archive downloaded')
expect(errorMock).toHaveBeenCalledWith(
"Can't install extension on mocked platform"
)
})
afterEach(() => {
delete process.env.OVERRIDE_PLATFORM
})
})

describe('on mock Mac', () => {
beforeEach(() => {
process.env.OVERRIDE_PLATFORM = 'darwin'
})
it('successfully downloads the Mac binary', async () => {
await main.run()
expect(runMock).toHaveReturned()
expect(infoMock).toHaveBeenCalledWith('Mac archive downloaded')
expect(errorMock).toHaveBeenCalledWith(
"Can't install extension on mocked platform"
)
})
afterEach(() => {
delete process.env.OVERRIDE_PLATFORM
})
})

describe('on mock Linux', () => {
beforeEach(() => {
process.env.OVERRIDE_PLATFORM = 'linux'
})
it('successfully downloads the Linux binary', async () => {
await main.run()
expect(runMock).toHaveReturned()
expect(infoMock).toHaveBeenCalledWith('Linux archive downloaded')
expect(errorMock).toHaveBeenCalledWith(
"Can't install extension on mocked platform"
)
})
afterEach(() => {
delete process.env.OVERRIDE_PLATFORM
})
})

describe('on any other platform', () => {
beforeEach(() => {
process.env.OVERRIDE_PLATFORM = 'unknown'
})
it('successfully downloads the Linux binary', async () => {
await main.run()
expect(runMock).toHaveReturned()
expect(infoMock).toHaveBeenCalledWith('Linux archive downloaded')
expect(errorMock).toHaveBeenCalledWith(
"Can't install extension on mocked platform"
)
})
afterEach(() => {
delete process.env.OVERRIDE_PLATFORM
})
})

it('sets a failed status for invalid version', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation((name: string): string => {
switch (name) {
case 'version':
return 'this is not a number'
default:
return ''
}
})

await main.run()
expect(runMock).toHaveReturned()

// Verify that all of the core library functions were called correctly
expect(setFailedMock).toHaveBeenNthCalledWith(
1,
'invalid version string: this is not a number'
)
expect(errorMock).not.toHaveBeenCalled()
})

it('sets a failed status for non-zero status code thrown by the binary', async () => {
// Set input variable 'include' to a non-existent file glob
process.env.INPUT_include = 'this-file-does-not-exist.xml'

await main.run()
expect(runMock).toHaveReturned()
expect(errorMock).not.toHaveBeenCalled()
expect(setFailedMock).toHaveBeenCalled()
})
})
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.
92 changes: 58 additions & 34 deletions dist/index.js

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

8 changes: 8 additions & 0 deletions output/Sample.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="Admin::Jobs::Applications::Actions::CreativeServicesIncBackgroundCheckTest" filepath="test/system/admin/jobs/applications/actions/creative_services_inc_background_check_test.rb" time="49.09959481199999" tests="5" failed="0" errors="0" skipped="0" assertions="17">
<testcase name="test_should_not_see_Creative_Services_Inc_integration_when_removed" classname="Admin::Jobs::Applications::Actions::CreativeServicesIncBackgroundCheckTest" file="test/system/admin/jobs/applications/actions/creative_services_inc_background_check_test.rb" lineno="53" assertions="1" time="4.922785552499988"></testcase>
<testcase name="test_should_show_each_of_the_different_values_depending_on_which_billing_option_you_select" classname="Admin::Jobs::Applications::Actions::CreativeServicesIncBackgroundCheckTest" file="test/system/admin/jobs/applications/actions/creative_services_inc_background_check_test.rb" lineno="90" assertions="0" time="12.873165432000008"></testcase>
<testcase name="test_should_not_be_able_to_view_a_background_check_without_background_check_viewer_role" classname="Admin::Jobs::Applications::Actions::CreativeServicesIncBackgroundCheckTest" file="test/system/admin/jobs/applications/actions/creative_services_inc_background_check_test.rb" lineno="176" assertions="2" time="4.697016684999994"></testcase>
</testsuite>
</testsuites>
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"jest": {
"preset": "ts-jest",
"verbose": true,
"silent": false,
"clearMocks": true,
"testEnvironment": "node",
"moduleFileExtensions": [
Expand Down Expand Up @@ -87,4 +88,4 @@
"ts-jest": "^29.1.1",
"typescript": "^5.3.3"
}
}
}
Loading

0 comments on commit 0003dcf

Please sign in to comment.