Skip to main content

Import

import { invokeFlow, createMockContextComponents } from "@prismatic-io/spectral/dist/testing";

Signature

export const invokeFlow = async <
  TInputs extends Inputs,
  TActionInputs extends Inputs,
  TConfigVars extends ConfigVarResultCollection = ConfigVarResultCollection,
  TConfigVarValues extends TestConfigVarValues = ToTestValues<TConfigVars>,
  TPayload extends TriggerPayload = TriggerPayload,
  TAllowsBranching extends boolean = boolean,
  TResult extends TriggerResult<TAllowsBranching, TPayload> = TriggerResult<TAllowsBranching, TPayload>,
>(
  flow: Flow<TInputs, TActionInputs, TPayload, TAllowsBranching, TResult>,
  {
    configVars,
    context,
    payload,
  }?: {
    configVars?: TConfigVarValues;
    context?: Partial<ActionContext<TConfigVars>>;
    payload?: Partial<TriggerPayload>;
  },
): Promise<InvokeReturn<ActionPerformReturn<false, unknown>>>
invokeFlow runs both the onTrigger and onExecution functions of a CNI Flow definition in sequence. If the flow has no onTrigger, the default trigger payload is used. The result of the trigger is made available to onExecution via params.onTrigger.results.
invokeFlow is for testing Code Native Integration (CNI) flows. For testing individual actions, use invoke instead.

Parameters

flow
Flow
required
The flow definition created with the flow() function.
options
object
Optional configuration for the test run.

Return value

interface InvokeReturn<ActionPerformReturn<false, unknown>> {
  result: ActionPerformReturn<false, unknown>;
  loggerMock: ActionLogger;
}
result
ActionPerformReturn<false, unknown>
required
The return value of the flow’s onExecution function.
loggerMock
ActionLogger
required
A mock logger for asserting log output. See loggerMock.

Examples

import { flow } from "@prismatic-io/spectral";
import { invokeFlow } from "@prismatic-io/spectral/dist/testing";
import { describe, expect, it } from "vitest";

const basicFlow = flow({
  name: "Basic Flow",
  stableKey: "basic-flow",
  description: "Processes incoming webhook data",
  onExecution: async (context, params) => {
    return { data: params.onTrigger.results.body.data };
  },
});

describe("basicFlow", () => {
  it("returns body data from trigger payload", async () => {
    const { result } = await invokeFlow(basicFlow, {});
    // defaultTriggerPayload body.data is JSON.stringify({ foo: "bar" })
    expect(result).toMatchObject({ data: JSON.stringify({ foo: "bar" }) });
  });

  it("supports mocking trigger payload", async () => {
    const testPayload = { hello: "world" };
    const { result } = await invokeFlow(basicFlow, {
      payload: { body: { data: testPayload } },
    });
    expect(result).toMatchObject({ data: testPayload });
  });
});

createMockContextComponents

createMockContextComponents builds a mock components object for use in flow tests. It reads examplePayload from the component manifest to generate default mock responses, and accepts override functions for specific actions.
import { createMockContextComponents } from "@prismatic-io/spectral/dist/testing";

const components = createMockContextComponents(
  // Component registry: keys are component names, values have an `actions` map
  { myComponent: myComponentManifest },
  // Optional overrides for specific actions
  {
    actions: {
      myComponent: {
        myAction: () => Promise.resolve({ data: "custom mock" }),
      },
    },
  },
);