Skip to content
Closed
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ jobs:

- name: Check commits messages
uses: wagoid/commitlint-github-action@v5

- name: Run the tests
Comment thread
Sekhar-Kumar-Dash marked this conversation as resolved.
Outdated
run: npm test -- --coverage
69 changes: 69 additions & 0 deletions newtest/unittest1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import fetchMock, { MockResponse } from "fetch-mock";
Comment thread
Sekhar-Kumar-Dash marked this conversation as resolved.
Outdated
import { wrappedNodeFetch } from "../integrations/octokit/require";
import { Response } from 'node-fetch';
import { createExecutionContext, getExecutionContext} from '../src/context';
import { HTTP } from '../src/keploy';

describe("wrappedNodeFetch", () => {
afterEach(() => {
fetchMock.restore();
});

it("should make a network request in record mode", async () => {
const ctx = {
mode: "record",
testId: "test-id",
mocks: [],
deps: [],
};
const url = "https://api.github.com/repos/octocat/hello-world/issues/123";
const options: RequestInit = {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: "token secret123",
},
};
const fetchSpy = jest.spyOn(fetchMock, "default").mockReturnValue(
Promise.resolve({
status: 200,
body: "Hello world!",
headers: {
"Content-Type": "text/plain",
},
})
);

const wrappedFetch = wrappedNodeFetch(fetchMock.default);

await wrappedFetch.call({ fetch: fetchMock.default }, url, options);

expect(fetchSpy).toHaveBeenCalledWith(url, {
method: options.method,
headers: options.headers,
});

expect(ctx.mocks).toHaveLength(1);

const httpMock = ctx.mocks[0] as HTTP;
expect(httpMock.Kind).toEqual("HTTP");
expect(httpMock.Spec.Metadata.name).toEqual("node-fetch");
expect(httpMock.Spec.Metadata.url).toEqual(url);
expect(httpMock.Spec.Metadata.type).toEqual("HTTP_CLIENT");
expect(httpMock.Spec.Metadata.options).toEqual(options);

expect(httpMock.Spec.Req.Method).toEqual(options.method);
expect(httpMock.Spec.Req.URL).toEqual(url);
expect(httpMock.Spec.Req.Header).toEqual({
"Content-Type": "application/json",
Authorization: "token secret123",
});
expect(httpMock.Spec.Req.Body).toBeUndefined();

expect(httpMock.Spec.Res.StatusCode).toEqual(200);
expect(httpMock.Spec.Res.Header).toEqual({
"Content-Type": "text/plain",
});
expect(httpMock.Spec.Res.Body).toEqual("Hello world!");
});
});
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <lunasunkaiser@gmail.com>",
Expand All @@ -31,6 +32,7 @@
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-prettier": "^4.0.0",
"husky": "^7.0.4",
"jest": "^27.4.5",
"prettier": "^2.5.1",
"typescript": "^4.6.2"
},
Expand Down