mirror of
https://github.com/actions/container-toolkit-action.git
synced 2025-12-10 20:21:17 +00:00
Update linting and testing process
This commit is contained in:
17
__tests__/index.test.ts
Normal file
17
__tests__/index.test.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* Unit tests for the action's entrypoint, src/index.ts
|
||||
*/
|
||||
|
||||
import * as main from '../src/main'
|
||||
|
||||
// Mock the action's entrypoint
|
||||
const runMock = jest.spyOn(main, 'run').mockImplementation()
|
||||
|
||||
describe('index', () => {
|
||||
it('calls run when imported', async () => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||
require('../src/index')
|
||||
|
||||
expect(runMock).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
@@ -1,4 +1,68 @@
|
||||
describe('TODO - Add a test suite', () => {
|
||||
it('TODO - Add a test', async () => {
|
||||
});
|
||||
});
|
||||
/**
|
||||
* 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 GitHub Actions core library
|
||||
//const debugMock = jest.spyOn(core, 'debug')
|
||||
const infoMock = jest.spyOn(core, 'info')
|
||||
const getInputMock = jest.spyOn(core, 'getInput')
|
||||
const setFailedMock = jest.spyOn(core, 'setFailed')
|
||||
const setOutputMock = jest.spyOn(core, 'setOutput')
|
||||
|
||||
// Mock the action's main function
|
||||
const runMock = jest.spyOn(main, 'run')
|
||||
|
||||
describe('action', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
it('sets the time output', async () => {
|
||||
// Set the action's inputs as return values from core.getInput()
|
||||
getInputMock.mockImplementation((name: string): string => {
|
||||
switch (name) {
|
||||
case 'milliseconds':
|
||||
return '500'
|
||||
default:
|
||||
return ''
|
||||
}
|
||||
})
|
||||
|
||||
await main.run()
|
||||
|
||||
expect(runMock).toHaveReturned()
|
||||
expect(infoMock).toHaveBeenCalledWith(expect.stringMatching(/.*/))
|
||||
expect(infoMock).toHaveBeenCalledWith(expect.stringMatching(/.*/))
|
||||
expect(setOutputMock).toHaveBeenCalledWith(
|
||||
'time',
|
||||
expect.stringMatching(/.*/)
|
||||
)
|
||||
})
|
||||
|
||||
it('sets a failed status', async () => {
|
||||
// Set the action's inputs as return values from core.getInput()
|
||||
getInputMock.mockImplementation((name: string): string => {
|
||||
switch (name) {
|
||||
case 'milliseconds':
|
||||
return 'this is not a number'
|
||||
default:
|
||||
return ''
|
||||
}
|
||||
})
|
||||
|
||||
await main.run()
|
||||
|
||||
expect(runMock).toHaveReturned()
|
||||
expect(setFailedMock).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
'milliseconds not a number'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user