Skip to content

Commit

Permalink
fix(src/utils): adds ctx parameter in processDep (#69)
Browse files Browse the repository at this point in the history
the executionContext was getting undefined for mocking dependency call outputs

Signed-off-by: re-Tick <[email protected]>
  • Loading branch information
re-Tick authored Mar 18, 2023
1 parent bc40db8 commit 9ecec0c
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 28 deletions.
8 changes: 4 additions & 4 deletions integrations/mongoose/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function kDeleteOne(...args) {
// wrap callback of updateOne to capture the mongodb-native-driver outputs
// @ts-ignore
args[args.length - 1] = function (...outputs) {
ProcessDep(meta, ...outputs);
ProcessDep(ctx, meta, ...outputs);
// calls the actual mongoose callback for findOne
callback.apply(this, outputs);
};
Expand All @@ -50,7 +50,7 @@ export function kDeleteOne(...args) {
if (typeof callback === "function") {
// mocked outputs of updateOne opperation
const outputs: any[] = [null, {}];
const mocks = ProcessDep(meta, ...outputs);
const mocks = ProcessDep(ctx, meta, ...outputs);
// calls the actual mongoose callback for findOne
// @ts-ignore
callback.apply(this, mocks);
Expand Down Expand Up @@ -102,7 +102,7 @@ export function kDeleteMany(...args) {
// wrap callback of deleteMany to capture the mongodb-native-driver outputs
// @ts-ignore
args[args.length - 1] = function (...outputs) {
ProcessDep(meta, ...outputs);
ProcessDep(ctx, meta, ...outputs);
// calls the actual mongoose callback for findOne
callback.apply(this, outputs);
};
Expand All @@ -117,7 +117,7 @@ export function kDeleteMany(...args) {
if (typeof callback === "function") {
// mocked outputs of deleteMany opperation
const outputs: any[] = [null, {}];
const mocks = ProcessDep(meta, ...outputs);
const mocks = ProcessDep(ctx, meta, ...outputs);
// calls the actual mongoose callback for findOne
// @ts-ignore
callback.apply(this, mocks);
Expand Down
8 changes: 4 additions & 4 deletions integrations/mongoose/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function kFindOne(...args) {
// wrap callback of findOne to capture the mongodb-native-driver outputs
// @ts-ignore
args[args.length - 1] = function (...outputs) {
ProcessDep(meta, ...outputs);
ProcessDep(ctx, meta, ...outputs);
// calls the actual mongoose callback for findOne
callback.apply(this, outputs);
};
Expand All @@ -51,7 +51,7 @@ export function kFindOne(...args) {
if (typeof callback === "function") {
// mocked outputs of findOne opperation
const outputs: any[] = [null, {}];
const mocks = ProcessDep(meta, ...outputs);
const mocks = ProcessDep(ctx, meta, ...outputs);
// calls the actual mongoose callback for findOne
// @ts-ignore
callback.apply(this, mocks);
Expand Down Expand Up @@ -110,7 +110,7 @@ export function kFind(...args) {
// call the actual toArray method of cursor
const result = await actualToArray.apply(outputs[1], cb);
// encode and stores the documents in executionContext
ProcessDep(meta, outputs[0], result);
ProcessDep(ctx, meta, outputs[0], result);
// calls the actual user defined callback
cb(outputs[0], result);
return result;
Expand All @@ -135,7 +135,7 @@ export function kFind(...args) {
const outputs = [null, {}];
let result: any[] = [];
// returns the mocked outputs of find call
const mocks = ProcessDep(meta, outputs[0], result);
const mocks = ProcessDep(ctx, meta, outputs[0], result);
if (mocks !== undefined && mocks.length == 2) {
result = mocks[1];
outputs[0] = mocks[0];
Expand Down
8 changes: 4 additions & 4 deletions integrations/mongoose/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function kInsertMany(...args) {
// wrap callback of insertMany to capture the mongodb-native-driver outputs
// @ts-ignore
args[args.length - 1] = function (...outputs) {
ProcessDep(meta, ...outputs);
ProcessDep(ctx, meta, ...outputs);
// calls the actual mongoose callback for findOne
callback.apply(this, outputs);
};
Expand All @@ -49,7 +49,7 @@ export function kInsertMany(...args) {
if (typeof callback === "function") {
// mocked outputs of insertMany opperation
const outputs: any[] = [null, {}];
const mocks = ProcessDep(meta, ...outputs);
const mocks = ProcessDep(ctx, meta, ...outputs);
// calls the actual mongoose callback for findOne
// @ts-ignore
callback.apply(this, mocks);
Expand Down Expand Up @@ -100,7 +100,7 @@ export function kInsertOne(...args) {
// wrap callback of insertOne to capture the mongodb-native-driver outputs
// @ts-ignore
args[args.length - 1] = function (...outputs) {
ProcessDep(meta, ...outputs);
ProcessDep(ctx, meta, ...outputs);
// calls the actual mongoose callback for findOne
callback.apply(this, outputs);
};
Expand All @@ -115,7 +115,7 @@ export function kInsertOne(...args) {
if (typeof callback === "function") {
// mocked outputs of insertOne opperation
const outputs: any[] = [null, {}];
const mocks = ProcessDep(meta, ...outputs);
const mocks = ProcessDep(ctx, meta, ...outputs);
// calls the actual mongoose callback for findOne
// @ts-ignore
callback.apply(this, mocks);
Expand Down
8 changes: 4 additions & 4 deletions integrations/mongoose/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function kUpdateOne(...args) {
// wrap callback of updateOne to capture the mongodb-native-driver outputs
// @ts-ignore
args[args.length - 1] = function (...outputs) {
ProcessDep(meta, ...outputs);
ProcessDep(ctx, meta, ...outputs);
// calls the actual mongoose callback for findOne
callback.apply(this, outputs);
};
Expand All @@ -49,7 +49,7 @@ export function kUpdateOne(...args) {
if (typeof callback === "function") {
// mocked outputs of updateOne opperation
const outputs: any[] = [null, {}];
const mocks = ProcessDep(meta, ...outputs);
const mocks = ProcessDep(ctx, meta, ...outputs);
// calls the actual mongoose callback for findOne
// @ts-ignore
callback.apply(this, mocks);
Expand Down Expand Up @@ -102,7 +102,7 @@ export function kUpdateMany(...args) {
// wrap callback of updateMany to capture the mongodb-native-driver outputs
// @ts-ignore
args[args.length - 1] = function (...outputs) {
ProcessDep(meta, ...outputs);
ProcessDep(ctx, meta, ...outputs);
// calls the actual mongoose callback for findOne
callback.apply(this, outputs);
};
Expand All @@ -117,7 +117,7 @@ export function kUpdateMany(...args) {
if (typeof callback === "function") {
// mocked outputs of updateMany opperation
const outputs: any[] = [null, {}];
const mocks = ProcessDep(meta, ...outputs);
const mocks = ProcessDep(ctx, meta, ...outputs);
// calls the actual mongoose callback for findOne
// @ts-ignore
callback.apply(this, mocks);
Expand Down
2 changes: 1 addition & 1 deletion integrations/node-fetch/require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export function wrappedNodeFetch(fetch: any) {
}
ctx.mocks.shift();
} else {
ProcessDep({}, outputs);
ProcessDep(ctx, {}, outputs);
}
rinit.headers = new Headers(outputs[1].headers);
rinit.status = outputs[1].status;
Expand Down
2 changes: 1 addition & 1 deletion integrations/octokit/require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export function wrappedNodeFetch(fetchFunc: Function) {
}
ctx.mocks.shift();
} else {
ProcessDep({}, outputs);
ProcessDep(ctx, {}, outputs);
}
rinit.headers = new Headers(outputs[1].headers);
rinit.status = outputs[1].status;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"commit": "cz"
},
"repository": "[email protected]:keploy/typescript-sdk.git",
"author": "Rajat Sharma <[email protected]>",
"author": "Keploy <[email protected]>",
"license": "MIT",
"devDependencies": {
"@commitlint/cli": "^16.2.1",
Expand Down
22 changes: 13 additions & 9 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@ const transformToSnakeCase = (obj: any): object => {

export { transformToSnakeCase };

export function ProcessDep(meta: { [key: string]: string }, ...outputs: any[]) {
if (
getExecutionContext() == undefined ||
getExecutionContext().context == undefined
) {
console.error("keploy context is not present to mock dependencies");
return;
}
const kctx = getExecutionContext().context;
export function ProcessDep(
kctx: any,
meta: { [key: string]: string },
...outputs: any[]
) {
// if (
// getExecutionContext() == undefined ||
// getExecutionContext().context == undefined
// ) {
// console.error("keploy context is not present to mock dependencies");
// return;
// }
// const kctx = getExecutionContext().context;
switch (kctx.mode) {
case "record":
const res: DataBytes[] = [];
Expand Down

0 comments on commit 9ecec0c

Please sign in to comment.