From 325ef7925a040090ae7990ae16731bd84a9b3431 Mon Sep 17 00:00:00 2001 From: Jack Williams Date: Wed, 11 Dec 2024 17:44:28 +0000 Subject: [PATCH] Fix `@inngest/test` automatic spying not accounting for `step.**` (#777) ## Summary `@inngest/test` performs automatic mocking of the functions within the `step` object. This change ensure we also recursively apply those mocks such that `step.**()` is mocked, such as `step.ai.infer()`. ## Checklist - [ ] ~Added a [docs PR](https://github.com/inngest/website) that references this PR~ N/A Bug fix - [ ] ~Added unit/integration tests~ N/A Using in another PR - [x] Added changesets if applicable --- .changeset/famous-kiwis-itch.md | 5 +++++ packages/test/src/spy.ts | 15 +++++++++++++++ packages/test/src/util.ts | 17 ++--------------- 3 files changed, 22 insertions(+), 15 deletions(-) create mode 100644 .changeset/famous-kiwis-itch.md diff --git a/.changeset/famous-kiwis-itch.md b/.changeset/famous-kiwis-itch.md new file mode 100644 index 000000000..42bc2efd0 --- /dev/null +++ b/.changeset/famous-kiwis-itch.md @@ -0,0 +1,5 @@ +--- +"@inngest/test": patch +--- + +Fix `@inngest/test` automatic spying not accounting for `step.**` diff --git a/packages/test/src/spy.ts b/packages/test/src/spy.ts index dd4dc0a2d..3b5cec8a5 100644 --- a/packages/test/src/spy.ts +++ b/packages/test/src/spy.ts @@ -623,3 +623,18 @@ export function fn( return enhancedSpy as any; } + +export const mockAny = (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; +}; diff --git a/packages/test/src/util.ts b/packages/test/src/util.ts index 973acb20c..4d3a37455 100644 --- a/packages/test/src/util.ts +++ b/packages/test/src/util.ts @@ -1,7 +1,7 @@ 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 @@ -9,22 +9,9 @@ import { fn as mockFn } from "./spy.js"; * this functionality. */ export const mockCtx = (ctx: Readonly): 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), }; };