Convert to ESM

This commit is contained in:
Nick Alteen
2024-11-15 12:30:35 -05:00
parent 14c58ed4a7
commit 081d9e881a
28 changed files with 32080 additions and 9796 deletions

View File

@ -1,96 +1,62 @@
/**
* 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.
*/
const core = require('@actions/core')
const main = require('../src/main')
import { jest } from '@jest/globals'
import * as core from '../__fixtures__/core.js'
import { wait } from '../__fixtures__/wait.js'
// Mock the GitHub Actions core library
const debugMock = jest.spyOn(core, 'debug').mockImplementation()
const getInputMock = jest.spyOn(core, 'getInput').mockImplementation()
const setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation()
const setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation()
// Mocks should be declared before the module being tested is imported.
jest.unstable_mockModule('@actions/core', () => core)
jest.unstable_mockModule('../src/wait.js', () => ({ wait }))
// Mock the action's main function
const runMock = jest.spyOn(main, 'run')
// 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')
// Other utilities
const timeRegex = /^\d{2}:\d{2}:\d{2}/
describe('action', () => {
describe('main.js', () => {
beforeEach(() => {
jest.clearAllMocks()
// 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!'))
})
it('sets the time output', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation(name => {
switch (name) {
case 'milliseconds':
return '500'
default:
return ''
}
})
afterEach(() => {
jest.resetAllMocks()
})
await main.run()
expect(runMock).toHaveReturned()
it('Sets the time output', async () => {
await run()
// Verify that all of the core library functions were called correctly
expect(debugMock).toHaveBeenNthCalledWith(1, 'Waiting 500 milliseconds ...')
expect(debugMock).toHaveBeenNthCalledWith(
2,
expect.stringMatching(timeRegex)
)
expect(debugMock).toHaveBeenNthCalledWith(
3,
expect.stringMatching(timeRegex)
)
expect(setOutputMock).toHaveBeenNthCalledWith(
// Verify the time output was set.
expect(core.setOutput).toHaveBeenNthCalledWith(
1,
'time',
expect.stringMatching(timeRegex)
// 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 () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation(name => {
switch (name) {
case 'milliseconds':
return 'this is not a number'
default:
return ''
}
})
it('Sets a failed status', async () => {
// Clear the getInput mock and return an invalid value.
core.getInput.mockClear().mockReturnValueOnce('this is not a number')
await main.run()
expect(runMock).toHaveReturned()
// Clear the wait mock and return a rejected promise.
wait
.mockClear()
.mockRejectedValueOnce(new Error('milliseconds is not a number'))
// Verify that all of the core library functions were called correctly
expect(setFailedMock).toHaveBeenNthCalledWith(
await run()
// Verify that the action was marked as failed.
expect(core.setFailed).toHaveBeenNthCalledWith(
1,
'milliseconds not a number'
)
})
it('fails if no input is provided', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation(name => {
switch (name) {
case 'milliseconds':
throw new Error('Input required and not supplied: milliseconds')
default:
return ''
}
})
await main.run()
expect(runMock).toHaveReturned()
// Verify that all of the core library functions were called correctly
expect(setFailedMock).toHaveBeenNthCalledWith(
1,
'Input required and not supplied: milliseconds'
'milliseconds is not a number'
)
})
})