Skip to content

test(core): avoid mocking got #5585

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

Merged
merged 1 commit into from
Jul 18, 2023
Merged
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
46 changes: 5 additions & 41 deletions packages/yarnpkg-core/tests/httpUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,8 @@
import {Configuration, Plugin} from '@yarnpkg/core';
import {npath} from '@yarnpkg/fslib';
import got from 'got';

import * as httpUtils from '../sources/httpUtils';
import {Method} from '../sources/httpUtils';

jest.mock(`got`, () => ({
extend: jest.fn(),
}));

const mockExtend = got.extend as jest.Mock;
const mockClient = jest.fn();
import {Configuration, Plugin, httpUtils} from '@yarnpkg/core';
import {npath} from '@yarnpkg/fslib';

describe(`httpUtils`, () => {
beforeEach(() => {
mockExtend.mockReset();
mockExtend.mockReturnValue(mockClient);
});

describe(`request`, () => {
it(`executes a request to the given target`, async () => {
// Arrange
const target = `https://my/fake/target`;
const body = {fake: `body`};
const configuration = Configuration.create(npath.toPortablePath(`.`));
const expectedResponse = {};
mockClient.mockReturnValue(expectedResponse);

// Act
const response = await httpUtils.request(target, body, {configuration});

// Assert
expect(mockExtend.mock.calls.length).toBe(1);
expect(mockClient.mock.calls.length).toBe(1);
expect(mockClient.mock.calls[0][0].href).toBe(target);
expect(response).toEqual(expectedResponse);
});

it(`executes a request to the given target using any registered wrapNetworkRequest plugins`, async () => {
// Arrange
const target = `https://my/fake/target`;
Expand All @@ -45,20 +11,19 @@ describe(`httpUtils`, () => {
const {plugins, mockWrapNetworkRequest} = getPluginsWithMockWrapNetworkRequestPlugin();

const configuration = Configuration.create(npath.toPortablePath(`.`), plugins);
const expectedResponse = {};
mockClient.mockReturnValue(expectedResponse);
mockWrapNetworkRequest.mockReturnValue(() => {});

const headers = {};
const jsonRequest = true;
const jsonResponse = true;
const method = Method.PUT;
const method = httpUtils.Method.PUT;

// Act
await httpUtils.request(target, body, {configuration, headers, jsonRequest, jsonResponse, method});

// Assert
expect(mockWrapNetworkRequest.mock.calls.length).toBe(1);
// mockWrapNetworkRequest.mock.calls[0][0] is supplied implicitly by Configuration.reduceHooks, tested elsewhere presumedly
// mockWrapNetworkRequest.mock.calls[0][0] is supplied implicitly by Configuration.reduceHooks, tested elsewhere presumably
const hookArgumentResult = mockWrapNetworkRequest.mock.calls[0][1];
expect(hookArgumentResult.target).toBe(target);
expect(hookArgumentResult.body).toBe(body);
Expand All @@ -72,7 +37,6 @@ describe(`httpUtils`, () => {

function getPluginsWithMockWrapNetworkRequestPlugin() {
const mockWrapNetworkRequest = jest.fn();
mockWrapNetworkRequest.mockImplementation(async () => async () => {});
const plugins = new Map<string, Plugin<any>>();
plugins.set(`fakeWrapNetworkRequestPlugin`, {
hooks: {
Expand Down