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

Fix @inngest/test automatic spying not accounting for step.** #777

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/famous-kiwis-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inngest/test": patch
---

Fix `@inngest/test` automatic spying not accounting for `step.**`
15 changes: 15 additions & 0 deletions packages/test/src/spy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,3 +623,18 @@ export function fn<T extends Procedure = Procedure>(

return enhancedSpy as any;
}

export const mockAny = <T>(obj: T): T => {
if (typeof obj === "function") {
return fn(obj as (...args: any[]) => any) as T;
}

if (typeof obj === "object" && obj !== null) {
return Object.keys(obj).reduce((acc, key) => {
(acc as any)[key] = mockAny(obj[key as keyof typeof obj]);
return acc;
}, obj);
}

return obj;
};
17 changes: 2 additions & 15 deletions packages/test/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
import { internalEvents } from "inngest";
import type { Context, EventPayload } from "inngest/types";
import { ulid } from "ulid";
import { fn as mockFn } from "./spy.js";
import { mockAny } from "./spy.js";

/**
* The default context transformation function that mocks all step tools. Use
* this in addition to your custom transformation function if you'd like to keep
* this functionality.
*/
export const mockCtx = (ctx: Readonly<Context.Any>): Context.Any => {
const step = Object.keys(ctx.step).reduce(
(acc, key) => {
const tool = ctx.step[key as keyof typeof ctx.step];
const mock = mockFn(tool);

return {
...acc,
[key]: mock,
};
},
{} as Context.Any["step"]
);

return {
...ctx,
step,
step: mockAny(ctx.step),
};
};

Expand Down
Loading