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: replace node-fetch with native fetch #1763

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
75 changes: 13 additions & 62 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"private": true,
"engines": {
"node": ">=15.0.0",
"node": ">=18.17.0",
"npm": ">=7.0.0"
},
"engineStrict": true,
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"redocly": "bin/cli.js"
},
"engines": {
"node": ">=14.19.0",
"node": ">=18.17.0",
"npm": ">=7.0.0"
},
"engineStrict": true,
Expand Down Expand Up @@ -46,7 +46,6 @@
"glob": "^7.1.6",
"handlebars": "^4.7.6",
"mobx": "^6.0.4",
"node-fetch": "^2.6.1",
"pluralize": "^8.0.0",
"react": "^17.0.0 || ^18.2.0",
"react-dom": "^17.0.0 || ^18.2.0",
Expand Down
16 changes: 10 additions & 6 deletions packages/cli/src/__tests__/commands/push-region.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ import { promptClientToken } from '../../commands/login';
import { ConfigFixture } from '../fixtures/config';

jest.mock('fs');
jest.mock('node-fetch', () => ({
default: jest.fn(() => ({
ok: true,
json: jest.fn().mockResolvedValue({}),
})),
}));
jest.mock('@redocly/openapi-core');
jest.mock('../../commands/login');
jest.mock('../../utils/miscellaneous');
Expand All @@ -22,8 +16,18 @@ describe('push-with-region', () => {
const redoclyClient = require('@redocly/openapi-core').__redoclyClient;
redoclyClient.isAuthorizedWithRedoclyByRegion = jest.fn().mockResolvedValue(false);

afterAll(() => {
jest.restoreAllMocks(); // Restore original fetch after tests
});

beforeAll(() => {
jest.spyOn(process.stdout, 'write').mockImplementation(() => true);
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: jest.fn().mockResolvedValue({}), // Mocked JSON response
} as unknown as Response)
);
});

it('should call login with default domain when region is US', async () => {
Expand Down
16 changes: 10 additions & 6 deletions packages/cli/src/__tests__/commands/push.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ import { ConfigFixture } from '../fixtures/config';
import { yellow } from 'colorette';

jest.mock('fs');
jest.mock('node-fetch', () => ({
default: jest.fn(() => ({
ok: true,
json: jest.fn().mockResolvedValue({}),
})),
}));
jest.mock('@redocly/openapi-core');
jest.mock('../../utils/miscellaneous');

Expand All @@ -22,6 +16,16 @@ describe('push', () => {

beforeEach(() => {
jest.spyOn(process.stdout, 'write').mockImplementation(() => true);
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: jest.fn().mockResolvedValue({}), // Mocked JSON response
} as unknown as Response)
);
});

afterAll(() => {
jest.restoreAllMocks(); // Restore original fetch after tests
});

it('pushes definition', async () => {
Expand Down
14 changes: 9 additions & 5 deletions packages/cli/src/__tests__/fetch-with-timeout.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import AbortController from 'abort-controller';
import fetchWithTimeout from '../utils/fetch-with-timeout';
import nodeFetch from 'node-fetch';
import { getProxyAgent } from '@redocly/openapi-core';
import { HttpsProxyAgent } from 'https-proxy-agent';

jest.mock('node-fetch');
jest.mock('@redocly/openapi-core');

describe('fetchWithTimeout', () => {
Expand All @@ -16,6 +14,12 @@ describe('fetchWithTimeout', () => {

beforeEach(() => {
(getProxyAgent as jest.Mock).mockReturnValueOnce(undefined);
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: jest.fn().mockResolvedValue({}), // Mocked JSON response
} as unknown as Response)
);
});

afterEach(() => {
Expand All @@ -26,7 +30,7 @@ describe('fetchWithTimeout', () => {
await fetchWithTimeout('url', { timeout: 1000 });

expect(global.setTimeout).toHaveBeenCalledTimes(1);
expect(nodeFetch).toHaveBeenCalledWith('url', {
expect(fetch).toHaveBeenCalledWith('url', {
signal: new AbortController().signal,
agent: undefined,
});
Expand All @@ -40,14 +44,14 @@ describe('fetchWithTimeout', () => {

await fetchWithTimeout('url');

expect(nodeFetch).toHaveBeenCalledWith('url', { agent: proxyAgent });
expect(fetch).toHaveBeenCalledWith('url', { agent: proxyAgent });
});

it('should call node-fetch without signal when timeout is not passed', async () => {
await fetchWithTimeout('url');

expect(global.setTimeout).not.toHaveBeenCalled();
expect(nodeFetch).toHaveBeenCalledWith('url', { agent: undefined });
expect(fetch).toHaveBeenCalledWith('url', { agent: undefined });
expect(global.clearTimeout).not.toHaveBeenCalled();
});
});
Loading
Loading