Files

53 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

2023-09-15 10:56:32 -04:00
/**
* Unit tests for the action's main functionality, src/main.js
*/
2025-01-08 11:40:16 -05:00
import { afterEach, beforeEach, jest } from '@jest/globals'
const core = await import('../__fixtures__/core')
const github = await import('../__fixtures__/github')
2023-09-15 10:56:32 -04:00
2025-01-08 11:40:16 -05:00
jest.unstable_mockModule('@actions/core', () => core)
jest.unstable_mockModule('@actions/github', () => github)
2023-09-15 10:56:32 -04:00
2025-01-08 11:40:16 -05:00
const main = await import('../src/main')
2023-09-15 10:56:32 -04:00
describe('action', () => {
beforeEach(() => {
// Mock the action's inputs
2025-01-08 11:40:16 -05:00
core.getInput.mockReturnValueOnce('World')
2023-09-15 10:56:32 -04:00
// Mock the action's payload
2025-01-08 11:40:16 -05:00
github.context.payload = {
actor: 'mona'
}
})
afterEach(() => {
jest.resetAllMocks()
})
2023-09-15 10:56:32 -04:00
2025-01-08 11:40:16 -05:00
it('sets the time output', async () => {
2023-09-15 10:56:32 -04:00
await main.run()
2025-01-08 11:40:16 -05:00
expect(core.setOutput).toHaveBeenCalledWith('time', expect.any(String))
2023-09-15 10:56:32 -04:00
})
it('logs the event payload', async () => {
await main.run()
2025-01-08 11:40:16 -05:00
expect(core.info).toHaveBeenCalledWith(
2023-09-15 10:56:32 -04:00
`The event payload: ${JSON.stringify(github.context.payload, null, 2)}`
)
})
it('sets a failed status', async () => {
2025-01-08 11:40:16 -05:00
// Mock a failure
core.getInput.mockReset().mockImplementation((name) => {
throw new Error('Something went wrong...')
2023-09-15 10:56:32 -04:00
})
await main.run()
2025-01-08 11:40:16 -05:00
expect(core.setFailed).toHaveBeenCalledWith('Something went wrong...')
2023-09-15 10:56:32 -04:00
})
})