Skip to content

Commit 21947ea

Browse files
committed
Fix SEA build tests to mock httpRequest
Replace global fetch mocks with httpRequest mocks to match actual implementation. Tests now import from source TypeScript files and properly mock the http-request module.
1 parent 4bedeff commit 21947ea

File tree

1 file changed

+84
-43
lines changed

1 file changed

+84
-43
lines changed

test/registry/sea-build.test.mts

Lines changed: 84 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
22

3+
// Mock the httpRequest module before importing sea-build module.
4+
vi.mock('../../registry/src/lib/http-request', () => {
5+
return {
6+
httpRequest: vi.fn(),
7+
}
8+
})
9+
10+
import { httpRequest as httpRequestActual } from '../../registry/src/lib/http-request'
311
import {
412
getBuildTargets,
513
getDefaultNodeVersion,
614
getLatestCurrentRelease,
7-
} from '../../registry/dist/lib/sea-build.js'
15+
} from '../../registry/src/lib/sea-build'
16+
17+
const httpRequest = httpRequestActual as unknown as ReturnType<typeof vi.fn>
818

919
describe('sea-build module', () => {
1020
let originalEnv: NodeJS.ProcessEnv
1121

1222
beforeEach(() => {
1323
originalEnv = { ...process.env }
24+
vi.clearAllMocks()
1425
})
1526

1627
afterEach(() => {
1728
process.env = originalEnv
18-
vi.unstubAllGlobals()
1929
})
2030

2131
describe('getDefaultNodeVersion', () => {
@@ -28,14 +38,19 @@ describe('sea-build module', () => {
2838
it('should fetch latest when env var not set', async () => {
2939
delete process.env['SOCKET_SEA_NODE_VERSION']
3040

31-
vi.stubGlobal('fetch', async () => ({
41+
httpRequest.mockResolvedValue({
42+
body: Buffer.from(
43+
JSON.stringify([
44+
{ version: 'v24.8.0' },
45+
{ version: 'v23.0.0' },
46+
{ version: 'v22.0.0' },
47+
]),
48+
),
49+
headers: {},
3250
ok: true,
33-
json: async () => [
34-
{ version: 'v24.8.0' },
35-
{ version: 'v23.0.0' },
36-
{ version: 'v22.0.0' },
37-
],
38-
}))
51+
status: 200,
52+
statusText: 'OK',
53+
})
3954

4055
const version = await getDefaultNodeVersion()
4156
expect(version).toBe('24.8.0')
@@ -44,83 +59,109 @@ describe('sea-build module', () => {
4459

4560
describe('getLatestCurrentRelease', () => {
4661
it('should fetch and parse latest even-numbered version', async () => {
47-
vi.stubGlobal('fetch', async () => ({
62+
httpRequest.mockResolvedValue({
63+
body: Buffer.from(
64+
JSON.stringify([
65+
{ version: 'v26.1.0' },
66+
{ version: 'v25.0.0' },
67+
{ version: 'v24.8.0' },
68+
{ version: 'v23.0.0' },
69+
{ version: 'v22.0.0' },
70+
]),
71+
),
72+
headers: {},
4873
ok: true,
49-
json: async () => [
50-
{ version: 'v26.1.0' },
51-
{ version: 'v25.0.0' },
52-
{ version: 'v24.8.0' },
53-
{ version: 'v23.0.0' },
54-
{ version: 'v22.0.0' },
55-
],
56-
}))
74+
status: 200,
75+
statusText: 'OK',
76+
})
5777

5878
const version = await getLatestCurrentRelease()
5979
expect(version).toBe('26.1.0')
6080
})
6181

6282
it('should filter out odd-numbered versions', async () => {
63-
vi.stubGlobal('fetch', async () => ({
83+
httpRequest.mockResolvedValue({
84+
body: Buffer.from(
85+
JSON.stringify([
86+
{ version: 'v25.0.0' },
87+
{ version: 'v24.8.0' },
88+
{ version: 'v23.0.0' },
89+
]),
90+
),
91+
headers: {},
6492
ok: true,
65-
json: async () => [
66-
{ version: 'v25.0.0' },
67-
{ version: 'v24.8.0' },
68-
{ version: 'v23.0.0' },
69-
],
70-
}))
93+
status: 200,
94+
statusText: 'OK',
95+
})
7196

7297
const version = await getLatestCurrentRelease()
7398
expect(version).toBe('24.8.0')
7499
})
75100

76101
it('should filter out versions below v24', async () => {
77-
vi.stubGlobal('fetch', async () => ({
102+
httpRequest.mockResolvedValue({
103+
body: Buffer.from(
104+
JSON.stringify([
105+
{ version: 'v22.0.0' },
106+
{ version: 'v20.0.0' },
107+
{ version: 'v18.0.0' },
108+
]),
109+
),
110+
headers: {},
78111
ok: true,
79-
json: async () => [
80-
{ version: 'v22.0.0' },
81-
{ version: 'v20.0.0' },
82-
{ version: 'v18.0.0' },
83-
],
84-
}))
112+
status: 200,
113+
statusText: 'OK',
114+
})
85115

86116
const version = await getLatestCurrentRelease()
87117
expect(version).toBe('24.8.0')
88118
})
89119

90120
it('should fallback to 24.8.0 when no suitable version found', async () => {
91-
vi.stubGlobal('fetch', async () => ({
121+
httpRequest.mockResolvedValue({
122+
body: Buffer.from(
123+
JSON.stringify([{ version: 'v21.0.0' }, { version: 'v19.0.0' }]),
124+
),
125+
headers: {},
92126
ok: true,
93-
json: async () => [{ version: 'v21.0.0' }, { version: 'v19.0.0' }],
94-
}))
127+
status: 200,
128+
statusText: 'OK',
129+
})
95130

96131
const version = await getLatestCurrentRelease()
97132
expect(version).toBe('24.8.0')
98133
})
99134

100135
it('should throw error on fetch failure', async () => {
101-
vi.stubGlobal('fetch', async () => ({
136+
httpRequest.mockResolvedValue({
137+
body: Buffer.from(''),
138+
headers: {},
102139
ok: false,
140+
status: 404,
103141
statusText: 'Not Found',
104-
}))
142+
})
105143

106144
await expect(getLatestCurrentRelease()).rejects.toThrow('Failed to fetch')
107145
})
108146

109147
it('should throw error on network error', async () => {
110-
vi.stubGlobal('fetch', async () => {
111-
throw new Error('Network error')
112-
})
148+
httpRequest.mockRejectedValue(new Error('Network error'))
113149

114150
await expect(getLatestCurrentRelease()).rejects.toThrow(
115151
'Failed to fetch latest Node.js Current release',
116152
)
117153
})
118154

119155
it('should handle invalid version format', async () => {
120-
vi.stubGlobal('fetch', async () => ({
156+
httpRequest.mockResolvedValue({
157+
body: Buffer.from(
158+
JSON.stringify([{ version: 'invalid' }, { version: 'v24.8.0' }]),
159+
),
160+
headers: {},
121161
ok: true,
122-
json: async () => [{ version: 'invalid' }, { version: 'v24.8.0' }],
123-
}))
162+
status: 200,
163+
statusText: 'OK',
164+
})
124165

125166
const version = await getLatestCurrentRelease()
126167
expect(version).toBe('24.8.0')

0 commit comments

Comments
 (0)