Skip to content

Commit

Permalink
feat: add axios mocking to component test-suite
Browse files Browse the repository at this point in the history
Signed-off-by: Sai Sankeerth <[email protected]>
  • Loading branch information
Sai Sankeerth committed Sep 20, 2023
1 parent 5fbddd1 commit b179787
Show file tree
Hide file tree
Showing 7 changed files with 1,088 additions and 4 deletions.
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
"devDependencies": {
"@commitlint/config-conventional": "^17.6.3",
"@digitalroute/cz-conventional-changelog-for-jira": "^8.0.1",
"@types/fast-json-stable-stringify": "^2.1.0",
"@types/jest": "^29.5.1",
"@types/koa": "^2.13.6",
"@types/koa-bodyparser": "^4.3.10",
Expand All @@ -133,6 +134,7 @@
"eslint-plugin-json": "^3.1.0",
"eslint-plugin-sonarjs": "^0.19.0",
"eslint-plugin-unicorn": "^46.0.1",
"fast-json-stable-stringify": "^2.1.0",
"glob": "^10.3.3",
"http-terminator": "^3.2.0",
"husky": "^8.0.3",
Expand Down
1 change: 1 addition & 0 deletions src/adapters/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,5 @@ module.exports = {
getPayloadData,
getFormData,
handleHttpRequest,
enhanceRequestOptions
};
79 changes: 76 additions & 3 deletions test/integrations/component.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { join } from 'path';
import Koa from 'koa';
import request from 'supertest';
// Mocking of axios calls
import axios from 'axios';
// new-library we are using
import stringify from 'fast-json-stable-stringify';
import bodyParser from 'koa-bodyparser';
import { Command } from 'commander';
import { createHttpTerminator } from 'http-terminator';
import { TestCaseData } from './testTypes';
import { MockHttpCallsData, TestCaseData } from './testTypes';
import { applicationRoutes } from '../../src/routes/index';
import { getTestDataFilePaths, getTestData } from './testUtils';
import { getTestDataFilePaths, getTestData, getMockHttpCallsData } from './testUtils';
import tags from '../../src/v0/util/tags';
import { Server } from 'http';

Expand Down Expand Up @@ -34,6 +38,68 @@ afterAll(async () => {
await createHttpTerminator({ server }).terminate();
});

// unmock already existing axios-mocking
jest.unmock('axios')

jest.mock('axios')
const formAxiosReqsMap = (calls: MockHttpCallsData[]) => {
try {
return calls.reduce((agg, curr) => {
let obj = curr.httpReq;
return { ...agg, [stringify(obj)]: curr.httpRes };
}, {})
} catch (error) {
return {}
}
}

const mockImpl = (type, axReqMap) => {
// return value fn
const retVal = (key) => {
if (axReqMap[key]) {
return axReqMap[key];
}
return {
status: 500,
body: 'Something bad'
}
}

if (['constructor'].includes(type)) {
return (opts) => {
// mock result from some cache
const key = stringify({ ...opts })
return retVal(key);
};
} else if (['delete', 'get'].includes(type)) {
return (url, opts) => {
// mock result from some cache
const key = stringify({ url, ...opts})
return retVal(key);
};
}

// post, patch, put
return (url, data, opts) => {
// mock result from some cache
const key = stringify({ url, data, ...opts })
return retVal(key);
};
};

const makeNetworkMocks = (axiosReqsMap: Record<string, any>) => {
axios.put = jest.fn(mockImpl('put', axiosReqsMap))
axios.post = jest.fn(mockImpl('post', axiosReqsMap))
axios.patch = jest.fn(mockImpl('patch', axiosReqsMap))
// @ts-ignore
axios.delete = jest.fn(mockImpl('delete', axiosReqsMap))
// @ts-ignore
axios.get = jest.fn(mockImpl('get', axiosReqsMap))
// @ts-ignore
axios.mockImplementation(mockImpl('constructor', axiosReqsMap))
}

// END
const rootDir = __dirname;
const allTestDataFilePaths = getTestDataFilePaths(rootDir, opts.destination);
const DEFAULT_VERSION = 'v0';
Expand Down Expand Up @@ -65,7 +131,7 @@ const testRoute = async (route, tcData: TestCaseData) => {
const outputResp = tcData.output.response || ({} as any);
expect(response.status).toEqual(outputResp.status);

if (outputResp && outputResp.body) {
if (outputResp?.body) {
expect(response.body).toEqual(outputResp.body);
}

Expand Down Expand Up @@ -111,6 +177,13 @@ const sourceTestHandler = async (tcData) => {
)}`;
await testRoute(route, tcData);
};
// all the axios requests will be stored in this map
const allAxiosReqsMap = allTestDataFilePaths.reduce((agg, currPath) => {
const mockNetworkCallsData: MockHttpCallsData[] = getMockHttpCallsData(currPath);
const reqMap = formAxiosReqsMap(mockNetworkCallsData)
return {...agg, ...reqMap}
}, {})
makeNetworkMocks(allAxiosReqsMap);

// Trigger the test suites
describe.each(allTestDataFilePaths)('%s Tests', (testDataPath) => {
Expand Down
Loading

0 comments on commit b179787

Please sign in to comment.