1
1
import * as path from 'node:path' ;
2
2
3
3
import { ElectronVersions , ReleaseInfo } from '@electron/fiddle-core' ;
4
- import { fetch } from 'cross-fetch' ;
5
4
import type { BrowserWindow } from 'electron' ;
6
5
import * as fs from 'fs-extra' ;
7
6
import { mocked } from 'jest-mock' ;
@@ -20,11 +19,8 @@ import { BrowserWindowMock, NodeTypesMock } from '../mocks/mocks';
20
19
import { waitFor } from '../utils' ;
21
20
22
21
jest . mock ( '../../src/main/ipc' ) ;
23
- jest . mock ( 'cross-fetch' ) ;
24
22
jest . unmock ( 'fs-extra' ) ;
25
23
26
- const { Response } = jest . requireActual ( 'cross-fetch' ) ;
27
-
28
24
describe ( 'ElectronTypes' , ( ) => {
29
25
const version = '10.11.12' ;
30
26
const nodeVersion = '16.2.0' ;
@@ -152,13 +148,13 @@ describe('ElectronTypes', () => {
152
148
it ( 'fetches types' , async ( ) => {
153
149
const version = { ...remoteVersion , version : '15.0.0-nightly.20210628' } ;
154
150
const types = 'here are the types' ;
155
- mocked ( fetch ) . mockImplementation (
156
- ( ) =>
157
- new Response ( types , {
158
- status : 200 ,
159
- statusText : 'OK' ,
160
- } ) ,
161
- ) ;
151
+ mocked ( fetch ) . mockResolvedValue ( {
152
+ text : jest . fn ( ) . mockResolvedValue ( types ) ,
153
+ json : jest . fn ( ) . mockImplementation ( async ( ) => JSON . parse ( types ) ) ,
154
+ ok : true ,
155
+ status : 200 ,
156
+ statusText : 'OK' ,
157
+ } as unknown as Response ) ;
162
158
163
159
await expect (
164
160
electronTypes . getElectronTypes ( browserWindow , version ) ,
@@ -175,13 +171,13 @@ describe('ElectronTypes', () => {
175
171
176
172
// setup: fetch and cache a .d.ts that we did not have
177
173
const types = 'here are the types' ;
178
- mocked ( fetch ) . mockImplementation (
179
- ( ) =>
180
- new Response ( types , {
181
- status : 200 ,
182
- statusText : 'OK' ,
183
- } ) ,
184
- ) ;
174
+ mocked ( fetch ) . mockResolvedValue ( {
175
+ text : jest . fn ( ) . mockResolvedValue ( types ) ,
176
+ json : jest . fn ( ) . mockImplementation ( async ( ) => JSON . parse ( types ) ) ,
177
+ ok : true ,
178
+ status : 200 ,
179
+ statusText : 'OK' ,
180
+ } as unknown as Response ) ;
185
181
await expect (
186
182
electronTypes . getElectronTypes ( browserWindow , remoteVersion ) ,
187
183
) . resolves . toEqual ( types ) ;
@@ -203,12 +199,12 @@ describe('ElectronTypes', () => {
203
199
} ) ;
204
200
205
201
it ( 'does not crash if fetch() does not find the package' , async ( ) => {
206
- mocked ( fetch ) . mockResolvedValue (
207
- new Response ( 'Cannot find package' , {
208
- status : 404 ,
209
- statusText : 'Not Found' ,
210
- } ) ,
211
- ) ;
202
+ mocked ( fetch ) . mockResolvedValue ( {
203
+ text : jest . fn ( ) . mockResolvedValue ( 'Cannot find package' ) ,
204
+ ok : false ,
205
+ status : 404 ,
206
+ statusText : 'Not Found' ,
207
+ } as unknown as Response ) ;
212
208
await expect (
213
209
electronTypes . getElectronTypes ( browserWindow , remoteVersion ) ,
214
210
) . resolves . toBe ( undefined ) ;
@@ -218,13 +214,14 @@ describe('ElectronTypes', () => {
218
214
219
215
describe ( 'getNodeTypes' , ( ) => {
220
216
it ( 'fetches types' , async ( ) => {
221
- mocked ( fetch ) . mockImplementation (
222
- ( ) =>
223
- new Response ( JSON . stringify ( { files : nodeTypesData } ) , {
224
- status : 200 ,
225
- statusText : 'OK' ,
226
- } ) ,
227
- ) ;
217
+ const content = JSON . stringify ( { files : nodeTypesData } ) ;
218
+ mocked ( fetch ) . mockResolvedValue ( {
219
+ text : jest . fn ( ) . mockResolvedValue ( content ) ,
220
+ json : jest . fn ( ) . mockImplementation ( async ( ) => JSON . parse ( content ) ) ,
221
+ ok : true ,
222
+ status : 200 ,
223
+ statusText : 'OK' ,
224
+ } as unknown as Response ) ;
228
225
229
226
const version = { ...remoteVersion , version : '15.0.0-nightly.20210628' } ;
230
227
await expect (
@@ -249,12 +246,12 @@ describe('ElectronTypes', () => {
249
246
} ) ;
250
247
251
248
it ( 'does not crash if fetch() does not find the package' , async ( ) => {
252
- mocked ( fetch ) . mockResolvedValue (
253
- new Response ( 'Cannot find package' , {
254
- status : 404 ,
255
- statusText : 'Not Found' ,
256
- } ) ,
257
- ) ;
249
+ mocked ( fetch ) . mockResolvedValue ( {
250
+ text : jest . fn ( ) . mockResolvedValue ( 'Cannot find package' ) ,
251
+ ok : false ,
252
+ status : 404 ,
253
+ statusText : 'Not Found' ,
254
+ } as unknown as Response ) ;
258
255
await expect (
259
256
electronTypes . getNodeTypes ( remoteVersion . version ) ,
260
257
) . resolves . toBe ( undefined ) ;
@@ -276,13 +273,14 @@ describe('ElectronTypes', () => {
276
273
// setup: fetch and cache some types
277
274
expect ( fs . existsSync ( cacheFile ) ) . toBe ( false ) ;
278
275
const types = 'here are the types' ;
279
- mocked ( fetch ) . mockImplementation (
280
- ( ) =>
281
- new Response ( JSON . stringify ( { files : types } ) , {
282
- status : 200 ,
283
- statusText : 'OK' ,
284
- } ) ,
285
- ) ;
276
+ const content = JSON . stringify ( { files : types } ) ;
277
+ mocked ( fetch ) . mockResolvedValue ( {
278
+ text : jest . fn ( ) . mockResolvedValue ( content ) ,
279
+ json : jest . fn ( ) . mockImplementation ( async ( ) => JSON . parse ( content ) ) ,
280
+ ok : true ,
281
+ status : 200 ,
282
+ statusText : 'OK' ,
283
+ } as unknown as Response ) ;
286
284
await electronTypes . getElectronTypes ( browserWindow , remoteVersion ) ;
287
285
} ) ;
288
286
0 commit comments