Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add new some test #92

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions __tests__/action.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const core = require('@actions/core')
const main = require('../src/action')

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

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

it.each(['scope', 'database', 'report'])('should throw an error if the %s is not provided', async (input) => {
getInputMock.mockImplementation(name => {
if (name === input) {
throw new Error(`Input required and not supplied: ${input}`)
}

return ''
})
await main.run()

expect(runMock).toHaveReturned()
expect(setFailedMock).toHaveBeenNthCalledWith(
1,
`Input required and not supplied: ${input}`
)
})

it('should throw an error if the available report is not valid', async () => {
getInputMock.mockImplementation(name => {
if (name === 'report-tool') {
return 'invalid'
}

return ''
})
await main.run()

expect(runMock).toHaveReturned()
expect(setFailedMock).toHaveBeenNthCalledWith(1,
'The report-tool is not valid, please use: scorecard-visualizer, deps.dev'
)
})

it.todo("should't throw an error if the available report is valid")
it.todo("should scorecard-visualizer be the default report tool")

it.each(['auto-push', 'generate-issue', 'auto-commit', 'discovery-enabled'])('should throw an error if the github token is not provided when %s is enabled', async (input) => {
getInputMock.mockImplementation(name => {
if (name === input) {
return 'true'
}

return ''
})
await main.run()

expect(runMock).toHaveReturned()
expect(setFailedMock).toHaveBeenNthCalledWith(1,
'Github token is required for push, commit, create an issue and discovery operations!'
)
})

it('should throw an error if discovery is enabled but no organizations are provided', async () => {
getInputMock.mockImplementation(name => {
if (name === 'github-token') {
return 'ghp_obj_obj'
}
if (name === 'discovery-enabled') {
return 'true'
}

return ''
})
await main.run()

expect(runMock).toHaveReturned()
expect(setFailedMock).toHaveBeenNthCalledWith(1,
'Discovery is enabled but no organizations were provided!'
)
})
})
14 changes: 14 additions & 0 deletions __tests__/bin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { run } = require('../src/action')

// Mock the action's entrypoint
jest.mock('../src/action', () => ({
run: jest.fn()
}))

describe('index', () => {
it('calls run when imported', async () => {
require('../src/bin')

expect(run).toHaveBeenCalled()
})
})
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"name": "openssf-scorecard-monitor",
"version": "2.0.0-beta8",
"description": "A simple way to monitor OpenSSF Scorecard at organization level",
"main": "src/index.js",
"main": "src/action.js",
"private": true,
"scripts": {
"build": "ncc build src/action.js -o dist",
"build": "ncc build src/bin.js -o dist",
"test": "FORCE_COLOR=3 jest --verbose",
"test:update": "FORCE_COLOR=3 jest --verbose --u",
"test:coverage": "FORCE_COLOR=3 jest --verbose --coverage",
Expand Down
Loading