99import * as fs from 'fs'
1010import * as os from 'os'
1111import * as path from 'path'
12- import * as core from '@actions/core'
13- import * as main from '../src/main'
14- import { getMSYS } from '../src/get-msys'
12+ import { expect , jest , describe , beforeEach , afterAll , it } from '@jest/globals'
1513
1614// Some expected string
1715const mdCoverageTable = `| Total | Frontend | Backend | SimCode | Templates | Compilation | Simulation | Verification |
1816| --- | --- | --- | --- | --- | --- | --- | --- |
1917| 2 | 2 | 2 | 2 | 2 | 2 | 2 | 1 |`
2018
21- // Mock the action's main function
22- const runMock = jest . spyOn ( main , 'run' )
2319const modelicaFile = path . resolve (
2420 path . join ( 'examples' , 'MyLibrary' , 'package.mo' )
2521)
2622const referenceFilesDir = path . resolve ( path . join ( 'examples' , 'ReferenceFiles' ) )
2723
2824// Mock the GitHub Actions core library
29- let debugMock : jest . SpyInstance
30- let errorMock : jest . SpyInstance
31- let infoMock : jest . SpyInstance
32- let getInputMock : jest . SpyInstance
33- let setFailedMock : jest . SpyInstance
34- let setOutputMock : jest . SpyInstance
25+ const debugMock = jest
26+ . fn < ( message : string ) => void > ( )
27+ . mockImplementation ( msg => console . log ( `::debug::${ msg } ` ) )
28+ const infoMock = jest
29+ . fn < ( message : string ) => void > ( )
30+ . mockImplementation ( msg => console . log ( `::info::${ msg } ` ) )
31+ const errorMock = jest
32+ . fn < ( message : string | Error ) => void > ( )
33+ . mockImplementation ( msg => console . log ( `::error::${ msg } ` ) )
34+ const getInputMock =
35+ jest . fn < ( name : string , options ?: { required ?: boolean } ) => string > ( )
36+ const setFailedMock = jest . fn < ( message : string | Error ) => void > ( )
37+ const setOutputMock = jest . fn < ( name : string , value : unknown ) => void > ( )
38+
39+ // Create a summary mock that actually writes to the file
40+ let summaryBuffer = ''
41+ const summaryMock = {
42+ addRaw : jest . fn ( ( text : string ) => {
43+ summaryBuffer += text
44+ return summaryMock
45+ } ) ,
46+ write : jest . fn ( async ( ) => {
47+ const summaryFile = process . env . GITHUB_STEP_SUMMARY
48+ if ( summaryFile ) {
49+ fs . appendFileSync ( summaryFile , summaryBuffer )
50+ }
51+ summaryBuffer = ''
52+ } )
53+ }
54+
55+ jest . unstable_mockModule ( '@actions/core' , ( ) => ( {
56+ debug : debugMock ,
57+ info : infoMock ,
58+ warning : jest . fn ( ) ,
59+ error : errorMock ,
60+ getInput : getInputMock ,
61+ setFailed : setFailedMock ,
62+ setOutput : setOutputMock ,
63+ summary : summaryMock
64+ } ) )
3565
3666// Mock @actions /artifact and @actions /github
37- jest . mock ( '@actions/artifact' )
38- jest . mock ( '@actions/github' )
67+ const uploadArtifactMock = jest
68+ . fn < ( ) => Promise < { size : number ; id : number } > > ( )
69+ . mockResolvedValue ( { size : 0 , id : 0 } )
70+
71+ jest . unstable_mockModule ( '@actions/artifact' , ( ) => ( {
72+ DefaultArtifactClient : jest . fn ( ) . mockImplementation ( ( ) => ( {
73+ uploadArtifact : uploadArtifactMock
74+ } ) )
75+ } ) )
76+
77+ jest . unstable_mockModule ( '@actions/github' , ( ) => ( {
78+ context : {
79+ repo : { owner : 'test' , repo : 'test' } ,
80+ runId : 123
81+ }
82+ } ) )
83+
84+ // Dynamic imports after mocking
85+ const main = await import ( '../src/main' )
86+ const { getMSYS } = await import ( '../src/get-msys' )
3987
4088// Set GitHub summary file
4189const gitHubStepSummaryFile = path . resolve (
@@ -56,19 +104,12 @@ describe('action', () => {
56104 fs . writeFileSync ( gitHubStepSummaryFile , '' , { flag : 'w' } )
57105
58106 jest . clearAllMocks ( )
107+ // Reset summary buffer
108+ summaryBuffer = ''
59109
60- debugMock = jest
61- . spyOn ( core , 'debug' )
62- . mockImplementation ( msg => console . log ( `::debug::${ msg } ` ) )
63- infoMock = jest
64- . spyOn ( core , 'info' )
65- . mockImplementation ( msg => console . log ( `::info::${ msg } ` ) )
66- errorMock = jest
67- . spyOn ( core , 'error' )
68- . mockImplementation ( msg => console . log ( `::error::${ msg } ` ) )
69- getInputMock = jest . spyOn ( core , 'getInput' ) . mockImplementation ( )
70- setFailedMock = jest . spyOn ( core , 'setFailed' ) . mockImplementation ( )
71- setOutputMock = jest . spyOn ( core , 'setOutput' ) . mockImplementation ( )
110+ debugMock . mockImplementation ( msg => console . log ( `::debug::${ msg } ` ) )
111+ infoMock . mockImplementation ( msg => console . log ( `::info::${ msg } ` ) )
112+ errorMock . mockImplementation ( msg => console . log ( `::error::${ msg } ` ) )
72113 } )
73114
74115 it (
@@ -101,7 +142,6 @@ describe('action', () => {
101142 } )
102143
103144 await main . run ( )
104- expect ( runMock ) . toHaveReturned ( )
105145
106146 // Verify that all of the core library functions were called correctly
107147 expect ( debugMock ) . toHaveBeenNthCalledWith ( 1 , 'Get inputs' )
0 commit comments