11import type { ArtifactClient , UploadArtifactResponse } from '@actions/artifact'
22import core from '@actions/core'
33import github from '@actions/github'
4- import type { Context } from '@actions/github/lib/context'
54import { jest } from '@jest/globals'
65import type { components } from '@octokit/openapi-types'
76import 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'
2523import { run } from '../src/main'
2624
2725describe ( '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' ) ,
0 commit comments