Skip to main content

Import

import { invokeDataSource } from "@prismatic-io/spectral/dist/testing";

Signature

export const invokeDataSource = async <
  TInputs extends Inputs,
  TConfigVars extends ConfigVarResultCollection,
  TDataSourceType extends DataSourceType,
>(
  { perform }: DataSourceDefinition<TInputs, TConfigVars, TDataSourceType>,
  params: ActionInputParameters<TInputs>,
  context?: Partial<DataSourceContext<TConfigVars>>,
): Promise<DataSourceResult<TDataSourceType>>
invokeDataSource calls the perform function of a DataSourceDefinition with a mock DataSourceContext and the params you provide. Unlike invoke, it returns the DataSourceResult directly (not wrapped in an InvokeReturn).
Data source contexts are slightly different from action contexts — they do not include stepId, executionId, webhookUrls, or other execution-specific fields. invokeDataSource constructs the correct context type automatically.

Parameters

dataSource
DataSourceDefinition
required
The data source definition object to invoke. Only the perform function is used.
params
ActionInputParameters<TInputs>
required
An object containing the input parameter values for the data source. Keys must match the input keys defined on the data source.
context
Partial<DataSourceContext<TConfigVars>>
Optional partial data source context. Overrides the default mock context.

Return value

type DataSourceResult<TDataSourceType extends DataSourceType> = ...
// e.g. { result: string } for dataSourceType: "string"
// e.g. { result: string[] } for dataSourceType: "picklist"
result
varies by dataSourceType
required
The value returned by the data source’s perform function. The shape depends on the dataSourceType declared on the definition.

Examples

import { dataSource, input } from "@prismatic-io/spectral";
import { invokeDataSource } from "@prismatic-io/spectral/dist/testing";
import { describe, expect, it } from "vitest";

const listRegionsDataSource = dataSource({
  display: { label: "List Regions", description: "Returns available regions" },
  inputs: {
    apiKey: input({ label: "API Key", type: "string" }),
  },
  dataSourceType: "picklist",
  perform: async (context, { apiKey }) => {
    // In a real component, you'd call an API here
    return { result: ["us-east-1", "us-west-2", "eu-west-1"] };
  },
});

describe("listRegionsDataSource", () => {
  it("returns a list of regions", async () => {
    const result = await invokeDataSource(listRegionsDataSource, {
      apiKey: "test-key",
    });
    expect(result.result).toContain("us-east-1");
    expect(result.result).toHaveLength(3);
  });
});
  • invoke — invoke an action definition
  • createConnection — create a test connection for use in context
  • createHarness — harness-based testing with harness.dataSource(key, params)