From 3fb754c638036b566ddc7100e60c3f9cc07fe8d2 Mon Sep 17 00:00:00 2001 From: Hermione Dadheech Date: Wed, 8 Mar 2023 20:19:39 +0530 Subject: [PATCH 1/9] feat(github actions and wrappednodefetched.test.js): integrated github action and wrote unit test For code coverage added unit test for wrappedNodeFetch function and integrated git hub actions. test #354 Signed-off-by: Hermione Dadheech --- .github/workflows/ci.yml | 3 + .gitignore | 6 +- jest.config.ts | 197 ++++++++++++++++++++++++++++++++++ package.json | 7 +- test/wrappedNodeFetch.test.ts | 112 +++++++++++++++++++ 5 files changed, 323 insertions(+), 2 deletions(-) create mode 100644 jest.config.ts create mode 100644 test/wrappedNodeFetch.test.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9f6df20..5c5046a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,3 +33,6 @@ jobs: - name: Check commits messages uses: wagoid/commitlint-github-action@v3 + + - name: Run the tests + run: npm test -- --coverage diff --git a/.gitignore b/.gitignore index e4bfe12..53de32b 100644 --- a/.gitignore +++ b/.gitignore @@ -184,4 +184,8 @@ node_modules # Build files -dist \ No newline at end of file +dist + +#Test coverage report + +coverage \ No newline at end of file diff --git a/jest.config.ts b/jest.config.ts new file mode 100644 index 0000000..c094f87 --- /dev/null +++ b/jest.config.ts @@ -0,0 +1,197 @@ +/* + * For a detailed explanation regarding each configuration property and type check, visit: + * https://jestjs.io/docs/configuration + */ + +export default { + preset:'ts-jest', + testEnvironment:'node', + // All imported modules in your tests should be mocked automatically + // automock: false, + + // Stop running tests after `n` failures + // bail: 0, + + // The directory where Jest should store its cached dependency information + // cacheDirectory: "/tmp/jest_rs", + + // Automatically clear mock calls, instances, contexts and results before every test + clearMocks: true, + + // Indicates whether the coverage information should be collected while executing the test + collectCoverage: true, + + // An array of glob patterns indicating a set of files for which coverage information should be collected + // collectCoverageFrom: undefined, + + // The directory where Jest should output its coverage files + coverageDirectory: "coverage", + + // An array of regexp pattern strings used to skip coverage collection + // coveragePathIgnorePatterns: [ + // "/node_modules/" + // ], + + // Indicates which provider should be used to instrument code for coverage + coverageProvider: "v8", + + // A list of reporter names that Jest uses when writing coverage reports + // coverageReporters: [ + // "json", + // "text", + // "lcov", + // "clover" + // ], + + // An object that configures minimum threshold enforcement for coverage results + // coverageThreshold: undefined, + + // A path to a custom dependency extractor + // dependencyExtractor: undefined, + + // Make calling deprecated APIs throw helpful error messages + // errorOnDeprecated: false, + + // The default configuration for fake timers + // fakeTimers: { + // "enableGlobally": false + // }, + + // Force coverage collection from ignored files using an array of glob patterns + // forceCoverageMatch: [], + + // A path to a module which exports an async function that is triggered once before all test suites + // globalSetup: undefined, + + // A path to a module which exports an async function that is triggered once after all test suites + // globalTeardown: undefined, + + // A set of global variables that need to be available in all test environments + // globals: {}, + + // The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers. + // maxWorkers: "50%", + + // An array of directory names to be searched recursively up from the requiring module's location + // moduleDirectories: [ + // "node_modules" + // ], + + // An array of file extensions your modules use + // moduleFileExtensions: [ + // "js", + // "mjs", + // "cjs", + // "jsx", + // "ts", + // "tsx", + // "json", + // "node" + // ], + + // A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module + // moduleNameMapper: {}, + + // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader + // modulePathIgnorePatterns: [], + + // Activates notifications for test results + // notify: false, + + // An enum that specifies notification mode. Requires { notify: true } + // notifyMode: "failure-change", + + // A preset that is used as a base for Jest's configuration + // preset: undefined, + + // Run tests from one or more projects + // projects: undefined, + + // Use this configuration option to add custom reporters to Jest + // reporters: undefined, + + // Automatically reset mock state before every test + // resetMocks: false, + + // Reset the module registry before running each individual test + // resetModules: false, + + // A path to a custom resolver + // resolver: undefined, + + // Automatically restore mock state and implementation before every test + // restoreMocks: false, + + // The root directory that Jest should scan for tests and modules within + // rootDir: undefined, + + // A list of paths to directories that Jest should use to search for files in + // roots: [ + // "" + // ], + + // Allows you to use a custom runner instead of Jest's default test runner + // runner: "jest-runner", + + // The paths to modules that run some code to configure or set up the testing environment before each test + // setupFiles: [], + + // A list of paths to modules that run some code to configure or set up the testing framework before each test + // setupFilesAfterEnv: [], + + // The number of seconds after which a test is considered as slow and reported as such in the results. + // slowTestThreshold: 5, + + // A list of paths to snapshot serializer modules Jest should use for snapshot testing + // snapshotSerializers: [], + + // The test environment that will be used for testing + // testEnvironment: "jest-environment-node", + + // Options that will be passed to the testEnvironment + // testEnvironmentOptions: {}, + + // Adds a location field to test results + // testLocationInResults: false, + + // The glob patterns Jest uses to detect test files + // testMatch: [ + // "**/__tests__/**/*.[jt]s?(x)", + // "**/?(*.)+(spec|test).[tj]s?(x)" + // ], + + // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped + // testPathIgnorePatterns: [ + // "/node_modules/" + // ], + + // The regexp pattern or array of patterns that Jest uses to detect test files + // testRegex: [], + + // This option allows the use of a custom results processor + // testResultsProcessor: undefined, + + // This option allows use of a custom test runner + // testRunner: "jest-circus/runner", + + // A map from regular expressions to paths to transformers + // transform: undefined, + + // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation + // transformIgnorePatterns: [ + // "/node_modules/", + // "\\.pnp\\.[^\\/]+$" + // ], + + // An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them + // unmockedModulePathPatterns: undefined, + + // Indicates whether each individual test should be reported during the run + // verbose: undefined, + + // An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode + // watchPathIgnorePatterns: [], + + // Whether to use watchman for file crawling + // watchman: true, +}; diff --git a/package.json b/package.json index 8ea57c5..271cf77 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "scripts": { "build": "tsc && cp ./proto/services.proto ./dist/proto/", "prepare": "npm run build", - "commit": "cz" + "commit": "cz", + "test": "jest" }, "repository": "git@github.com:keploy/typescript-sdk.git", "author": "Rajat Sharma ", @@ -18,6 +19,7 @@ "@commitlint/config-conventional": "^16.2.1", "@types/cors": "^2.8.12", "@types/express": "^4.17.13", + "@types/jest": "^29.4.0", "@types/merge-descriptors": "^1.0.1", "@types/node": "^18.7.18", "@types/node-fetch": "^2.6.2", @@ -31,7 +33,10 @@ "eslint-plugin-import": "^2.25.4", "eslint-plugin-prettier": "^4.0.0", "husky": "^7.0.4", + "jest": "^29.5.0", "prettier": "^2.5.1", + "ts-jest": "^29.0.5", + "ts-node": "^10.9.1", "typescript": "^4.6.2" }, "config": { diff --git a/test/wrappedNodeFetch.test.ts b/test/wrappedNodeFetch.test.ts new file mode 100644 index 0000000..61366bb --- /dev/null +++ b/test/wrappedNodeFetch.test.ts @@ -0,0 +1,112 @@ +import { wrappedNodeFetch } from '../integrations/octokit/require'; +import { Response } from 'node-fetch'; +import { createExecutionContext } from '../src/context'; +describe('wrappedNodeFetch', () => { + it('should call fetch function with correct arguments in record mode', async () => { + const mockFetch = jest.fn().mockResolvedValueOnce(new Response()); + const ctx = { + context:{ + mode: 'record', + testId: 'testId', + mocks: [], + deps: [], + } + }; + createExecutionContext(ctx) + const wrappedFetch = (wrappedNodeFetch(mockFetch) as any).bind({ fetch: mockFetch }); + const url = 'http://example.com'; + const options = { + method: 'GET', + }; + const response = await wrappedFetch(url, options); + console.log(response,"RESPONSE") + + expect(mockFetch).toHaveBeenCalledWith(url, options); + expect(response).toBeInstanceOf(Response); + }); + + it('should return mocked response in test mode', async () => { + const mockResponse = new Response('mocked response'); + const mockFetch = jest.fn().mockResolvedValue(mockResponse); + const ctx = { + context:{ + mode: 'test', + testId: 'testId', + mocks: [ + { + Version: 'V1_BETA2', + Name: 'testId', + Kind: 'HTTP', + Spec: { + Metadata: { + name: 'node-fetch', + url: 'http://example.com', + options: { method: 'GET' }, + type: 'HTTP_CLIENT', + }, + Req: { + URL: 'http://example.com', + Body: '', + Header: {}, + Method: 'GET', + }, + Res: { + StatusCode: 200, + Header: { 'content-type': { Value: ['text/plain'] } }, + Body: 'mocked response', + }, + }, + }, + ], + deps: [], + } + }; + createExecutionContext(ctx) + + const wrappedFetch = (wrappedNodeFetch(mockFetch) as any).bind({ fetch: mockFetch }); + const url = 'http://example.com'; + const options = { + method: 'GET', + }; + const response = await wrappedFetch(url, options); + console.log(response,"RESPONSE") + expect(response).toEqual(mockResponse); + }); + + 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 options = { + method: 'GET', + }; + const response = await wrappedFetch(url, options); + expect(consoleSpy).toHaveBeenCalledWith('keploy context is not present to mock dependencies'); + expect(response).toBeUndefined(); + }); + + it('should call fetch function with correct arguments in off mode', async () => { + const mockFetch = jest.fn().mockResolvedValueOnce(new Response()); + const ctx = { + context:{ + context: 'off', + testId: 'testId', + mocks: [], + deps: [], + } + }; + createExecutionContext(ctx) + + const wrappedFetch = (wrappedNodeFetch(mockFetch) as any).bind({ fetch: mockFetch }); + const url = 'http://example.com'; + const options = { + method: 'GET', + }; + const response = await wrappedFetch(url, options); + console.log(response,"RESPONSE") + + expect(mockFetch).toHaveBeenCalledWith(url, options); + expect(response).toBeInstanceOf(Response); + }); +}); \ No newline at end of file From bd525e2d305ef1aa767e4b1e204d3f8d05489ce0 Mon Sep 17 00:00:00 2001 From: Ritik Jain <60597329+re-Tick@users.noreply.github.com> Date: Wed, 1 Mar 2023 22:07:55 +0530 Subject: [PATCH 2/9] fix: updates the commit linter versio Signed-off-by: Hermione Dadheech --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5c5046a..564e570 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -32,7 +32,7 @@ jobs: cmd: build - name: Check commits messages - uses: wagoid/commitlint-github-action@v3 + uses: wagoid/commitlint-github-action@v5 - name: Run the tests run: npm test -- --coverage From 479e05d8b1474af51de01ed9e6be4fb3473cb1bc Mon Sep 17 00:00:00 2001 From: Hermione Dadheech Date: Thu, 9 Mar 2023 01:18:08 +0530 Subject: [PATCH 3/9] feat(ci.yml): revert version and removing console Revert version and removing console from unit test file test #354 Signed-off-by: Hermione Dadheech --- test/wrappedNodeFetch.test.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/wrappedNodeFetch.test.ts b/test/wrappedNodeFetch.test.ts index 61366bb..7ba2516 100644 --- a/test/wrappedNodeFetch.test.ts +++ b/test/wrappedNodeFetch.test.ts @@ -19,8 +19,6 @@ describe('wrappedNodeFetch', () => { method: 'GET', }; const response = await wrappedFetch(url, options); - console.log(response,"RESPONSE") - expect(mockFetch).toHaveBeenCalledWith(url, options); expect(response).toBeInstanceOf(Response); }); @@ -69,7 +67,6 @@ describe('wrappedNodeFetch', () => { method: 'GET', }; const response = await wrappedFetch(url, options); - console.log(response,"RESPONSE") expect(response).toEqual(mockResponse); }); @@ -104,7 +101,6 @@ describe('wrappedNodeFetch', () => { method: 'GET', }; const response = await wrappedFetch(url, options); - console.log(response,"RESPONSE") expect(mockFetch).toHaveBeenCalledWith(url, options); expect(response).toBeInstanceOf(Response); From 532413e8480e32c902634809e0e1d7f8a5e8ee72 Mon Sep 17 00:00:00 2001 From: Hermione Dadheech Date: Thu, 9 Mar 2023 19:57:41 +0530 Subject: [PATCH 4/9] feat(wrappednodefetch.test.ts): added expect test for mocks and deps Added expect test for mocks and deps after fetching in record and test mode test #354 Signed-off-by: Hermione Dadheech --- test/wrappedNodeFetch.test.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/test/wrappedNodeFetch.test.ts b/test/wrappedNodeFetch.test.ts index 7ba2516..8833ffc 100644 --- a/test/wrappedNodeFetch.test.ts +++ b/test/wrappedNodeFetch.test.ts @@ -1,16 +1,14 @@ import { wrappedNodeFetch } from '../integrations/octokit/require'; import { Response } from 'node-fetch'; -import { createExecutionContext } from '../src/context'; +import { createExecutionContext, getExecutionContext} from '../src/context'; describe('wrappedNodeFetch', () => { it('should call fetch function with correct arguments in record mode', async () => { const mockFetch = jest.fn().mockResolvedValueOnce(new Response()); const ctx = { - context:{ mode: 'record', testId: 'testId', mocks: [], - deps: [], - } + deps: [], }; createExecutionContext(ctx) const wrappedFetch = (wrappedNodeFetch(mockFetch) as any).bind({ fetch: mockFetch }); @@ -18,16 +16,21 @@ describe('wrappedNodeFetch', () => { const options = { method: 'GET', }; + const response = await wrappedFetch(url, options); + const updatedctx= getExecutionContext().context; + const mocks=updatedctx.mocks.length; + const deps=updatedctx.deps.length; expect(mockFetch).toHaveBeenCalledWith(url, options); expect(response).toBeInstanceOf(Response); + expect(mocks).toBeGreaterThan(0); + expect(deps).toBeGreaterThan(0); }); it('should return mocked response in test mode', async () => { const mockResponse = new Response('mocked response'); const mockFetch = jest.fn().mockResolvedValue(mockResponse); const ctx = { - context:{ mode: 'test', testId: 'testId', mocks: [ @@ -38,12 +41,12 @@ describe('wrappedNodeFetch', () => { Spec: { Metadata: { name: 'node-fetch', - url: 'http://example.com', + url: 'http://localhost:8080', options: { method: 'GET' }, type: 'HTTP_CLIENT', }, Req: { - URL: 'http://example.com', + URL: 'http://localhost:8080', Body: '', Header: {}, Method: 'GET', @@ -57,7 +60,7 @@ describe('wrappedNodeFetch', () => { }, ], deps: [], - } + }; createExecutionContext(ctx) @@ -67,7 +70,10 @@ describe('wrappedNodeFetch', () => { method: 'GET', }; const response = await wrappedFetch(url, options); + const updatedctx= getExecutionContext().context; expect(response).toEqual(mockResponse); + const mocks=updatedctx.mocks.length(); + expect(mocks).toBe(0); }); it('should return undefined if execution context is not present in record mode', async () => { @@ -86,12 +92,10 @@ describe('wrappedNodeFetch', () => { it('should call fetch function with correct arguments in off mode', async () => { const mockFetch = jest.fn().mockResolvedValueOnce(new Response()); const ctx = { - context:{ context: 'off', testId: 'testId', mocks: [], deps: [], - } }; createExecutionContext(ctx) From 75ff0cb91ddc851a32e4575e4dea475732343619 Mon Sep 17 00:00:00 2001 From: Hermione Dadheech Date: Fri, 10 Mar 2023 00:36:50 +0530 Subject: [PATCH 5/9] feat(wrappednodefetch.test.ts): added a check to compare the recorded output with response Added a check to compare the recorded output with response to ensure wrappedNodeFetch functionality test #354 Signed-off-by: Hermione Dadheech --- test/wrappedNodeFetch.test.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/wrappedNodeFetch.test.ts b/test/wrappedNodeFetch.test.ts index 8833ffc..3da609e 100644 --- a/test/wrappedNodeFetch.test.ts +++ b/test/wrappedNodeFetch.test.ts @@ -21,10 +21,14 @@ describe('wrappedNodeFetch', () => { 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 () => { @@ -41,12 +45,12 @@ describe('wrappedNodeFetch', () => { Spec: { Metadata: { name: 'node-fetch', - url: 'http://localhost:8080', + url: 'http://example.com', options: { method: 'GET' }, type: 'HTTP_CLIENT', }, Req: { - URL: 'http://localhost:8080', + URL: 'http://example.com', Body: '', Header: {}, Method: 'GET', From 351517f337f6e2222434c350ca40989b77147227 Mon Sep 17 00:00:00 2001 From: Hermione Dadheech Date: Fri, 10 Mar 2023 12:48:38 +0530 Subject: [PATCH 6/9] feat(wrappednodefetch.test.ts): importing HTTP from src/keploy.ts importing HTTP from src/keploy.ts test #354 Signed-off-by: Hermione Dadheech --- test/wrappedNodeFetch.test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/wrappedNodeFetch.test.ts b/test/wrappedNodeFetch.test.ts index 3da609e..2e7adb8 100644 --- a/test/wrappedNodeFetch.test.ts +++ b/test/wrappedNodeFetch.test.ts @@ -1,6 +1,8 @@ import { wrappedNodeFetch } from '../integrations/octokit/require'; import { Response } 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()); @@ -41,7 +43,7 @@ describe('wrappedNodeFetch', () => { { Version: 'V1_BETA2', Name: 'testId', - Kind: 'HTTP', + Kind: HTTP, Spec: { Metadata: { name: 'node-fetch', From ff354ac49eb152f829a15df1ab8ee593b6e32d39 Mon Sep 17 00:00:00 2001 From: Hermione Dadheech Date: Tue, 14 Mar 2023 15:39:43 +0530 Subject: [PATCH 7/9] test(wrappednodefetch, octokit/require.ts): adding fetch method and createExecutionContext Resolving the failed test cases by using fetch from node-fetch and createExecutionContext test#354 Signed-off-by: Hermione Dadheech --- integrations/octokit/require.ts | 3 ++- test/wrappedNodeFetch.test.ts | 29 ++++++++++++++--------------- 2 files changed, 16 insertions(+), 16 deletions(-) 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', }; From b1066bcc321f3e4df4717b88f0da9fa6a2145d2b Mon Sep 17 00:00:00 2001 From: Hermione Dadheech Date: Tue, 14 Mar 2023 17:28:53 +0530 Subject: [PATCH 8/9] test(wrappednodefetch): updating checks updating test cases test #354 Signed-off-by: Hermione Dadheech --- test/wrappedNodeFetch.test.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/wrappedNodeFetch.test.ts b/test/wrappedNodeFetch.test.ts index 32ad0c5..4eeaf23 100644 --- a/test/wrappedNodeFetch.test.ts +++ b/test/wrappedNodeFetch.test.ts @@ -20,13 +20,11 @@ describe('wrappedNodeFetch', () => { }; 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(response).toBeInstanceOf(Response); - expect(mocks).toBeGreaterThan(0); - expect(deps).toBeGreaterThan(0); + expect(updatedctx.mocks.length).toBeGreaterThan(0); + expect(updatedctx.deps.length).toBeGreaterThan(0); expect(response).toHaveProperty('body'); expect(responseBody).toEqual(recordedOutput); }); From 4bec4d0b00ff36a7f77700c166314bcd92fe5857 Mon Sep 17 00:00:00 2001 From: Hermione Dadheech Date: Tue, 14 Mar 2023 18:52:41 +0530 Subject: [PATCH 9/9] feat(require.ts): removing createExecutionContext removing createExecutionContext from octokit test #354 Signed-off-by: Hermione Dadheech --- integrations/octokit/require.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/integrations/octokit/require.ts b/integrations/octokit/require.ts index 522f75c..295dbae 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,createExecutionContext } from "../../src/context"; +import { getExecutionContext} from "../../src/context"; import { Readable } from "stream"; import { ProcessDep, stringToBinary } from "../../src/util"; import { putMocks } from "../../mock/utils"; @@ -208,7 +208,7 @@ export function wrappedNodeFetch(fetchFunc: Function) { ); return fetchFunc.apply(this, [url, options]); } - createExecutionContext(ctx); + return resp; } return mixin(wrappedFetch, fetchFunc, false);