mirror of
https://github.com/actions/javascript-action.git
synced 2025-04-06 15:29:40 +00:00
63 lines
1.9 KiB
JavaScript
63 lines
1.9 KiB
JavaScript
/**
|
|
* Unit tests for the action's main functionality, src/main.js
|
|
*
|
|
* To mock dependencies in ESM, you can create fixtures that export mock
|
|
* functions and objects. For example, the core module is mocked in this test,
|
|
* so that the actual '@actions/core' module is not imported.
|
|
*/
|
|
import { jest } from '@jest/globals'
|
|
import * as core from '../__fixtures__/core.js'
|
|
import { wait } from '../__fixtures__/wait.js'
|
|
|
|
// Mocks should be declared before the module being tested is imported.
|
|
jest.unstable_mockModule('@actions/core', () => core)
|
|
jest.unstable_mockModule('../src/wait.js', () => ({ wait }))
|
|
|
|
// The module being tested should be imported dynamically. This ensures that the
|
|
// mocks are used in place of any actual dependencies.
|
|
const { run } = await import('../src/main.js')
|
|
|
|
describe('main.js', () => {
|
|
beforeEach(() => {
|
|
// Set the action's inputs as return values from core.getInput().
|
|
core.getInput.mockImplementation(() => '500')
|
|
|
|
// Mock the wait function so that it does not actually wait.
|
|
wait.mockImplementation(() => Promise.resolve('done!'))
|
|
})
|
|
|
|
afterEach(() => {
|
|
jest.resetAllMocks()
|
|
})
|
|
|
|
it('Sets the time output', async () => {
|
|
await run()
|
|
|
|
// Verify the time output was set.
|
|
expect(core.setOutput).toHaveBeenNthCalledWith(
|
|
1,
|
|
'time',
|
|
// Simple regex to match a time string in the format HH:MM:SS.
|
|
expect.stringMatching(/^\d{2}:\d{2}:\d{2}/)
|
|
)
|
|
})
|
|
|
|
it('Sets a failed status', async () => {
|
|
// Clear the getInput mock and return an invalid value.
|
|
core.getInput.mockClear().mockReturnValueOnce('this is not a number')
|
|
|
|
// Clear the wait mock and return a rejected promise.
|
|
wait
|
|
.mockClear()
|
|
.mockRejectedValueOnce(new Error('milliseconds is not a number'))
|
|
|
|
await run()
|
|
|
|
// Verify that the action was marked as failed.
|
|
expect(core.setFailed).toHaveBeenNthCalledWith(
|
|
1,
|
|
'milliseconds is not a number'
|
|
)
|
|
})
|
|
})
|