Skip to content

Commit 381ad16

Browse files
committed
Fix broken mocks in tests, inject octokit factory
1 parent e74c67b commit 381ad16

File tree

7 files changed

+57
-96
lines changed

7 files changed

+57
-96
lines changed

__tests__/main.test.ts

Lines changed: 37 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { ArtifactClient, UploadArtifactResponse } from '@actions/artifact'
22
import core from '@actions/core'
33
import github from '@actions/github'
4-
import type { Context } from '@actions/github/lib/context'
54
import { jest } from '@jest/globals'
65
import type { components } from '@octokit/openapi-types'
76
import type { RestEndpointMethodTypes } from '@octokit/plugin-rest-endpoint-methods'
@@ -21,77 +20,14 @@ import {
2120
type FetchResult,
2221
type SimpleGit
2322
} from 'simple-git'
24-
import type { ActionInputs } from '../src/inputs'
2523
import { run } from '../src/main'
2624

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

30-
let git: SimpleGit
31-
let artifact: ArtifactClient
32-
33-
beforeEach(async () => {
34-
jest.clearAllMocks()
35-
36-
jest.spyOn(process, 'cwd').mockReturnValue(workDir)
37-
38-
jest.spyOn(core, 'setOutput').mockReturnValue()
39-
jest.spyOn(core, 'setFailed').mockReturnValue()
40-
jest.spyOn(core, 'error').mockReturnValue()
41-
jest.spyOn(core, 'warning').mockReturnValue()
42-
jest.spyOn(core, 'notice').mockReturnValue()
43-
jest.spyOn(core, 'info').mockReturnValue()
44-
jest.spyOn(core, 'debug').mockReturnValue()
45-
jest.spyOn(core, 'isDebug').mockReturnValue(false)
46-
47-
jest.spyOn(core, 'getInput').mockImplementation((name: string): string => {
48-
switch (name as keyof ActionInputs) {
49-
case 'token':
50-
return '<mock-github-token>'
51-
case 'bin':
52-
return 'npx code-pushup'
53-
case 'directory':
54-
return workDir
55-
case 'retention':
56-
return '14'
57-
default:
58-
return ''
59-
}
60-
})
61-
jest
62-
.spyOn(core, 'getBooleanInput')
63-
.mockImplementation((name: string): boolean => {
64-
switch (name as keyof ActionInputs) {
65-
case 'silent':
66-
return true
67-
case 'artifacts':
68-
return true
69-
case 'annotations':
70-
return true
71-
default:
72-
return false
73-
}
74-
})
75-
76-
// @ts-expect-error context is readonly
77-
github.context = {
78-
repo: {
79-
owner: 'dunder-mifflin',
80-
repo: 'website'
81-
},
82-
payload: {},
83-
get issue() {
84-
return {
85-
owner: github.context.repo.owner,
86-
repo: github.context.repo.repo,
87-
number:
88-
github.context.payload.pull_request?.number ??
89-
github.context.payload.number
90-
}
91-
}
92-
} as Context
93-
94-
const mockOctokit = {
28+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
29+
const getOctokit = () =>
30+
({
9531
// eslint-disable-next-line @typescript-eslint/no-explicit-any
9632
paginate: (async () => []) as any,
9733
rest: {
@@ -114,8 +50,38 @@ describe('code-pushup action', () => {
11450
})
11551
}
11652
}
117-
} as ReturnType<typeof github.getOctokit>
118-
jest.spyOn(github, 'getOctokit').mockReturnValue(mockOctokit)
53+
}) as ReturnType<typeof github.getOctokit>
54+
55+
let git: SimpleGit
56+
let artifact: ArtifactClient
57+
58+
beforeEach(async () => {
59+
jest.clearAllMocks()
60+
61+
jest.spyOn(process, 'cwd').mockReturnValue(workDir)
62+
63+
jest.spyOn(core, 'setFailed').mockReturnValue()
64+
65+
process.env['INPUT_TOKEN'] = '<mock-github-token>'
66+
process.env['INPUT_BIN'] = 'npx code-pushup'
67+
process.env['INPUT_DIRECTORY'] = workDir
68+
process.env['INPUT_RETENTION'] = '14'
69+
process.env['INPUT_TASK'] = 'code-pushup'
70+
process.env['INPUT_SILENT'] = 'true'
71+
process.env['INPUT_ARTIFACTS'] = 'true'
72+
process.env['INPUT_ANNOTATIONS'] = 'true'
73+
74+
jest.spyOn(github.context, 'repo', 'get').mockReturnValue({
75+
owner: 'dunder-mifflin',
76+
repo: 'website'
77+
})
78+
jest.spyOn(github.context, 'issue', 'get').mockImplementation(() => ({
79+
owner: github.context.repo.owner,
80+
repo: github.context.repo.repo,
81+
number:
82+
github.context.payload.pull_request?.number ??
83+
github.context.payload.number
84+
}))
11985

12086
artifact = {
12187
uploadArtifact: jest
@@ -168,7 +134,7 @@ describe('code-pushup action', () => {
168134
})
169135

170136
it('should collect report', async () => {
171-
await run(artifact, git)
137+
await run(artifact, getOctokit, git)
172138

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

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

188-
expect(core.setOutput).toHaveBeenCalledWith('artifact-id', 123)
189-
190154
const jsonPromise = readFile(
191155
join(workDir, '.code-pushup/report.json'),
192156
'utf8'
@@ -230,15 +194,7 @@ describe('code-pushup action', () => {
230194
})
231195

232196
it('should compare reports', async () => {
233-
await run(artifact, git)
234-
235-
expect(core.setFailed).not.toHaveBeenCalled()
236-
237-
expect(core.error).not.toHaveBeenCalled()
238-
expect(core.warning).not.toHaveBeenCalled()
239-
expect(core.notice).not.toHaveBeenCalled()
240-
241-
expect(core.setOutput).toHaveBeenCalledWith('comment-id', 10)
197+
await run(artifact, getOctokit, git)
242198

243199
const mdPromise = readFile(
244200
join(workDir, '.code-pushup/report-diff.md'),

badges/coverage.svg

Lines changed: 1 addition & 1 deletion
Loading

dist/index.js

Lines changed: 6 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ export class GitHubApiClient implements ProviderAPIClient {
1313
constructor(
1414
private readonly token: string,
1515
private readonly refs: GitRefs,
16-
private readonly artifact: ArtifactClient
16+
private readonly artifact: ArtifactClient,
17+
getOctokit: typeof github.getOctokit
1718
) {
18-
this.octokit = github.getOctokit(token)
19+
this.octokit = getOctokit(token)
1920
}
2021

2122
async createComment(body: string): Promise<Comment> {
@@ -52,7 +53,12 @@ export class GitHubApiClient implements ProviderAPIClient {
5253
core.debug(`Tried to download artifact without base branch, skipping`)
5354
return null
5455
}
55-
return downloadReportArtifact(this.artifact, this.refs.base, this.token)
56+
return downloadReportArtifact(
57+
this.artifact,
58+
this.octokit,
59+
this.refs.base,
60+
this.token
61+
)
5662
}
5763

5864
private convertComment(

src/artifact.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,10 @@ export async function uploadArtifact(
5050

5151
export async function downloadReportArtifact(
5252
artifact: ArtifactClient,
53+
octokit: ReturnType<typeof github.getOctokit>,
5354
branch: GitBranch,
5455
token: string
5556
): Promise<string | null> {
56-
const octokit = github.getOctokit(token)
57-
5857
const {
5958
data: { workflow_id }
6059
} = await octokit.rest.actions.getWorkflowRun({

src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ import { parseGitRefs } from './refs'
1616

1717
export async function run(
1818
artifact = new DefaultArtifactClient(),
19+
getOctokit = github.getOctokit,
1920
git = simpleGit()
2021
): Promise<void> {
2122
try {
2223
const inputs = parseInputs()
2324

2425
const refs = parseGitRefs()
25-
const api = new GitHubApiClient(inputs.token, refs, artifact)
26+
const api = new GitHubApiClient(inputs.token, refs, artifact, getOctokit)
2627
const options = createOptions(inputs)
2728

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

0 commit comments

Comments
 (0)