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'
13+
14+ // Import mocks from fixtures
15+ import * as core from '../__fixtures__/core.js'
1516
1617// Some expected string
1718const mdCoverageTable = `| Total | Frontend | Backend | SimCode | Templates | Compilation | Simulation | Verification |
1819| --- | --- | --- | --- | --- | --- | --- | --- |
1920| 2 | 2 | 2 | 2 | 2 | 2 | 2 | 1 |`
2021
21- // Mock the action's main function
22- const runMock = jest . spyOn ( main , 'run' )
2322const modelicaFile = path . resolve (
2423 path . join ( 'examples' , 'MyLibrary' , 'package.mo' )
2524)
2625const referenceFilesDir = path . resolve ( path . join ( 'examples' , 'ReferenceFiles' ) )
2726
2827// 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
28+ jest . unstable_mockModule ( '@actions/core' , ( ) => core )
3529
3630// Mock @actions /artifact and @actions /github
37- jest . mock ( '@actions/artifact' )
38- jest . mock ( '@actions/github' )
31+ const uploadArtifactMock = jest
32+ . fn < ( ) => Promise < { size : number ; id : number } > > ( )
33+ . mockResolvedValue ( { size : 0 , id : 0 } )
34+
35+ jest . unstable_mockModule ( '@actions/artifact' , ( ) => ( {
36+ DefaultArtifactClient : jest . fn ( ) . mockImplementation ( ( ) => ( {
37+ uploadArtifact : uploadArtifactMock
38+ } ) )
39+ } ) )
40+
41+ jest . unstable_mockModule ( '@actions/github' , ( ) => ( {
42+ context : {
43+ repo : { owner : 'test' , repo : 'test' } ,
44+ runId : 123
45+ }
46+ } ) )
47+
48+ // Dynamic imports after mocking
49+ const main = await import ( '../src/main' )
50+ const { getMSYS } = await import ( '../src/get-msys' )
3951
4052// Set GitHub summary file
4153const gitHubStepSummaryFile = path . resolve (
@@ -56,26 +68,19 @@ describe('action', () => {
5668 fs . writeFileSync ( gitHubStepSummaryFile , '' , { flag : 'w' } )
5769
5870 jest . clearAllMocks ( )
71+ // Reset summary buffer
72+ core . resetSummaryBuffer ( )
5973
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 ( )
74+ core . debug . mockImplementation ( msg => console . log ( `::debug::${ msg } ` ) )
75+ core . info . mockImplementation ( msg => console . log ( `::info::${ msg } ` ) )
76+ core . error . mockImplementation ( msg => console . log ( `::error::${ msg } ` ) )
7277 } )
7378
7479 it (
7580 'Run action' ,
7681 async ( ) => {
7782 // Set the action's inputs as return values from core.getInput()
78- getInputMock . mockImplementation ( ( name : string ) : string => {
83+ core . getInput . mockImplementation ( ( name : string ) : string => {
7984 switch ( name ) {
8085 case 'library' :
8186 return 'MyLibrary'
@@ -101,68 +106,67 @@ describe('action', () => {
101106 } )
102107
103108 await main . run ( )
104- expect ( runMock ) . toHaveReturned ( )
105109
106110 // Verify that all of the core library functions were called correctly
107- expect ( debugMock ) . toHaveBeenNthCalledWith ( 1 , 'Get inputs' )
108- expect ( debugMock ) . toHaveBeenNthCalledWith (
111+ expect ( core . debug ) . toHaveBeenNthCalledWith ( 1 , 'Get inputs' )
112+ expect ( core . debug ) . toHaveBeenNthCalledWith (
109113 2 ,
110114 'clone OpenModelicaLibraryTesting'
111115 )
112- expect ( debugMock ) . toHaveBeenNthCalledWith ( 4 , 'Generating configuration' )
113- expect ( debugMock ) . toHaveBeenNthCalledWith (
116+ expect ( core . debug ) . toHaveBeenNthCalledWith ( 4 , 'Generating configuration' )
117+ expect ( core . debug ) . toHaveBeenNthCalledWith (
114118 5 ,
115119 'Running python test.py --verbose --branch=master --noclean' +
116120 ` ${ os . platform ( ) === 'win32' ? `--msysEnvironment=${ getMSYS ( ) } ` : '' } ` +
117121 ` ${ path . join ( 'configs' , 'conf-MyLibrary.json' ) } `
118122 )
119- expect ( debugMock ) . toHaveBeenNthCalledWith (
123+ expect ( core . debug ) . toHaveBeenNthCalledWith (
120124 7 ,
121125 'Running python report.py --branch=master' +
122126 ` ${ path . join ( 'configs' , 'conf-MyLibrary.json' ) } `
123127 )
124- expect ( debugMock ) . toHaveBeenNthCalledWith ( 9 , 'Write summary' )
125- expect ( debugMock ) . toHaveBeenNthCalledWith ( 10 , 'Set outputs' )
126- expect ( debugMock ) . toHaveBeenNthCalledWith ( 11 , 'Collect HTML outputs' )
127- expect ( debugMock ) . toHaveBeenNthCalledWith ( 12 , 'Upload artifacts' )
128- expect ( debugMock ) . toHaveBeenCalledTimes ( 12 )
128+ expect ( core . debug ) . toHaveBeenNthCalledWith ( 9 , 'Write summary' )
129+ expect ( core . debug ) . toHaveBeenNthCalledWith ( 10 , 'Set outputs' )
130+ expect ( core . debug ) . toHaveBeenNthCalledWith ( 11 , 'Collect HTML outputs' )
131+ expect ( core . debug ) . toHaveBeenNthCalledWith ( 12 , 'Upload artifacts' )
132+ expect ( core . debug ) . toHaveBeenCalledTimes ( 12 )
129133
130- expect ( setOutputMock ) . toHaveBeenNthCalledWith (
134+ expect ( core . setOutput ) . toHaveBeenNthCalledWith (
131135 1 ,
132136 'simulation-tests-passing' ,
133137 true
134138 )
135- expect ( setOutputMock ) . toHaveBeenNthCalledWith (
139+ expect ( core . setOutput ) . toHaveBeenNthCalledWith (
136140 2 ,
137141 'n-simulation-passing' ,
138142 2
139143 )
140- expect ( setOutputMock ) . toHaveBeenNthCalledWith (
144+ expect ( core . setOutput ) . toHaveBeenNthCalledWith (
141145 3 ,
142146 'verification-tests-passing' ,
143147 false
144148 )
145- expect ( setOutputMock ) . toHaveBeenNthCalledWith (
149+ expect ( core . setOutput ) . toHaveBeenNthCalledWith (
146150 4 ,
147151 'n-verification-passing' ,
148152 1
149153 )
150- expect ( setOutputMock ) . toHaveBeenCalledTimes ( 4 )
154+ expect ( core . setOutput ) . toHaveBeenCalledTimes ( 4 )
151155
152- expect ( infoMock ) . toHaveBeenNthCalledWith (
156+ expect ( core . info ) . toHaveBeenNthCalledWith (
153157 2 ,
154158 `simulation-tests-passing: true`
155159 )
156- expect ( infoMock ) . toHaveBeenNthCalledWith ( 3 , `n-simulation-passing: 2` )
157- expect ( infoMock ) . toHaveBeenNthCalledWith (
160+ expect ( core . info ) . toHaveBeenNthCalledWith ( 3 , `n-simulation-passing: 2` )
161+ expect ( core . info ) . toHaveBeenNthCalledWith (
158162 4 ,
159163 `verification-tests-passing: false`
160164 )
161- expect ( infoMock ) . toHaveBeenNthCalledWith ( 5 , `n-verification-passing: 1` )
162- expect ( infoMock ) . toHaveBeenCalledTimes ( 5 )
165+ expect ( core . info ) . toHaveBeenNthCalledWith ( 5 , `n-verification-passing: 1` )
166+ expect ( core . info ) . toHaveBeenCalledTimes ( 5 )
163167
164- expect ( errorMock ) . not . toHaveBeenCalled ( )
165- expect ( setFailedMock ) . not . toHaveBeenCalled ( )
168+ expect ( core . error ) . not . toHaveBeenCalled ( )
169+ expect ( core . setFailed ) . not . toHaveBeenCalled ( )
166170
167171 // Verify summary file
168172 const summaryContent = fs . readFileSync ( gitHubStepSummaryFile , 'utf-8' )
0 commit comments