Skip to content

Commit

Permalink
test: don't mock got
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed Jul 15, 2023
1 parent 69d71ef commit aceeef9
Showing 1 changed file with 5 additions and 41 deletions.
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

0 comments on commit aceeef9

Please sign in to comment.