Skip to content

Commit 402733e

Browse files
committed
refactor: migrate from Jest to Vitest for testing framework
- Updated package.json to replace Jest with Vitest for testing commands. - Modified main.js to check for the new environment variable for Vitest. - Added vitest.config.js to configure Vitest with coverage settings and test environment. - Updated dependencies to include Vitest and its coverage provider.
1 parent 4c21973 commit 402733e

45 files changed

Lines changed: 6273 additions & 8778 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.babelrc

Lines changed: 0 additions & 9 deletions
This file was deleted.

.eslintrc.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
{
22
"env": {
33
"es2022": true,
4-
"jest": true,
54
"node": true
65
},
76
"extends": "eslint:recommended",
87
"globals": {
98
"Atomics": "readonly",
10-
"SharedArrayBuffer": "readonly"
9+
"SharedArrayBuffer": "readonly",
10+
"vi": "readonly",
11+
"describe": "readonly",
12+
"test": "readonly",
13+
"expect": "readonly",
14+
"beforeEach": "readonly",
15+
"afterEach": "readonly"
1116
},
1217
"parserOptions": {
1318
"ecmaVersion": 2022,

__tests__/functions/actions-status.test.js

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
import * as core from '@actions/core'
2-
import {
3-
jest,
4-
expect,
5-
describe,
6-
test,
7-
beforeEach,
8-
afterEach
9-
} from '@jest/globals'
2+
import {vi, expect, describe, test, beforeEach, afterEach} from 'vitest'
103
import {actionStatus} from '../../src/functions/action-status.js'
114
import {truncateCommentBody} from '../../src/functions/truncate-comment-body.js'
125
import {API_HEADERS} from '../../src/functions/api-headers.js'
136

147
var context
158
var octokit
169
beforeEach(() => {
17-
jest.clearAllMocks()
18-
19-
jest.spyOn(core, 'debug').mockImplementation(() => {})
20-
jest.spyOn(core, 'warning').mockImplementation(() => {})
10+
vi.clearAllMocks()
2111

2212
process.env.GITHUB_SERVER_URL = 'https://github.com'
2313
process.env.GITHUB_RUN_ID = '12345'
@@ -40,15 +30,15 @@ beforeEach(() => {
4030
octokit = {
4131
rest: {
4232
reactions: {
43-
createForIssueComment: jest.fn().mockReturnValueOnce({
33+
createForIssueComment: vi.fn().mockReturnValueOnce({
4434
data: {}
4535
}),
46-
deleteForIssueComment: jest.fn().mockReturnValueOnce({
36+
deleteForIssueComment: vi.fn().mockReturnValueOnce({
4737
data: {}
4838
})
4939
},
5040
issues: {
51-
createComment: jest.fn().mockReturnValueOnce({
41+
createComment: vi.fn().mockReturnValueOnce({
5242
data: {}
5343
})
5444
}

__tests__/functions/admin.test.js

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
import {isAdmin} from '../../src/functions/admin.js'
2-
import {
3-
jest,
4-
expect,
5-
describe,
6-
test,
7-
beforeEach,
8-
afterEach
9-
} from '@jest/globals'
2+
import {vi, expect, describe, test, beforeEach, afterEach} from 'vitest'
103
import {COLORS} from '../../src/functions/colors.js'
114
import * as github from '@actions/github'
125
import * as core from '@actions/core'
136

14-
const debugMock = jest.spyOn(core, 'debug').mockImplementation(() => {})
15-
const warningMock = jest.spyOn(core, 'warning').mockImplementation(() => {})
16-
// const infoMock = jest.spyOn(core, 'info').mockImplementation(() => {})
7+
const debugMock = vi.spyOn(core, 'debug')
8+
const warningMock = vi.spyOn(core, 'warning')
179

1810
class NotFoundError extends Error {
1911
constructor(message) {
@@ -32,8 +24,7 @@ class WildError extends Error {
3224
var context
3325
var octokit
3426
beforeEach(() => {
35-
jest.clearAllMocks()
36-
jest.spyOn(core, 'info').mockImplementation(() => {})
27+
vi.clearAllMocks()
3728
process.env.INPUT_ADMINS_PAT = 'faketoken'
3829
process.env.INPUT_ADMINS =
3930
'MoNaLiSa,@lisamona,octoawesome/octo-awEsome-team,bad$user'
@@ -43,24 +34,24 @@ beforeEach(() => {
4334
}
4435

4536
octokit = {
46-
request: jest.fn().mockReturnValueOnce({
37+
request: vi.fn().mockReturnValueOnce({
4738
status: 204
4839
}),
4940
rest: {
5041
orgs: {
51-
get: jest.fn().mockReturnValueOnce({
42+
get: vi.fn().mockReturnValueOnce({
5243
data: {id: '12345'}
5344
})
5445
},
5546
teams: {
56-
getByName: jest.fn().mockReturnValueOnce({
47+
getByName: vi.fn().mockReturnValueOnce({
5748
data: {id: '567890'}
5849
})
5950
}
6051
}
6152
}
6253

63-
jest.spyOn(github, 'getOctokit').mockImplementation(() => {
54+
vi.spyOn(github, 'getOctokit').mockImplementation(() => {
6455
return octokit
6556
})
6657
})
@@ -125,11 +116,11 @@ test('runs isAdmin checks for an org team and finds a valid user', async () => {
125116

126117
// This only handles the global failure case of any 404 in the admin.js file
127118
test('runs isAdmin checks for an org team and does not find the org', async () => {
128-
jest.spyOn(github, 'getOctokit').mockImplementation(() => {
119+
vi.spyOn(github, 'getOctokit').mockImplementation(() => {
129120
return {
130121
rest: {
131122
orgs: {
132-
get: jest
123+
get: vi
133124
.fn()
134125
.mockRejectedValueOnce(
135126
new NotFoundError('Reference does not exist')
@@ -147,16 +138,16 @@ test('runs isAdmin checks for an org team and does not find the org', async () =
147138

148139
// This only handles the global failure case of any 404 in the admin.js file
149140
test('runs isAdmin checks for an org team and does not find the team', async () => {
150-
jest.spyOn(github, 'getOctokit').mockImplementation(() => {
141+
vi.spyOn(github, 'getOctokit').mockImplementation(() => {
151142
return {
152143
rest: {
153144
orgs: {
154-
get: jest.fn().mockReturnValueOnce({
145+
get: vi.fn().mockReturnValueOnce({
155146
data: {id: '12345'}
156147
})
157148
},
158149
teams: {
159-
getByName: jest
150+
getByName: vi
160151
.fn()
161152
.mockRejectedValueOnce(
162153
new NotFoundError('Reference does not exist')
@@ -174,19 +165,19 @@ test('runs isAdmin checks for an org team and does not find the team', async ()
174165

175166
// This test correctly tests if a user is a member of a team or not. If they are in a team a 204 is returned. If they are not a 404 is returned like in this test example
176167
test('runs isAdmin checks for an org team and does not find the user in the team', async () => {
177-
jest.spyOn(github, 'getOctokit').mockImplementation(() => {
168+
vi.spyOn(github, 'getOctokit').mockImplementation(() => {
178169
return {
179-
request: jest
170+
request: vi
180171
.fn()
181172
.mockRejectedValueOnce(new NotFoundError('Reference does not exist')),
182173
rest: {
183174
orgs: {
184-
get: jest.fn().mockReturnValueOnce({
175+
get: vi.fn().mockReturnValueOnce({
185176
data: {id: '12345'}
186177
})
187178
},
188179
teams: {
189-
getByName: jest.fn().mockReturnValueOnce({
180+
getByName: vi.fn().mockReturnValueOnce({
190181
data: {id: '567890'}
191182
})
192183
}
@@ -201,19 +192,19 @@ test('runs isAdmin checks for an org team and does not find the user in the team
201192
})
202193

203194
test('runs isAdmin checks for an org team and an unexpected status code is received from the request method with octokit', async () => {
204-
jest.spyOn(github, 'getOctokit').mockImplementation(() => {
195+
vi.spyOn(github, 'getOctokit').mockImplementation(() => {
205196
return {
206-
request: jest.fn().mockReturnValueOnce({
197+
request: vi.fn().mockReturnValueOnce({
207198
status: 500
208199
}),
209200
rest: {
210201
orgs: {
211-
get: jest.fn().mockReturnValueOnce({
202+
get: vi.fn().mockReturnValueOnce({
212203
data: {id: '12345'}
213204
})
214205
},
215206
teams: {
216-
getByName: jest.fn().mockReturnValueOnce({
207+
getByName: vi.fn().mockReturnValueOnce({
217208
data: {id: '567890'}
218209
})
219210
}
@@ -229,19 +220,19 @@ test('runs isAdmin checks for an org team and an unexpected status code is recei
229220
})
230221

231222
test('runs isAdmin checks for an org team and an unexpected error is thrown from any API call', async () => {
232-
jest.spyOn(github, 'getOctokit').mockImplementation(() => {
223+
vi.spyOn(github, 'getOctokit').mockImplementation(() => {
233224
return {
234-
request: jest
225+
request: vi
235226
.fn()
236227
.mockRejectedValueOnce(new WildError('something went boom')),
237228
rest: {
238229
orgs: {
239-
get: jest.fn().mockReturnValueOnce({
230+
get: vi.fn().mockReturnValueOnce({
240231
data: {id: '12345'}
241232
})
242233
},
243234
teams: {
244-
getByName: jest.fn().mockReturnValueOnce({
235+
getByName: vi.fn().mockReturnValueOnce({
245236
data: {id: '567890'}
246237
})
247238
}

__tests__/functions/branch-ruleset-checks.test.js

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
import {branchRulesetChecks} from '../../src/functions/branch-ruleset-checks.js'
2-
import {
3-
jest,
4-
expect,
5-
describe,
6-
test,
7-
beforeEach,
8-
afterEach
9-
} from '@jest/globals'
2+
import {vi, expect, describe, test, beforeEach, afterEach} from 'vitest'
103
import * as core from '@actions/core'
114
import {COLORS} from '../../src/functions/colors.js'
125
import {SUGGESTED_RULESETS} from '../../src/functions/suggested-rulesets.js'
136
import {ERROR} from '../../src/functions/templates/error.js'
147

8+
const debugMock = vi.spyOn(core, 'debug')
9+
const infoMock = vi.spyOn(core, 'info')
10+
const warningMock = vi.spyOn(core, 'warning')
11+
1512
var context
1613
var octokit
1714
var data
1815
var rulesets
1916

20-
const debugMock = jest.spyOn(core, 'debug').mockImplementation(() => {})
21-
const warningMock = jest.spyOn(core, 'warning').mockImplementation(() => {})
22-
const infoMock = jest.spyOn(core, 'info').mockImplementation(() => {})
23-
2417
class ForbiddenError extends Error {
2518
constructor(message) {
2619
super(message)
@@ -29,10 +22,7 @@ class ForbiddenError extends Error {
2922
}
3023

3124
beforeEach(() => {
32-
jest.spyOn(core, 'info').mockImplementation(() => {})
33-
jest.spyOn(core, 'debug').mockImplementation(() => {})
34-
jest.spyOn(core, 'warning').mockImplementation(() => {})
35-
jest.clearAllMocks()
25+
vi.clearAllMocks()
3626

3727
data = {
3828
branch: 'main'
@@ -87,7 +77,7 @@ beforeEach(() => {
8777
octokit = {
8878
rest: {
8979
repos: {
90-
getBranchRules: jest.fn().mockReturnValueOnce({data: rulesets})
80+
getBranchRules: vi.fn().mockReturnValueOnce({data: rulesets})
9181
}
9282
}
9383
}
@@ -97,7 +87,7 @@ test('finds that no branch protections or rulesets are defined', async () => {
9787
octokit = {
9888
rest: {
9989
repos: {
100-
getBranchRules: jest.fn().mockReturnValueOnce({data: []})
90+
getBranchRules: vi.fn().mockReturnValueOnce({data: []})
10191
}
10292
}
10393
}
@@ -127,7 +117,7 @@ test('finds that the branch ruleset is missing the deletion rule', async () => {
127117
octokit = {
128118
rest: {
129119
repos: {
130-
getBranchRules: jest.fn().mockReturnValueOnce({data: rulesets})
120+
getBranchRules: vi.fn().mockReturnValueOnce({data: rulesets})
131121
}
132122
}
133123
}
@@ -158,7 +148,7 @@ test('finds that the branch ruleset is missing the dismiss_stale_reviews_on_push
158148
octokit = {
159149
rest: {
160150
repos: {
161-
getBranchRules: jest.fn().mockReturnValueOnce({data: rulesets})
151+
getBranchRules: vi.fn().mockReturnValueOnce({data: rulesets})
162152
}
163153
}
164154
}
@@ -183,7 +173,7 @@ test('finds that all suggested branch rulesets are defined', async () => {
183173
octokit = {
184174
rest: {
185175
repos: {
186-
getBranchRules: jest.fn().mockReturnValueOnce({data: rulesets})
176+
getBranchRules: vi.fn().mockReturnValueOnce({data: rulesets})
187177
}
188178
}
189179
}
@@ -222,7 +212,7 @@ test('finds that all suggested branch rulesets are defined but required reviews
222212
octokit = {
223213
rest: {
224214
repos: {
225-
getBranchRules: jest.fn().mockReturnValueOnce({data: rulesets})
215+
getBranchRules: vi.fn().mockReturnValueOnce({data: rulesets})
226216
}
227217
}
228218
}
@@ -260,7 +250,7 @@ test('should still pass even with many required reviewers', async () => {
260250
octokit = {
261251
rest: {
262252
repos: {
263-
getBranchRules: jest.fn().mockReturnValueOnce({data: rulesets})
253+
getBranchRules: vi.fn().mockReturnValueOnce({data: rulesets})
264254
}
265255
}
266256
}
@@ -279,7 +269,7 @@ test('fails due to a 403 from the GitHub API due to a repository being private o
279269
octokit = {
280270
rest: {
281271
repos: {
282-
getBranchRules: jest
272+
getBranchRules: vi
283273
.fn()
284274
.mockRejectedValueOnce(
285275
new ForbiddenError(ERROR.messages.upgrade_or_public.message)
@@ -301,7 +291,7 @@ test('fails due to an unknown 403 from the GitHub API', async () => {
301291
octokit = {
302292
rest: {
303293
repos: {
304-
getBranchRules: jest
294+
getBranchRules: vi
305295
.fn()
306296
.mockRejectedValueOnce(new ForbiddenError(errorMessage))
307297
}

__tests__/functions/check-input.test.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
import {checkInput} from '../../src/functions/check-input.js'
2-
import {
3-
jest,
4-
expect,
5-
describe,
6-
test,
7-
beforeEach,
8-
afterEach
9-
} from '@jest/globals'
2+
import {vi, expect, describe, test, beforeEach, afterEach} from 'vitest'
103

114
test('checks an input an finds that it is valid', async () => {
125
expect(checkInput('production')).toStrictEqual('production')

0 commit comments

Comments
 (0)