Skip to content

Commit 3228c9c

Browse files
authored
chore: remove cross-fetch dependency (#1623)
1 parent 1ca9113 commit 3228c9c

File tree

7 files changed

+61
-279
lines changed

7 files changed

+61
-279
lines changed

package.json

-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
"algoliasearch": "^4.12.0",
5252
"classnames": "^2.2.6",
5353
"commander": "^7.1.0",
54-
"cross-fetch": "^3.1.0",
5554
"electron-default-menu": "^1.0.2",
5655
"electron-devtools-installer": "^3.1.1",
5756
"electron-squirrel-startup": "^1.0.0",
@@ -121,7 +120,6 @@
121120
"eslint-plugin-import": "^2.27.5",
122121
"eslint-plugin-prettier": "^5.0.0",
123122
"eslint-plugin-react": "^7.32.2",
124-
"fetch-mock-jest": "^1.5.1",
125123
"fork-ts-checker-webpack-plugin": "^8.0.0",
126124
"husky": "^9.0.11",
127125
"jest": "^29.6.2",

src/main/content.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as path from 'node:path';
22

3-
import fetch from 'cross-fetch';
43
import { IpcMainEvent, app } from 'electron';
54
import * as fs from 'fs-extra';
65

src/main/electron-types.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as path from 'node:path';
22

33
import { ElectronVersions } from '@electron/fiddle-core';
4-
import fetch from 'cross-fetch';
54
import { BrowserWindow, IpcMainEvent, app } from 'electron';
65
import * as fs from 'fs-extra';
76
import watch from 'node-watch';

tests/main/content-spec.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as path from 'node:path';
22

3-
import { Response, fetch } from 'cross-fetch';
43
import { app } from 'electron';
54
import * as fs from 'fs-extra';
65
import { mocked } from 'jest-mock';
@@ -12,7 +11,6 @@ import { EditorValues, MAIN_JS } from '../../src/interfaces';
1211
import { getTemplate, getTestTemplate } from '../../src/main/content';
1312
import { isReleasedMajor } from '../../src/main/versions';
1413

15-
jest.mock('cross-fetch');
1614
jest.unmock('fs-extra');
1715
jest.mock('../../src/main/constants', () => ({
1816
STATIC_DIR: path.join(__dirname, '../../static'),
@@ -21,11 +19,6 @@ jest.mock('../../src/main/versions', () => ({
2119
isReleasedMajor: jest.fn(),
2220
}));
2321

24-
let lastResponse = new Response(null, {
25-
status: 503,
26-
statusText: 'Service Unavailable',
27-
});
28-
2922
// instead of downloading fixtures,
3023
// pull the files from tests/fixtures/templates/
3124
const fetchFromFilesystem = async (url: string) => {
@@ -47,8 +40,12 @@ const fetchFromFilesystem = async (url: string) => {
4740
} catch (err) {
4841
console.log(err);
4942
}
50-
lastResponse = new Response(arrayBuffer, { status, statusText });
51-
return lastResponse;
43+
return {
44+
arrayBuffer: jest.fn().mockResolvedValue(arrayBuffer),
45+
ok: true,
46+
status,
47+
statusText,
48+
} as unknown as Response;
5249
};
5350

5451
describe('content', () => {

tests/main/electron-types-spec.ts

+42-44
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as path from 'node:path';
22

33
import { ElectronVersions, ReleaseInfo } from '@electron/fiddle-core';
4-
import { fetch } from 'cross-fetch';
54
import type { BrowserWindow } from 'electron';
65
import * as fs from 'fs-extra';
76
import { mocked } from 'jest-mock';
@@ -20,11 +19,8 @@ import { BrowserWindowMock, NodeTypesMock } from '../mocks/mocks';
2019
import { waitFor } from '../utils';
2120

2221
jest.mock('../../src/main/ipc');
23-
jest.mock('cross-fetch');
2422
jest.unmock('fs-extra');
2523

26-
const { Response } = jest.requireActual('cross-fetch');
27-
2824
describe('ElectronTypes', () => {
2925
const version = '10.11.12';
3026
const nodeVersion = '16.2.0';
@@ -152,13 +148,13 @@ describe('ElectronTypes', () => {
152148
it('fetches types', async () => {
153149
const version = { ...remoteVersion, version: '15.0.0-nightly.20210628' };
154150
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);
162158

163159
await expect(
164160
electronTypes.getElectronTypes(browserWindow, version),
@@ -175,13 +171,13 @@ describe('ElectronTypes', () => {
175171

176172
// setup: fetch and cache a .d.ts that we did not have
177173
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);
185181
await expect(
186182
electronTypes.getElectronTypes(browserWindow, remoteVersion),
187183
).resolves.toEqual(types);
@@ -203,12 +199,12 @@ describe('ElectronTypes', () => {
203199
});
204200

205201
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);
212208
await expect(
213209
electronTypes.getElectronTypes(browserWindow, remoteVersion),
214210
).resolves.toBe(undefined);
@@ -218,13 +214,14 @@ describe('ElectronTypes', () => {
218214

219215
describe('getNodeTypes', () => {
220216
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);
228225

229226
const version = { ...remoteVersion, version: '15.0.0-nightly.20210628' };
230227
await expect(
@@ -249,12 +246,12 @@ describe('ElectronTypes', () => {
249246
});
250247

251248
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);
258255
await expect(
259256
electronTypes.getNodeTypes(remoteVersion.version),
260257
).resolves.toBe(undefined);
@@ -276,13 +273,14 @@ describe('ElectronTypes', () => {
276273
// setup: fetch and cache some types
277274
expect(fs.existsSync(cacheFile)).toBe(false);
278275
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);
286284
await electronTypes.getElectronTypes(browserWindow, remoteVersion);
287285
});
288286

tests/utils.ts

-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { mocked } from 'jest-mock';
21
import { toJS } from 'mobx';
32

43
import { FiddleEvent } from '../src/interfaces';
@@ -60,13 +59,6 @@ export function resetRendererArch() {
6059
});
6160
}
6261

63-
export function mockFetchOnce(text: string) {
64-
mocked(window.fetch).mockResolvedValueOnce({
65-
text: jest.fn().mockResolvedValue(text),
66-
json: jest.fn().mockImplementation(async () => JSON.parse(text)),
67-
} as unknown as Response);
68-
}
69-
7062
export class FetchMock {
7163
private readonly urls: Map<string, string> = new Map();
7264
public add(url: string, content: string) {

0 commit comments

Comments
 (0)