Skip to content

Commit

Permalink
adds basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nickzelei committed Dec 4, 2023
1 parent 778018c commit 3efe29e
Show file tree
Hide file tree
Showing 5 changed files with 28,699 additions and 890 deletions.
129 changes: 48 additions & 81 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,89 +1,56 @@
// /**
// * 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'
/**
* 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 tc from '@actions/tool-cache'
import * as main from '../src/main'

// // Mock the action's main function
// const runMock = jest.spyOn(main, 'run')
const runMock = jest.spyOn(main, 'run')

// // Other utilities
// const timeRegex = /^\d{2}:\d{2}:\d{2}/

// // Mock the GitHub Actions core library
// let debugMock: jest.SpyInstance
// let errorMock: jest.SpyInstance
// let getInputMock: jest.SpyInstance
// let setFailedMock: jest.SpyInstance
// let setOutputMock: jest.SpyInstance

// describe('action', () => {
// beforeEach(() => {
// jest.clearAllMocks()

// debugMock = jest.spyOn(core, 'debug').mockImplementation()
// errorMock = jest.spyOn(core, 'error').mockImplementation()
// getInputMock = jest.spyOn(core, 'getInput').mockImplementation()
// setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation()
// setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation()
// })

// // 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()

// // // 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)
// // )
// // expect(errorMock).not.toHaveBeenCalled()
// // })

// // 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()

// // // Verify that all of the core library functions were called correctly
// // expect(setFailedMock).toHaveBeenNthCalledWith(
// // 1,
// // 'milliseconds not a number'
// // )
// // expect(errorMock).not.toHaveBeenCalled()
// // })
// })
let errorMock: jest.SpyInstance
let getInputMock: jest.SpyInstance
let addPathMock: jest.SpyInstance
let downloadToolMock: jest.SpyInstance
let extractTarMock: jest.SpyInstance

describe('action', () => {
beforeEach(() => {
jest.clearAllMocks()

getInputMock = jest.spyOn(core, 'getInput').mockImplementation()
addPathMock = jest.spyOn(core, 'addPath').mockImplementation()
errorMock = jest.spyOn(core, 'setFailed').mockImplementation()
downloadToolMock = jest.spyOn(tc, 'downloadTool').mockImplementation()
extractTarMock = jest.spyOn(tc, 'extractTar').mockImplementation()
})

it('sets the version output', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation((name: string): string => {
switch (name) {
case 'version':
return 'v0.0.18'
default:
return ''
}
})

await main.run()
expect(runMock).toHaveReturned()
expect(errorMock).not.toHaveBeenCalled()
expect(downloadToolMock).toHaveBeenCalled()
expect(extractTarMock).toHaveBeenCalled()
expect(addPathMock).toHaveBeenCalled()
})
})
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 3efe29e

Please sign in to comment.