diff --git a/integrations/octokit/require.ts b/integrations/octokit/require.ts index 7286b3b..59689ab 100644 --- a/integrations/octokit/require.ts +++ b/integrations/octokit/require.ts @@ -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"; @@ -207,6 +207,7 @@ export function wrappedNodeFetch(fetchFunc: Function) { ); return fetchFunc.apply(this, [url, options]); } + createExecutionContext(ctx); return resp; } return mixin(wrappedFetch, fetchFunc, false); diff --git a/test/wrappedNodeFetch.test.ts b/test/wrappedNodeFetch.test.ts index 2e7adb8..32ad0c5 100644 --- a/test/wrappedNodeFetch.test.ts +++ b/test/wrappedNodeFetch.test.ts @@ -1,11 +1,11 @@ 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', @@ -13,19 +13,17 @@ describe('wrappedNodeFetch', () => { 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); @@ -33,9 +31,8 @@ describe('wrappedNodeFetch', () => { 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', @@ -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', @@ -70,15 +67,17 @@ 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); }); @@ -86,7 +85,7 @@ describe('wrappedNodeFetch', () => { 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', }; @@ -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', };