mirror of
https://github.com/actions/javascript-action.git
synced 2025-04-06 23:39:39 +00:00
Restructure source and tests
This commit is contained in:
18
__tests__/index.test.js
Normal file
18
__tests__/index.test.js
Normal file
@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Unit tests for the action's entrypoint, src/index.ts
|
||||
*/
|
||||
|
||||
const { run } = require('../src/main')
|
||||
|
||||
// Mock the action's entrypoint
|
||||
jest.mock('../src/main', () => ({
|
||||
run: jest.fn()
|
||||
}))
|
||||
|
||||
describe('index', () => {
|
||||
it('calls run when imported', async () => {
|
||||
require('../src/index')
|
||||
|
||||
expect(run).toHaveBeenCalled()
|
||||
})
|
||||
})
|
100
__tests__/main.test.js
Normal file
100
__tests__/main.test.js
Normal file
@ -0,0 +1,100 @@
|
||||
/**
|
||||
* 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>`.
|
||||
*/
|
||||
const core = require('@actions/core')
|
||||
const main = require('../src/main')
|
||||
|
||||
// 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()
|
||||
|
||||
// Mock the action's main function
|
||||
const runMock = jest.spyOn(main, 'run')
|
||||
|
||||
// Other utilities
|
||||
const timeRegex = /^\d{2}:\d{2}:\d{2}/
|
||||
|
||||
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 => {
|
||||
switch (name) {
|
||||
case 'milliseconds':
|
||||
return '500'
|
||||
default:
|
||||
return ''
|
||||
}
|
||||
})
|
||||
|
||||
await main.run()
|
||||
expect(runMock).toHaveReturned()
|
||||
|
||||
// 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(
|
||||
1,
|
||||
'time',
|
||||
expect.stringMatching(timeRegex)
|
||||
)
|
||||
})
|
||||
|
||||
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 ''
|
||||
}
|
||||
})
|
||||
|
||||
await main.run()
|
||||
expect(runMock).toHaveReturned()
|
||||
|
||||
// Verify that all of the core library functions were called correctly
|
||||
expect(setFailedMock).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'
|
||||
)
|
||||
})
|
||||
})
|
24
__tests__/wait.test.js
Normal file
24
__tests__/wait.test.js
Normal file
@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Unit tests for src/wait.ts
|
||||
*/
|
||||
const { wait } = require('../src/wait')
|
||||
const { expect } = require('@jest/globals')
|
||||
|
||||
describe('wait.ts', () => {
|
||||
it('throws an invalid number', async () => {
|
||||
const input = parseInt('foo', 10)
|
||||
expect(isNaN(input)).toBe(true)
|
||||
|
||||
await expect(wait(input)).rejects.toThrow('milliseconds not a number')
|
||||
})
|
||||
|
||||
it('waits with a valid number', async () => {
|
||||
const start = new Date()
|
||||
await wait(500)
|
||||
const end = new Date()
|
||||
|
||||
const delta = Math.abs(end.getTime() - start.getTime())
|
||||
|
||||
expect(delta).toBeGreaterThan(450)
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user