11import * as core from '@actions/core'
22import * as fs from 'fs'
33import { run } from '../src/main'
4+ import {
5+ vi ,
6+ describe ,
7+ test ,
8+ expect ,
9+ beforeEach ,
10+ type MockInstance
11+ } from 'vitest'
412
5- jest . mock ( '@actions/core' )
6- jest . mock ( 'fs' , ( ) => ( {
13+ vi . mock ( '@actions/core' )
14+ vi . mock ( 'fs' , ( ) => ( {
715 promises : {
8- access : jest . fn ( ) ,
9- readFile : jest . fn ( )
16+ access : vi . fn ( ) ,
17+ readFile : vi . fn ( )
1018 } ,
1119 constants : {
1220 O_RDONLY : 0
1321 }
1422} ) )
1523
24+ // Mock types
25+ let mockGetInput : MockInstance < typeof core . getInput >
26+
1627/**
1728 * Test suite for the GitHub Action's main functionality.
1829 * Tests the complete validation workflow including:
@@ -23,7 +34,9 @@ jest.mock('fs', () => ({
2334 */
2435describe ( 'run' , ( ) => {
2536 beforeEach ( ( ) => {
26- jest . clearAllMocks ( )
37+ vi . clearAllMocks ( )
38+ mockGetInput = vi . spyOn ( core , 'getInput' )
39+ vi . spyOn ( core , 'setFailed' ) . mockImplementation ( ( ) => { } )
2740 } )
2841
2942 /**
@@ -45,7 +58,7 @@ describe('run', () => {
4558 }
4659
4760 // Mock inputs and file operations
48- jest . spyOn ( core , 'getInput' ) . mockImplementation ( name => {
61+ mockGetInput . mockImplementation ( name => {
4962 switch ( name ) {
5063 case 'required-extensions' :
5164 return 'required-ext'
@@ -55,8 +68,8 @@ describe('run', () => {
5568 return ''
5669 }
5770 } )
58- ; ( fs . promises . access as jest . Mock ) . mockResolvedValue ( undefined )
59- ; ( fs . promises . readFile as jest . Mock ) . mockResolvedValue (
71+ vi . spyOn ( fs . promises , 'access' ) . mockResolvedValue ( undefined )
72+ vi . spyOn ( fs . promises , 'readFile' ) . mockResolvedValue (
6073 JSON . stringify ( mockContent )
6174 )
6275
@@ -70,11 +83,11 @@ describe('run', () => {
7083 */
7184 describe ( 'file handling errors' , ( ) => {
7285 test ( 'should throw error when devcontainer.json is not found' , async ( ) => {
73- jest . spyOn ( core , 'getInput' ) . mockImplementation ( name => {
86+ mockGetInput . mockImplementation ( name => {
7487 if ( name === 'required-extensions' ) return 'ext1'
7588 return ''
7689 } )
77- ; ( fs . promises . access as jest . Mock ) . mockRejectedValue (
90+ vi . spyOn ( fs . promises , 'access' ) . mockRejectedValue (
7891 new Error ( 'File not found' )
7992 )
8093
@@ -85,9 +98,9 @@ describe('run', () => {
8598 } )
8699
87100 test ( 'should throw error when JSON is invalid' , async ( ) => {
88- jest . spyOn ( core , 'getInput' ) . mockReturnValue ( 'ext1' )
89- ; ( fs . promises . access as jest . Mock ) . mockResolvedValue ( undefined )
90- ; ( fs . promises . readFile as jest . Mock ) . mockResolvedValue ( 'invalid json' )
101+ mockGetInput . mockReturnValue ( 'ext1' )
102+ vi . spyOn ( fs . promises , 'access' ) . mockResolvedValue ( undefined )
103+ vi . spyOn ( fs . promises , 'readFile' ) . mockResolvedValue ( 'invalid json' )
91104
92105 await run ( )
93106 expect ( core . setFailed ) . toHaveBeenCalledWith (
@@ -109,9 +122,9 @@ describe('run', () => {
109122 }
110123 }
111124
112- jest . spyOn ( core , 'getInput' ) . mockReturnValue ( 'ext1' )
113- ; ( fs . promises . access as jest . Mock ) . mockResolvedValue ( undefined )
114- ; ( fs . promises . readFile as jest . Mock ) . mockResolvedValue (
125+ mockGetInput . mockReturnValue ( 'ext1' )
126+ vi . spyOn ( fs . promises , 'access' ) . mockResolvedValue ( undefined )
127+ vi . spyOn ( fs . promises , 'readFile' ) . mockResolvedValue (
115128 JSON . stringify ( invalidContent )
116129 )
117130
@@ -130,12 +143,12 @@ describe('run', () => {
130143 }
131144 }
132145
133- jest . spyOn ( core , 'getInput' ) . mockImplementation ( name => {
146+ mockGetInput . mockImplementation ( name => {
134147 if ( name === 'required-extensions' ) return 'ext1,ext2'
135148 return ''
136149 } )
137- ; ( fs . promises . access as jest . Mock ) . mockResolvedValue ( undefined )
138- ; ( fs . promises . readFile as jest . Mock ) . mockResolvedValue (
150+ vi . spyOn ( fs . promises , 'access' ) . mockResolvedValue ( undefined )
151+ vi . spyOn ( fs . promises , 'readFile' ) . mockResolvedValue (
139152 JSON . stringify ( content )
140153 )
141154
@@ -162,13 +175,13 @@ describe('run', () => {
162175 }
163176 }
164177
165- jest . spyOn ( core , 'getInput' ) . mockImplementation ( name => {
178+ mockGetInput . mockImplementation ( name => {
166179 if ( name === 'required-extensions' ) return 'ext1'
167180 if ( name === 'required-features' ) return 'feature1,feature2'
168181 return ''
169182 } )
170- ; ( fs . promises . access as jest . Mock ) . mockResolvedValue ( undefined )
171- ; ( fs . promises . readFile as jest . Mock ) . mockResolvedValue (
183+ vi . spyOn ( fs . promises , 'access' ) . mockResolvedValue ( undefined )
184+ vi . spyOn ( fs . promises , 'readFile' ) . mockResolvedValue (
172185 JSON . stringify ( content )
173186 )
174187
@@ -187,13 +200,13 @@ describe('run', () => {
187200 }
188201 }
189202
190- jest . spyOn ( core , 'getInput' ) . mockImplementation ( name => {
203+ mockGetInput . mockImplementation ( name => {
191204 if ( name === 'required-extensions' ) return 'ext1'
192205 if ( name === 'required-features' ) return ''
193206 return ''
194207 } )
195- ; ( fs . promises . access as jest . Mock ) . mockResolvedValue ( undefined )
196- ; ( fs . promises . readFile as jest . Mock ) . mockResolvedValue (
208+ vi . spyOn ( fs . promises , 'access' ) . mockResolvedValue ( undefined )
209+ vi . spyOn ( fs . promises , 'readFile' ) . mockResolvedValue (
197210 JSON . stringify ( content )
198211 )
199212
@@ -219,13 +232,13 @@ describe('run', () => {
219232 }
220233 }
221234
222- jest . spyOn ( core , 'getInput' ) . mockImplementation ( name => {
235+ mockGetInput . mockImplementation ( name => {
223236 if ( name === 'required-extensions' ) return 'ext1'
224237 if ( name === 'validate-tasks' ) return 'true'
225238 return ''
226239 } )
227- ; ( fs . promises . access as jest . Mock ) . mockResolvedValue ( undefined )
228- ; ( fs . promises . readFile as jest . Mock ) . mockResolvedValue (
240+ vi . spyOn ( fs . promises , 'access' ) . mockResolvedValue ( undefined )
241+ vi . spyOn ( fs . promises , 'readFile' ) . mockResolvedValue (
229242 JSON . stringify ( content )
230243 )
231244
@@ -250,7 +263,7 @@ describe('run', () => {
250263 }
251264 } as Error
252265
253- jest . spyOn ( core , 'getInput' ) . mockImplementation ( ( ) => {
266+ mockGetInput . mockImplementation ( ( ) => {
254267 throw errorLike
255268 } )
256269
@@ -259,9 +272,9 @@ describe('run', () => {
259272 } )
260273
261274 test ( 'should handle string errors correctly' , async ( ) => {
262- jest . spyOn ( core , 'getInput' ) . mockReturnValue ( 'ext1' )
263- ; ( fs . promises . access as jest . Mock ) . mockResolvedValue ( undefined )
264- ; ( fs . promises . readFile as jest . Mock ) . mockRejectedValue (
275+ mockGetInput . mockReturnValue ( 'ext1' )
276+ vi . spyOn ( fs . promises , 'access' ) . mockResolvedValue ( undefined )
277+ vi . spyOn ( fs . promises , 'readFile' ) . mockRejectedValue (
265278 'String error message'
266279 )
267280
0 commit comments