11import type { ArtifactClient , UploadArtifactResponse } from '@actions/artifact'
2- import * as core from '@actions/core'
3- import * as github from '@actions/github'
2+ import core from '@actions/core'
3+ import github from '@actions/github'
44import type { Context } from '@actions/github/lib/context'
5+ import { jest } from '@jest/globals'
56import type { components } from '@octokit/openapi-types'
67import type { RestEndpointMethodTypes } from '@octokit/plugin-rest-endpoint-methods'
78import {
@@ -12,30 +13,36 @@ import {
1213 rm ,
1314 writeFile
1415} from 'node:fs/promises'
15- import { join } from 'node:path'
16- import { simpleGit , type DiffResult , type SimpleGit } from 'simple-git'
16+ import { dirname , join } from 'node:path'
17+ import { fileURLToPath } from 'node:url'
18+ import {
19+ simpleGit ,
20+ type DiffResult ,
21+ type FetchResult ,
22+ type SimpleGit
23+ } from 'simple-git'
1724import type { ActionInputs } from '../src/inputs'
1825import { run } from '../src/main'
1926
20- jest . mock ( '@actions/core' )
21- jest . mock ( '@actions/github' )
22-
2327describe ( 'code-pushup action' , ( ) => {
2428 const workDir = join ( 'tmp' , 'git-repo' )
2529
2630 let git : SimpleGit
2731 let artifact : ArtifactClient
2832
29- let cwdSpy : jest . SpiedFunction < typeof process . cwd >
30-
3133 beforeEach ( async ( ) => {
3234 jest . clearAllMocks ( )
3335
34- cwdSpy = jest . spyOn ( process , 'cwd' ) . mockReturnValue ( workDir )
36+ jest . spyOn ( process , 'cwd' ) . mockReturnValue ( workDir )
3537
36- // uncomment to see debug logs
37- // jest.spyOn(core, 'debug').mockImplementation(console.debug)
38- // jest.spyOn(core, 'isDebug').mockReturnValue(true)
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 )
3946
4047 jest . spyOn ( core , 'getInput' ) . mockImplementation ( ( name : string ) : string => {
4148 switch ( name as keyof ActionInputs ) {
@@ -111,22 +118,26 @@ describe('code-pushup action', () => {
111118 jest . spyOn ( github , 'getOctokit' ) . mockReturnValue ( mockOctokit )
112119
113120 artifact = {
114- uploadArtifact : jest . fn ( ) . mockResolvedValue ( {
115- id : 123
116- } as UploadArtifactResponse ) as ArtifactClient [ 'uploadArtifact' ]
117- } as ArtifactClient
121+ uploadArtifact : jest
122+ . fn < ArtifactClient [ 'uploadArtifact' ] > ( )
123+ . mockResolvedValue ( { id : 123 } as UploadArtifactResponse )
124+ } as Partial < ArtifactClient > as ArtifactClient
118125
119126 await rm ( workDir , { recursive : true , force : true } )
120127 await mkdir ( workDir , { recursive : true } )
121128 await copyFile (
122- join ( __dirname , 'fixtures' , 'code-pushup.config.ts' ) ,
129+ join (
130+ fileURLToPath ( dirname ( import . meta. url ) ) ,
131+ 'fixtures' ,
132+ 'code-pushup.config.ts'
133+ ) ,
123134 join ( workDir , 'code-pushup.config.ts' )
124135 )
125136 await writeFile ( join ( workDir , 'index.js' ) , 'console.log("Hello, world!")' )
126137
127138 git = simpleGit ( workDir )
128139
129- jest . spyOn ( git , 'fetch' ) . mockImplementation ( )
140+ jest . spyOn ( git , 'fetch' ) . mockResolvedValue ( { } as FetchResult )
130141 jest . spyOn ( git , 'diffSummary' ) . mockResolvedValue ( {
131142 files : [ { file : 'index.ts' , binary : false } ]
132143 } as DiffResult )
@@ -140,11 +151,12 @@ describe('code-pushup action', () => {
140151 await git . add ( 'index.js' )
141152 await git . add ( 'code-pushup.config.ts' )
142153 await git . commit ( 'Initial commit' )
154+
155+ github . context . ref = 'refs/heads/main'
156+ github . context . sha = await git . revparse ( 'main' )
143157 } )
144158
145159 afterAll ( async ( ) => {
146- cwdSpy . mockRestore ( )
147-
148160 await rm ( workDir , { recursive : true , force : true } )
149161 } )
150162
0 commit comments