Skip to content

Commit 30a4e22

Browse files
committed
lib: migrate from jest to vitest for ts support
1 parent 52740df commit 30a4e22

13 files changed

Lines changed: 2025 additions & 5001 deletions

__tests__/blob.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import fs from 'node:fs'
33
import { join } from 'node:path'
44
import { Buffer } from 'node:buffer'
55
import { Readable, PassThrough } from 'node:stream'
6-
import { describe, jest, beforeEach, it, expect } from '@jest/globals'
6+
import { describe, vi, beforeEach, it, expect } from 'vitest'
77
import { Blob, getBlob } from '../src/blob'
88
import * as cwd from '../src/utils/cwd'
99

1010
describe('Blob', () => {
1111
beforeEach(() => {
12-
jest.spyOn(core, 'debug').mockReturnValue()
13-
jest.spyOn(cwd, 'getCwd').mockReturnValue(__dirname)
12+
vi.spyOn(core, 'debug').mockReturnValue()
13+
vi.spyOn(cwd, 'getCwd').mockReturnValue(__dirname)
1414
})
1515

1616
it('path', () => {
@@ -50,7 +50,7 @@ describe('Blob', () => {
5050

5151
it('file exists', async () => {
5252
const blob = new Blob('/my_stream.txt')
53-
jest
53+
vi
5454
.spyOn(blob, 'streamable', 'get')
5555
.mockReturnValue(Readable.from('Hello World'))
5656

@@ -75,7 +75,7 @@ describe('Blob', () => {
7575
it('file with string', async () => {
7676
const blob = getBlob('fixtures/error.txt')
7777
const mockStream = new PassThrough()
78-
jest.spyOn(blob, 'streamable', 'get').mockReturnValue(mockStream)
78+
vi.spyOn(blob, 'streamable', 'get').mockReturnValue(mockStream)
7979

8080
const loadPromise = blob.load()
8181
mockStream.emit('data', 'string data')
@@ -89,7 +89,7 @@ describe('Blob', () => {
8989
it('stream with error', async () => {
9090
const blob = getBlob('fixtures/error.txt')
9191
const mockStream = new PassThrough()
92-
jest.spyOn(blob, 'streamable', 'get').mockReturnValue(mockStream)
92+
vi.spyOn(blob, 'streamable', 'get').mockReturnValue(mockStream)
9393

9494
blob.load()
9595
expect(() => mockStream.emit('error', new Error('stream error'))).toThrow(

__tests__/git.test.ts

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as core from '@actions/core'
22
import * as exec from '@actions/exec'
3-
import { describe, jest, beforeEach, afterAll, it, expect } from '@jest/globals'
3+
import { describe, vi, beforeEach, it, expect } from 'vitest'
44
import * as cwd from '../src/utils/cwd'
55
import {
66
addFileChanges,
@@ -11,12 +11,12 @@ import {
1111

1212
describe('Git CLI', () => {
1313
beforeEach(() => {
14-
jest.restoreAllMocks()
14+
vi.restoreAllMocks()
1515
})
1616

1717
describe('git checkout', () => {
1818
it('should create new branch', async () => {
19-
const execMock = jest.spyOn(exec, 'exec').mockResolvedValue(0)
19+
const execMock = vi.spyOn(exec, 'exec').mockResolvedValue(0)
2020

2121
await switchBranch('new-branch')
2222
expect(execMock).toHaveBeenCalledWith(
@@ -29,7 +29,7 @@ describe('Git CLI', () => {
2929
})
3030

3131
it('should log debug', async () => {
32-
const execMock = jest
32+
const execMock = vi
3333
.spyOn(exec, 'exec')
3434
.mockImplementationOnce(async (cmd, args, options) => {
3535
const io = options?.listeners?.stdline
@@ -40,15 +40,15 @@ describe('Git CLI', () => {
4040
return 0
4141
})
4242

43-
const debugMock = jest.spyOn(core, 'debug').mockReturnValue()
43+
const debugMock = vi.spyOn(core, 'debug').mockReturnValue()
4444

4545
await switchBranch('new-branch')
4646
expect(execMock).toHaveBeenCalled()
4747
expect(debugMock).toHaveBeenCalledWith('git checkout debug log message')
4848
})
4949

5050
it('should log warning', async () => {
51-
const execMock = jest
51+
const execMock = vi
5252
.spyOn(exec, 'exec')
5353
.mockImplementationOnce(async (cmd, args, options) => {
5454
const io = options?.listeners?.errline
@@ -59,7 +59,7 @@ describe('Git CLI', () => {
5959
return 0
6060
})
6161

62-
const warningMock = jest.spyOn(core, 'warning').mockReturnValue()
62+
const warningMock = vi.spyOn(core, 'warning').mockReturnValue()
6363

6464
await switchBranch('new-branch')
6565
expect(execMock).toHaveBeenCalled()
@@ -69,7 +69,7 @@ describe('Git CLI', () => {
6969
})
7070

7171
it('should log error', async () => {
72-
const execMock = jest
72+
const execMock = vi
7373
.spyOn(exec, 'exec')
7474
.mockImplementationOnce(async (cmd, args, options) => {
7575
const io = options?.listeners?.errline
@@ -80,7 +80,7 @@ describe('Git CLI', () => {
8080
return 0
8181
})
8282

83-
const errorMock = jest.spyOn(core, 'error').mockReturnValue()
83+
const errorMock = vi.spyOn(core, 'error').mockReturnValue()
8484

8585
await switchBranch('new-branch')
8686
expect(execMock).toHaveBeenCalled()
@@ -90,11 +90,11 @@ describe('Git CLI', () => {
9090

9191
describe('git push', () => {
9292
beforeEach(() => {
93-
jest.spyOn(core, 'getBooleanInput').mockReturnValue(false)
93+
vi.spyOn(core, 'getBooleanInput').mockReturnValue(false)
9494
})
9595

9696
it('should push new branch', async () => {
97-
const execMock = jest.spyOn(exec, 'exec').mockResolvedValue(0)
97+
const execMock = vi.spyOn(exec, 'exec').mockResolvedValue(0)
9898

9999
await pushCurrentBranch()
100100
expect(execMock).toHaveBeenCalledWith(
@@ -107,8 +107,8 @@ describe('Git CLI', () => {
107107
})
108108

109109
it('should force push new branch', async () => {
110-
const execMock = jest.spyOn(exec, 'exec').mockResolvedValue(0)
111-
const getInput = jest.spyOn(core, 'getBooleanInput').mockReturnValue(true)
110+
const execMock = vi.spyOn(exec, 'exec').mockResolvedValue(0)
111+
const getInput = vi.spyOn(core, 'getBooleanInput').mockReturnValue(true)
112112

113113
await pushCurrentBranch()
114114
expect(execMock).toHaveBeenCalled()
@@ -123,7 +123,7 @@ describe('Git CLI', () => {
123123
})
124124

125125
it('should log debug', async () => {
126-
const execMock = jest
126+
const execMock = vi
127127
.spyOn(exec, 'exec')
128128
.mockImplementationOnce(async (cmd, args, options) => {
129129
const io = options?.listeners?.stdline
@@ -134,15 +134,15 @@ describe('Git CLI', () => {
134134
return 0
135135
})
136136

137-
const debugMock = jest.spyOn(core, 'debug').mockReturnValue()
137+
const debugMock = vi.spyOn(core, 'debug').mockReturnValue()
138138

139139
await pushCurrentBranch()
140140
expect(execMock).toHaveBeenCalled()
141141
expect(debugMock).toHaveBeenCalledWith('git push debug log message')
142142
})
143143

144144
it('should log warning', async () => {
145-
const execMock = jest
145+
const execMock = vi
146146
.spyOn(exec, 'exec')
147147
.mockImplementationOnce(async (cmd, args, options) => {
148148
const io = options?.listeners?.errline
@@ -153,15 +153,15 @@ describe('Git CLI', () => {
153153
return 0
154154
})
155155

156-
const warningMock = jest.spyOn(core, 'warning').mockReturnValue()
156+
const warningMock = vi.spyOn(core, 'warning').mockReturnValue()
157157

158158
await pushCurrentBranch()
159159
expect(execMock).toHaveBeenCalled()
160160
expect(warningMock).toHaveBeenCalledWith('git push warning log message')
161161
})
162162

163163
it('should log error', async () => {
164-
const execMock = jest
164+
const execMock = vi
165165
.spyOn(exec, 'exec')
166166
.mockImplementationOnce(async (cmd, args, options) => {
167167
const io = options?.listeners?.errline
@@ -172,7 +172,7 @@ describe('Git CLI', () => {
172172
return 0
173173
})
174174

175-
const errorMock = jest.spyOn(core, 'error').mockReturnValue()
175+
const errorMock = vi.spyOn(core, 'error').mockReturnValue()
176176

177177
await pushCurrentBranch()
178178
expect(execMock).toHaveBeenCalled()
@@ -182,11 +182,11 @@ describe('Git CLI', () => {
182182

183183
describe('git add', () => {
184184
beforeEach(() => {
185-
jest.spyOn(cwd, 'getWorkspace').mockReturnValue('/test-workspace')
185+
vi.spyOn(cwd, 'getWorkspace').mockReturnValue('/test-workspace')
186186
})
187187

188188
it('should ensure file paths are within curent working directory', async () => {
189-
const execMock = jest.spyOn(exec, 'exec').mockResolvedValue(0)
189+
const execMock = vi.spyOn(exec, 'exec').mockResolvedValue(0)
190190

191191
await addFileChanges(['*.ts', '~/.bashrc'])
192192
expect(execMock).toHaveBeenCalledWith(
@@ -199,7 +199,7 @@ describe('Git CLI', () => {
199199
})
200200

201201
it('should log debug', async () => {
202-
const execMock = jest
202+
const execMock = vi
203203
.spyOn(exec, 'exec')
204204
.mockImplementationOnce(async (cmd, args, options) => {
205205
const io = options?.listeners?.stdline
@@ -210,15 +210,15 @@ describe('Git CLI', () => {
210210
return 0
211211
})
212212

213-
const debugMock = jest.spyOn(core, 'debug').mockReturnValue()
213+
const debugMock = vi.spyOn(core, 'debug').mockReturnValue()
214214

215215
await addFileChanges(['*.ts'])
216216
expect(execMock).toHaveBeenCalled()
217217
expect(debugMock).toHaveBeenCalledWith('git add debug log message')
218218
})
219219

220220
it('should log warning', async () => {
221-
const execMock = jest
221+
const execMock = vi
222222
.spyOn(exec, 'exec')
223223
.mockImplementationOnce(async (cmd, args, options) => {
224224
const io = options?.listeners?.errline
@@ -229,15 +229,15 @@ describe('Git CLI', () => {
229229
return 0
230230
})
231231

232-
const warningMock = jest.spyOn(core, 'warning').mockReturnValue()
232+
const warningMock = vi.spyOn(core, 'warning').mockReturnValue()
233233

234234
await addFileChanges(['*.ts'])
235235
expect(execMock).toHaveBeenCalled()
236236
expect(warningMock).toHaveBeenCalledWith('git add warning log message')
237237
})
238238

239239
it('should log error', async () => {
240-
const execMock = jest
240+
const execMock = vi
241241
.spyOn(exec, 'exec')
242242
.mockImplementationOnce(async (cmd, args, options) => {
243243
const io = options?.listeners?.errline
@@ -248,7 +248,7 @@ describe('Git CLI', () => {
248248
return 0
249249
})
250250

251-
const errorMock = jest.spyOn(core, 'error').mockReturnValue()
251+
const errorMock = vi.spyOn(core, 'error').mockReturnValue()
252252

253253
await addFileChanges(['*.ts'])
254254
expect(execMock).toHaveBeenCalled()
@@ -271,7 +271,7 @@ describe('Git CLI', () => {
271271
]
272272

273273
it('should parse ouput into file changes', async () => {
274-
const execMock = jest
274+
const execMock = vi
275275
.spyOn(exec, 'exec')
276276
.mockImplementationOnce(async (cmd, args, options) => {
277277
const io = options?.listeners?.stdline
@@ -321,7 +321,7 @@ describe('Git CLI', () => {
321321
})
322322

323323
it('should log warning', async () => {
324-
const execMock = jest
324+
const execMock = vi
325325
.spyOn(exec, 'exec')
326326
.mockImplementationOnce(async (cmd, args, options) => {
327327
const io = options?.listeners?.errline
@@ -332,15 +332,15 @@ describe('Git CLI', () => {
332332
return 0
333333
})
334334

335-
const warningMock = jest.spyOn(core, 'warning').mockReturnValue()
335+
const warningMock = vi.spyOn(core, 'warning').mockReturnValue()
336336

337337
await getFileChanges()
338338
expect(execMock).toHaveBeenCalled()
339339
expect(warningMock).toHaveBeenCalledWith('git status warning log message')
340340
})
341341

342342
it('should log error', async () => {
343-
const execMock = jest
343+
const execMock = vi
344344
.spyOn(exec, 'exec')
345345
.mockImplementationOnce(async (cmd, args, options) => {
346346
const io = options?.listeners?.errline
@@ -351,7 +351,7 @@ describe('Git CLI', () => {
351351
return 0
352352
})
353353

354-
const errorMock = jest.spyOn(core, 'error').mockReturnValue()
354+
const errorMock = vi.spyOn(core, 'error').mockReturnValue()
355355

356356
await getFileChanges()
357357
expect(execMock).toHaveBeenCalled()

__tests__/github/client.test.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
import {
22
describe,
3-
jest,
3+
vi,
44
afterEach,
55
beforeEach,
66
it,
77
expect,
8-
} from '@jest/globals'
8+
} from 'vitest'
99
import { graphqlClient } from '../../src/github/client'
1010

1111
describe('GitHub Client', () => {
12-
let replacedEnv: jest.Replaced<typeof process.env> | undefined
13-
1412
beforeEach(() => {
15-
jest.restoreAllMocks()
16-
replacedEnv = jest.replaceProperty(
17-
process,
18-
'env',
19-
Object.assign(process.env, { GH_TOKEN: 'fake-token' })
20-
)
13+
vi.restoreAllMocks()
14+
vi.stubEnv('GH_TOKEN', 'fake-token')
2115
})
2216

2317
afterEach(() => {
24-
replacedEnv?.restore()
18+
vi.unstubAllEnvs()
2519
})
2620

2721
it('should set authorization header from GH_TOKEN', async () => {

0 commit comments

Comments
 (0)