Skip to content

Commit

Permalink
test(wrappednodefetch, octokit/require.ts): adding fetch method and c…
Browse files Browse the repository at this point in the history
…reateExecutionContext

Resolving the failed test cases by using fetch from node-fetch and createExecutionContext

test#354

Signed-off-by: Hermione Dadheech <[email protected]>
  • Loading branch information
Hermione2408 committed Mar 14, 2023
1 parent 4cb13bf commit 6cdf1d6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
3 changes: 2 additions & 1 deletion integrations/octokit/require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import Hook from "require-in-the-middle";
import mixin from "merge-descriptors";
import fetch, { Headers, Response, ResponseInit } from "node-fetch";
import { getExecutionContext } from "../../src/context";
import { getExecutionContext,createExecutionContext } from "../../src/context";
import { Readable } from "stream";
import { ProcessDep, stringToBinary } from "../../src/util";
import { putMocks } from "../../mock/utils";
Expand Down Expand Up @@ -207,6 +207,7 @@ export function wrappedNodeFetch(fetchFunc: Function) {
);
return fetchFunc.apply(this, [url, options]);
}
createExecutionContext(ctx);
return resp;
}
return mixin(wrappedFetch, fetchFunc, false);
Expand Down
29 changes: 14 additions & 15 deletions test/wrappedNodeFetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
import { wrappedNodeFetch } from '../integrations/octokit/require';
import { Response } from 'node-fetch';
import fetch from 'node-fetch';
import { createExecutionContext, getExecutionContext} from '../src/context';
import { HTTP } from '../src/keploy';

describe('wrappedNodeFetch', () => {
it('should call fetch function with correct arguments in record mode', async () => {
const mockFetch = jest.fn().mockResolvedValueOnce(new Response());
const ctx = {
mode: 'record',
testId: 'testId',
mocks: [],
deps: [],
};
createExecutionContext(ctx)
const wrappedFetch = (wrappedNodeFetch(mockFetch) as any).bind({ fetch: mockFetch });
const url = 'http://example.com';
const wrappedFetch = (wrappedNodeFetch(fetch) as any).bind({ fetch });
const url = 'https://api.keploy.io/healthz';
const options = {
method: 'GET',
};

const response = await wrappedFetch(url, options);
const updatedctx= getExecutionContext().context;
const mocks=updatedctx.mocks.length;
const deps=updatedctx.deps.length;
const responseBody = await response.text();
const recordedOutput = updatedctx.mocks[0].Spec.Res.Body;
expect(mockFetch).toHaveBeenCalledWith(url, options);
expect(response).toBeInstanceOf(Response);
expect(mocks).toBeGreaterThan(0);
expect(deps).toBeGreaterThan(0);
expect(response).toHaveProperty('body');
expect(responseBody).toEqual(recordedOutput);
});

it('should return mocked response in test mode', async () => {
it('should return mocked response in test mode', async () => {
const mockResponse = new Response('mocked response');
const mockFetch = jest.fn().mockResolvedValue(mockResponse);
const ctx = {
mode: 'test',
testId: 'testId',
Expand All @@ -47,12 +44,12 @@ describe('wrappedNodeFetch', () => {
Spec: {
Metadata: {
name: 'node-fetch',
url: 'http://example.com',
url: 'https://api.keploy.io/healthz',
options: { method: 'GET' },
type: 'HTTP_CLIENT',
},
Req: {
URL: 'http://example.com',
URL: 'https://api.keploy.io/healthz',
Body: '',
Header: {},
Method: 'GET',
Expand All @@ -70,23 +67,25 @@ describe('wrappedNodeFetch', () => {
};
createExecutionContext(ctx)

const wrappedFetch = (wrappedNodeFetch(mockFetch) as any).bind({ fetch: mockFetch });
const url = 'http://example.com';
const wrappedFetch = (wrappedNodeFetch(fetch) as any).bind({ fetch });
const url = 'https://api.keploy.io/healthz';
const options = {
method: 'GET',
};
const response = await wrappedFetch(url, options);
const updatedctx= getExecutionContext().context;
expect(response).toEqual(mockResponse);
const mocks=updatedctx.mocks.length();
expect(response.status).toEqual(mockResponse.status);
expect(response.statusText).toEqual(mockResponse.statusText);

const mocks=updatedctx.mocks.length;
expect(mocks).toBe(0);
});

it('should return undefined if execution context is not present in record mode', async () => {
const mockFetch = jest.fn().mockResolvedValue(new Response());
const consoleSpy = jest.spyOn(console, 'error').mockImplementation();
const wrappedFetch = (wrappedNodeFetch(mockFetch) as any).bind({ fetch: mockFetch });
const url = 'http://example.com';
const url = 'https://api.keploy.io/healthz';
const options = {
method: 'GET',
};
Expand All @@ -106,7 +105,7 @@ describe('wrappedNodeFetch', () => {
createExecutionContext(ctx)

const wrappedFetch = (wrappedNodeFetch(mockFetch) as any).bind({ fetch: mockFetch });
const url = 'http://example.com';
const url = 'https://api.keploy.io/healthz';
const options = {
method: 'GET',
};
Expand Down

0 comments on commit 6cdf1d6

Please sign in to comment.