Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add axios mocking to component test-suite #2638

Merged
merged 8 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
sanpj2292 marked this conversation as resolved.
Show resolved Hide resolved
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);
};
sanpj2292 marked this conversation as resolved.
Show resolved Hide resolved
};

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) => {
koladilip marked this conversation as resolved.
Show resolved Hide resolved
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
28 changes: 28 additions & 0 deletions test/integrations/destinations/canny/data.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { enhanceRequestOptions } from "../../../../src/adapters/network";
import qs from 'qs';

export const data = [
{
name: 'canny',
Expand Down Expand Up @@ -1791,3 +1794,28 @@ export const data = [
},
},
];

export const networkCallsData = [
{
httpReq: enhanceRequestOptions({
url: 'https://canny.io/api/v1/users/retrieve',
"Content-Type": 'application/x-www-form-urlencoded',
Accept: 'application/json',
data: qs.stringify({apiKey: 'apikey123', email: '[email protected]'})
}),
httpRes: {
"data": {
"avatarURL": "https://canny.io/images/cddfd145056cd4bc04132ee0e7de04ee.png",
"created": "2022-07-15T11:16:32.648Z",
"email": "[email protected]",
"id": "52d14c90fff7c80abcd12345",
"isAdmin": true,
"lastActivity": "2022-07-18T14:24:43.632Z",
"name": "Rudder Test",
"url": "https://ruderstack.canny.io/admin/users/dummyUser",
"userID": null
},
status: 200
}
}
]
Loading
Loading