Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove cross-fetch dependency #1623

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
"algoliasearch": "^4.12.0",
"classnames": "^2.2.6",
"commander": "^7.1.0",
"cross-fetch": "^3.1.0",
"electron-default-menu": "^1.0.2",
"electron-devtools-installer": "^3.1.1",
"electron-squirrel-startup": "^1.0.0",
Expand Down Expand Up @@ -121,7 +120,6 @@
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-react": "^7.32.2",
"fetch-mock-jest": "^1.5.1",
"fork-ts-checker-webpack-plugin": "^8.0.0",
"husky": "^9.0.11",
"jest": "^29.6.2",
Expand Down
1 change: 0 additions & 1 deletion src/main/content.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as path from 'node:path';

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

Expand Down
1 change: 0 additions & 1 deletion src/main/electron-types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as path from 'node:path';

import { ElectronVersions } from '@electron/fiddle-core';
import fetch from 'cross-fetch';
import { BrowserWindow, IpcMainEvent, app } from 'electron';
import * as fs from 'fs-extra';
import watch from 'node-watch';
Expand Down
15 changes: 6 additions & 9 deletions tests/main/content-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as path from 'node:path';

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

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

let lastResponse = new Response(null, {
status: 503,
statusText: 'Service Unavailable',
});

// instead of downloading fixtures,
// pull the files from tests/fixtures/templates/
const fetchFromFilesystem = async (url: string) => {
Expand All @@ -47,8 +40,12 @@ const fetchFromFilesystem = async (url: string) => {
} catch (err) {
console.log(err);
}
lastResponse = new Response(arrayBuffer, { status, statusText });
return lastResponse;
return {
arrayBuffer: jest.fn().mockResolvedValue(arrayBuffer),
ok: true,
status,
statusText,
} as unknown as Response;
};

describe('content', () => {
Expand Down
86 changes: 42 additions & 44 deletions tests/main/electron-types-spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as path from 'node:path';

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

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

const { Response } = jest.requireActual('cross-fetch');

describe('ElectronTypes', () => {
const version = '10.11.12';
const nodeVersion = '16.2.0';
Expand Down Expand Up @@ -152,13 +148,13 @@ describe('ElectronTypes', () => {
it('fetches types', async () => {
const version = { ...remoteVersion, version: '15.0.0-nightly.20210628' };
const types = 'here are the types';
mocked(fetch).mockImplementation(
() =>
new Response(types, {
status: 200,
statusText: 'OK',
}),
);
mocked(fetch).mockResolvedValue({
text: jest.fn().mockResolvedValue(types),
json: jest.fn().mockImplementation(async () => JSON.parse(types)),
ok: true,
status: 200,
statusText: 'OK',
} as unknown as Response);

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

// setup: fetch and cache a .d.ts that we did not have
const types = 'here are the types';
mocked(fetch).mockImplementation(
() =>
new Response(types, {
status: 200,
statusText: 'OK',
}),
);
mocked(fetch).mockResolvedValue({
text: jest.fn().mockResolvedValue(types),
json: jest.fn().mockImplementation(async () => JSON.parse(types)),
ok: true,
status: 200,
statusText: 'OK',
} as unknown as Response);
await expect(
electronTypes.getElectronTypes(browserWindow, remoteVersion),
).resolves.toEqual(types);
Expand All @@ -203,12 +199,12 @@ describe('ElectronTypes', () => {
});

it('does not crash if fetch() does not find the package', async () => {
mocked(fetch).mockResolvedValue(
new Response('Cannot find package', {
status: 404,
statusText: 'Not Found',
}),
);
mocked(fetch).mockResolvedValue({
text: jest.fn().mockResolvedValue('Cannot find package'),
ok: false,
status: 404,
statusText: 'Not Found',
} as unknown as Response);
await expect(
electronTypes.getElectronTypes(browserWindow, remoteVersion),
).resolves.toBe(undefined);
Expand All @@ -218,13 +214,14 @@ describe('ElectronTypes', () => {

describe('getNodeTypes', () => {
it('fetches types', async () => {
mocked(fetch).mockImplementation(
() =>
new Response(JSON.stringify({ files: nodeTypesData }), {
status: 200,
statusText: 'OK',
}),
);
const content = JSON.stringify({ files: nodeTypesData });
mocked(fetch).mockResolvedValue({
text: jest.fn().mockResolvedValue(content),
json: jest.fn().mockImplementation(async () => JSON.parse(content)),
ok: true,
status: 200,
statusText: 'OK',
} as unknown as Response);

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

it('does not crash if fetch() does not find the package', async () => {
mocked(fetch).mockResolvedValue(
new Response('Cannot find package', {
status: 404,
statusText: 'Not Found',
}),
);
mocked(fetch).mockResolvedValue({
text: jest.fn().mockResolvedValue('Cannot find package'),
ok: false,
status: 404,
statusText: 'Not Found',
} as unknown as Response);
await expect(
electronTypes.getNodeTypes(remoteVersion.version),
).resolves.toBe(undefined);
Expand All @@ -276,13 +273,14 @@ describe('ElectronTypes', () => {
// setup: fetch and cache some types
expect(fs.existsSync(cacheFile)).toBe(false);
const types = 'here are the types';
mocked(fetch).mockImplementation(
() =>
new Response(JSON.stringify({ files: types }), {
status: 200,
statusText: 'OK',
}),
);
const content = JSON.stringify({ files: types });
mocked(fetch).mockResolvedValue({
text: jest.fn().mockResolvedValue(content),
json: jest.fn().mockImplementation(async () => JSON.parse(content)),
ok: true,
status: 200,
statusText: 'OK',
} as unknown as Response);
await electronTypes.getElectronTypes(browserWindow, remoteVersion);
});

Expand Down
8 changes: 0 additions & 8 deletions tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { mocked } from 'jest-mock';
import { toJS } from 'mobx';

import { FiddleEvent } from '../src/interfaces';
Expand Down Expand Up @@ -60,13 +59,6 @@ export function resetRendererArch() {
});
}

export function mockFetchOnce(text: string) {
mocked(window.fetch).mockResolvedValueOnce({
text: jest.fn().mockResolvedValue(text),
json: jest.fn().mockImplementation(async () => JSON.parse(text)),
} as unknown as Response);
}

export class FetchMock {
private readonly urls: Map<string, string> = new Map();
public add(url: string, content: string) {
Expand Down
Loading