Skip to content

Commit 8d0f924

Browse files
authored
lib: migrate from jest to vitest for ts support (#227)
* lib: migrate from jest to vitest for ts support * ci: update github workflows * test: update vitest run in serial opt
1 parent 52740df commit 8d0f924

17 files changed

Lines changed: 2237 additions & 5085 deletions

.github/actions/cleanup/action.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: 'Clean up test action environment'
2+
description: 'Deletes test branch and optionally test tag from remote'
3+
inputs:
4+
branch:
5+
description: 'Branch name to delete'
6+
required: true
7+
tag:
8+
description: 'Tag name to delete'
9+
required: false
10+
default: ''
11+
runs:
12+
using: composite
13+
steps:
14+
- name: Delete remote branch
15+
shell: bash
16+
run: |
17+
test_branch=$(git ls-remote --heads origin refs/heads/${{ inputs.branch }})
18+
if [[ -n $test_branch ]]; then
19+
echo "test branch ${{ inputs.branch }} existed"
20+
git push -d origin refs/heads/${{ inputs.branch }}
21+
else
22+
echo "test branch ${{ inputs.branch }} does not exist"
23+
fi
24+
25+
- name: Delete remote tag
26+
if: ${{ inputs.tag != '' }}
27+
shell: bash
28+
run: |
29+
test_tag=$(git ls-remote --tags origin refs/tags/${{ inputs.tag }})
30+
if [[ -n $test_tag ]]; then
31+
echo "test tag ${{ inputs.tag }} existed"
32+
git push -d origin refs/tags/${{ inputs.tag }}
33+
else
34+
echo "test tag ${{ inputs.tag }} does not exist"
35+
fi

.github/workflows/check-dist.yml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,15 @@ jobs:
2323
runs-on: ubuntu-latest
2424

2525
steps:
26-
- name: Checkout
27-
uses: actions/checkout@v6
26+
- uses: actions/checkout@v6
2827

29-
- name: Setup Node.js
30-
uses: actions/setup-node@v6
28+
- uses: actions/setup-node@v6
3129
with:
3230
node-version-file: .node-version
3331
cache: npm
3432

35-
- name: Install Dependencies
36-
run: npm ci
37-
38-
- name: Build dist/ Directory
39-
run: npm run bundle
33+
- run: npm ci
34+
- run: npm run bundle
4035

4136
# This will fail the workflow if the `dist/` directory is different than
4237
# expected.

.github/workflows/ci.yml

Lines changed: 39 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,21 @@ concurrency:
1616
cancel-in-progress: ${{ github.ref != 'main' }}
1717

1818
jobs:
19-
test-jest:
20-
name: Jest Tests
19+
test-vitest:
20+
name: Vitest Tests
2121
runs-on: ubuntu-latest
2222

2323
steps:
24-
- name: Checkout
25-
uses: actions/checkout@v6
26-
27-
- name: Setup Node.js
28-
uses: actions/setup-node@v6
24+
- uses: actions/checkout@v6
25+
- uses: actions/setup-node@v6
2926
with:
3027
node-version-file: .node-version
3128
cache: npm
3229

33-
- name: Install Dependencies
34-
run: npm ci
35-
36-
- name: Check Format
37-
run: npm run format:check
38-
39-
- name: Lint
40-
run: npm run lint
41-
42-
- name: Test
43-
run: npm run ci-test
30+
- run: npm ci
31+
- run: npm run format:check
32+
- run: npm run lint
33+
- run: npm test
4434

4535
test-action-basic:
4636
name: GitHub Actions Test (Basic)
@@ -51,8 +41,7 @@ jobs:
5141
BRANCH: test-action-basic
5242

5343
steps:
54-
- name: Checkout
55-
uses: actions/checkout@v6
44+
- uses: actions/checkout@v6
5645

5746
- name: Test Action
5847
id: test-action
@@ -68,16 +57,10 @@ jobs:
6857
run: echo "${{ steps.test-action.outputs.commit-sha }}"
6958

7059
- name: Clean up test action environment
71-
shell: bash
7260
if: always()
73-
run: |
74-
test_branch=$(git ls-remote --heads origin refs/heads/$BRANCH)
75-
if [[ -n $test_branch ]]; then
76-
echo "test branch $BRANCH existed"
77-
git push -d origin refs/heads/$BRANCH
78-
else
79-
echo "test branch $BRANCH does not exist"
80-
fi
61+
uses: ./.github/actions/cleanup
62+
with:
63+
branch: ${{ env.BRANCH }}
8164

8265
test-action-branch:
8366
name: GitHub Actions Test (Custom Branch)
@@ -89,8 +72,7 @@ jobs:
8972
BRANCH: test-action-branch
9073

9174
steps:
92-
- name: Checkout
93-
uses: actions/checkout@v6
75+
- uses: actions/checkout@v6
9476

9577
- name: generate dummy content
9678
shell: bash
@@ -113,16 +95,10 @@ jobs:
11395
run: echo "${{ steps.test-action.outputs.commit-sha }}"
11496

11597
- name: Clean up test action environment
116-
shell: bash
11798
if: always()
118-
run: |
119-
test_branch=$(git ls-remote --heads origin refs/heads/$BRANCH)
120-
if [[ -n $test_branch ]]; then
121-
echo "test branch $BRANCH existed"
122-
git push -d origin refs/heads/$BRANCH
123-
else
124-
echo "test branch $BRANCH does not exist"
125-
fi
99+
uses: ./.github/actions/cleanup
100+
with:
101+
branch: ${{ env.BRANCH }}
126102

127103
test-action-workspace:
128104
name: GitHub Actions Test (Workspace)
@@ -134,8 +110,7 @@ jobs:
134110
BRANCH: test-action-workspace
135111

136112
steps:
137-
- name: Checkout
138-
uses: actions/checkout@v6
113+
- uses: actions/checkout@v6
139114

140115
- name: generate dummy content
141116
shell: bash
@@ -159,16 +134,10 @@ jobs:
159134
run: echo "${{ steps.test-action.outputs.commit-sha }}"
160135

161136
- name: Clean up test action environment
162-
shell: bash
163137
if: always()
164-
run: |
165-
test_branch=$(git ls-remote --heads origin refs/heads/$BRANCH)
166-
if [[ -n $test_branch ]]; then
167-
echo "test branch $BRANCH existed"
168-
git push -d origin refs/heads/$BRANCH
169-
else
170-
echo "test branch $BRANCH does not exist"
171-
fi
138+
uses: ./.github/actions/cleanup
139+
with:
140+
branch: ${{ env.BRANCH }}
172141

173142
test-action-glob:
174143
name: GitHub Actions Test (Glob)
@@ -180,8 +149,7 @@ jobs:
180149
BRANCH: test-action-glob
181150

182151
steps:
183-
- name: Checkout
184-
uses: actions/checkout@v6
152+
- uses: actions/checkout@v6
185153

186154
- name: generate dummy content
187155
shell: bash
@@ -204,16 +172,10 @@ jobs:
204172
run: echo "${{ steps.test-action.outputs.commit-sha }}"
205173

206174
- name: Clean up test action environment
207-
shell: bash
208175
if: always()
209-
run: |
210-
test_branch=$(git ls-remote --heads origin refs/heads/$BRANCH)
211-
if [[ -n $test_branch ]]; then
212-
echo "test branch $BRANCH existed"
213-
git push -d origin refs/heads/$BRANCH
214-
else
215-
echo "test branch $BRANCH does not exist"
216-
fi
176+
uses: ./.github/actions/cleanup
177+
with:
178+
branch: ${{ env.BRANCH }}
217179

218180
test-action-tag:
219181
name: GitHub Actions Test (Tag)
@@ -226,8 +188,7 @@ jobs:
226188
BRANCH: test-action-tag
227189

228190
steps:
229-
- name: Checkout
230-
uses: actions/checkout@v6
191+
- uses: actions/checkout@v6
231192

232193
- name: Test Action
233194
id: test-action
@@ -244,23 +205,11 @@ jobs:
244205
echo "[tag] ${{ steps.test-action.outputs.tag }}"
245206
246207
- name: Clean up test action environment
247-
shell: bash
248208
if: always()
249-
run: |
250-
test_branch=$(git ls-remote --heads origin refs/heads/$BRANCH)
251-
if [[ -n $test_branch ]]; then
252-
echo "test branch $BRANCH existed"
253-
git push -d origin refs/heads/$BRANCH
254-
else
255-
echo "test branch $BRANCH does not exist"
256-
fi
257-
test_tag=$(git ls-remote --tags origin refs/tags/$TAG)
258-
if [[ -n $test_tag ]]; then
259-
echo "test tag $TAG existed"
260-
git push -d origin refs/tags/$TAG
261-
else
262-
echo "test tag $TAG does not exist"
263-
fi
209+
uses: ./.github/actions/cleanup
210+
with:
211+
branch: ${{ env.BRANCH }}
212+
tag: ${{ env.TAG }}
264213

265214
test-action-file-tag:
266215
name: GitHub Actions Test (File & Tag)
@@ -273,9 +222,7 @@ jobs:
273222
TAG: 4.5.6
274223

275224
steps:
276-
- name: Checkout
277-
id: checkout
278-
uses: actions/checkout@v6
225+
- uses: actions/checkout@v6
279226

280227
- name: generate dummy content
281228
shell: bash
@@ -301,23 +248,11 @@ jobs:
301248
echo "[tag] ${{ steps.test-action.outputs.tag }}"
302249
303250
- name: Clean up test action environment
304-
shell: bash
305251
if: always()
306-
run: |
307-
test_branch=$(git ls-remote --heads origin refs/heads/$BRANCH)
308-
if [[ -n $test_branch ]]; then
309-
echo "test branch $BRANCH existed"
310-
git push -d origin refs/heads/$BRANCH
311-
else
312-
echo "test branch $BRANCH does not exist"
313-
fi
314-
test_tag=$(git ls-remote --tags origin refs/tags/$TAG)
315-
if [[ -n $test_tag ]]; then
316-
echo "test tag $TAG existed"
317-
git push -d origin refs/tags/$TAG
318-
else
319-
echo "test tag $TAG does not exist"
320-
fi
252+
uses: ./.github/actions/cleanup
253+
with:
254+
branch: ${{ env.BRANCH }}
255+
tag: ${{ env.TAG }}
321256

322257
test-action-no-file-changes-tag:
323258
name: GitHub Actions Test (No File Changes & Tag)
@@ -330,8 +265,7 @@ jobs:
330265
TAG: 7.8.9
331266

332267
steps:
333-
- name: Checkout
334-
uses: actions/checkout@v6
268+
- uses: actions/checkout@v6
335269

336270
- name: Test Action
337271
id: test-action
@@ -350,20 +284,8 @@ jobs:
350284
echo "[tag] ${{ steps.test-action.outputs.tag }}"
351285
352286
- name: Clean up test action environment
353-
shell: bash
354287
if: always()
355-
run: |
356-
test_branch=$(git ls-remote --heads origin refs/heads/$BRANCH)
357-
if [[ -n $test_branch ]]; then
358-
echo "test branch $BRANCH existed"
359-
git push -d origin refs/heads/$BRANCH
360-
else
361-
echo "test branch $BRANCH does not exist"
362-
fi
363-
test_tag=$(git ls-remote --tags origin refs/tags/$TAG)
364-
if [[ -n $test_tag ]]; then
365-
echo "test tag $TAG existed"
366-
git push -d origin refs/tags/$TAG
367-
else
368-
echo "test tag $TAG does not exist"
369-
fi
288+
uses: ./.github/actions/cleanup
289+
with:
290+
branch: ${{ env.BRANCH }}
291+
tag: ${{ env.TAG }}

.github/workflows/coverage.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@ jobs:
1313

1414
steps:
1515
- uses: actions/checkout@v6
16-
1716
- uses: actions/setup-node@v6
1817
with:
1918
node-version-file: .node-version
2019
cache: npm
2120

2221
- run: npm ci
23-
24-
- name: Run tests
25-
run: npx jest --coverage
22+
- run: npm test -- --coverage
2623

2724
- name: Upload results to Codecov
2825
uses: codecov/codecov-action@v6

__tests__/blob.test.ts

Lines changed: 8 additions & 8 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,9 +50,9 @@ describe('Blob', () => {
5050

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

5757
const chunks: Buffer[] = []
5858
for await (const chunk of blob.streamable) {
@@ -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(

0 commit comments

Comments
 (0)