Skip to content

Commit

Permalink
Fix broken mocks in tests, inject octokit factory
Browse files Browse the repository at this point in the history
  • Loading branch information
matejchalk committed Oct 17, 2024
1 parent e74c67b commit 381ad16
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 96 deletions.
118 changes: 37 additions & 81 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { ArtifactClient, UploadArtifactResponse } from '@actions/artifact'
import core from '@actions/core'
import github from '@actions/github'
import type { Context } from '@actions/github/lib/context'
import { jest } from '@jest/globals'
import type { components } from '@octokit/openapi-types'
import type { RestEndpointMethodTypes } from '@octokit/plugin-rest-endpoint-methods'
Expand All @@ -21,77 +20,14 @@ import {
type FetchResult,
type SimpleGit
} from 'simple-git'
import type { ActionInputs } from '../src/inputs'
import { run } from '../src/main'

describe('code-pushup action', () => {
const workDir = join('tmp', 'git-repo')

let git: SimpleGit
let artifact: ArtifactClient

beforeEach(async () => {
jest.clearAllMocks()

jest.spyOn(process, 'cwd').mockReturnValue(workDir)

jest.spyOn(core, 'setOutput').mockReturnValue()
jest.spyOn(core, 'setFailed').mockReturnValue()
jest.spyOn(core, 'error').mockReturnValue()
jest.spyOn(core, 'warning').mockReturnValue()
jest.spyOn(core, 'notice').mockReturnValue()
jest.spyOn(core, 'info').mockReturnValue()
jest.spyOn(core, 'debug').mockReturnValue()
jest.spyOn(core, 'isDebug').mockReturnValue(false)

jest.spyOn(core, 'getInput').mockImplementation((name: string): string => {
switch (name as keyof ActionInputs) {
case 'token':
return '<mock-github-token>'
case 'bin':
return 'npx code-pushup'
case 'directory':
return workDir
case 'retention':
return '14'
default:
return ''
}
})
jest
.spyOn(core, 'getBooleanInput')
.mockImplementation((name: string): boolean => {
switch (name as keyof ActionInputs) {
case 'silent':
return true
case 'artifacts':
return true
case 'annotations':
return true
default:
return false
}
})

// @ts-expect-error context is readonly
github.context = {
repo: {
owner: 'dunder-mifflin',
repo: 'website'
},
payload: {},
get issue() {
return {
owner: github.context.repo.owner,
repo: github.context.repo.repo,
number:
github.context.payload.pull_request?.number ??
github.context.payload.number
}
}
} as Context

const mockOctokit = {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const getOctokit = () =>
({
// eslint-disable-next-line @typescript-eslint/no-explicit-any
paginate: (async () => []) as any,
rest: {
Expand All @@ -114,8 +50,38 @@ describe('code-pushup action', () => {
})
}
}
} as ReturnType<typeof github.getOctokit>
jest.spyOn(github, 'getOctokit').mockReturnValue(mockOctokit)
}) as ReturnType<typeof github.getOctokit>

let git: SimpleGit
let artifact: ArtifactClient

beforeEach(async () => {
jest.clearAllMocks()

jest.spyOn(process, 'cwd').mockReturnValue(workDir)

jest.spyOn(core, 'setFailed').mockReturnValue()

process.env['INPUT_TOKEN'] = '<mock-github-token>'
process.env['INPUT_BIN'] = 'npx code-pushup'
process.env['INPUT_DIRECTORY'] = workDir
process.env['INPUT_RETENTION'] = '14'
process.env['INPUT_TASK'] = 'code-pushup'
process.env['INPUT_SILENT'] = 'true'
process.env['INPUT_ARTIFACTS'] = 'true'
process.env['INPUT_ANNOTATIONS'] = 'true'

jest.spyOn(github.context, 'repo', 'get').mockReturnValue({
owner: 'dunder-mifflin',
repo: 'website'
})
jest.spyOn(github.context, 'issue', 'get').mockImplementation(() => ({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
number:
github.context.payload.pull_request?.number ??
github.context.payload.number
}))

artifact = {
uploadArtifact: jest
Expand Down Expand Up @@ -168,7 +134,7 @@ describe('code-pushup action', () => {
})

it('should collect report', async () => {
await run(artifact, git)
await run(artifact, getOctokit, git)

expect(core.setFailed).not.toHaveBeenCalled()

Expand All @@ -185,8 +151,6 @@ describe('code-pushup action', () => {
{ retentionDays: 14 }
)

expect(core.setOutput).toHaveBeenCalledWith('artifact-id', 123)

const jsonPromise = readFile(
join(workDir, '.code-pushup/report.json'),
'utf8'
Expand Down Expand Up @@ -230,15 +194,7 @@ describe('code-pushup action', () => {
})

it('should compare reports', async () => {
await run(artifact, git)

expect(core.setFailed).not.toHaveBeenCalled()

expect(core.error).not.toHaveBeenCalled()
expect(core.warning).not.toHaveBeenCalled()
expect(core.notice).not.toHaveBeenCalled()

expect(core.setOutput).toHaveBeenCalledWith('comment-id', 10)
await run(artifact, getOctokit, git)

const mdPromise = readFile(
join(workDir, '.code-pushup/report-diff.md'),
Expand Down
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.
13 changes: 6 additions & 7 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ export class GitHubApiClient implements ProviderAPIClient {
constructor(
private readonly token: string,
private readonly refs: GitRefs,
private readonly artifact: ArtifactClient
private readonly artifact: ArtifactClient,
getOctokit: typeof github.getOctokit
) {
this.octokit = github.getOctokit(token)
this.octokit = getOctokit(token)
}

async createComment(body: string): Promise<Comment> {
Expand Down Expand Up @@ -52,7 +53,12 @@ export class GitHubApiClient implements ProviderAPIClient {
core.debug(`Tried to download artifact without base branch, skipping`)
return null
}
return downloadReportArtifact(this.artifact, this.refs.base, this.token)
return downloadReportArtifact(
this.artifact,
this.octokit,
this.refs.base,
this.token
)
}

private convertComment(
Expand Down
3 changes: 1 addition & 2 deletions src/artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@ export async function uploadArtifact(

export async function downloadReportArtifact(
artifact: ArtifactClient,
octokit: ReturnType<typeof github.getOctokit>,
branch: GitBranch,
token: string
): Promise<string | null> {
const octokit = github.getOctokit(token)

const {
data: { workflow_id }
} = await octokit.rest.actions.getWorkflowRun({
Expand Down
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ import { parseGitRefs } from './refs'

export async function run(
artifact = new DefaultArtifactClient(),
getOctokit = github.getOctokit,
git = simpleGit()
): Promise<void> {
try {
const inputs = parseInputs()

const refs = parseGitRefs()
const api = new GitHubApiClient(inputs.token, refs, artifact)
const api = new GitHubApiClient(inputs.token, refs, artifact, getOctokit)
const options = createOptions(inputs)

const result = await runInCI(refs, api, options, git)
Expand Down

0 comments on commit 381ad16

Please sign in to comment.