@prismatic-io/spectral package exports a testing module with utilities for writing unit tests against your custom component actions, triggers, data sources, and code-native integration flows. Tests run in any Jest-compatible environment, including Vitest.
Two testing approaches
Spectral provides two complementary APIs for testing:| API | Best for |
|---|---|
Standalone invoke* functions | Testing individual action, trigger, or data source definitions directly |
createHarness() | Testing a complete assembled component with multiple actions, triggers, and data sources |
Setting up your test environment
Install a test runner
Spectral’s testing utilities are compatible with any Jest-compatible runner. Vitest is recommended for new projects.Add a test script to your
- Vitest
- Jest
package.json:package.json
Import testing utilities
Import the testing helpers you need from the
testing sub-module:my-component.test.ts
Core testing functions
invoke(action, params, context?)
Invokes an ActionDefinition’s perform function directly. Returns { result, loggerMock }.
invokeTrigger(trigger, context?, payload?, params?)
Invokes a TriggerDefinition’s perform function. Automatically merges a default trigger payload so you only need to supply fields you care about.
invokeDataSource(dataSource, params, context?)
Invokes a DataSourceDefinition’s perform function and returns its result directly.
invokeFlow(flow, options?)
Invokes a code-native integration Flow, running both the trigger (onTrigger) and execution (onExecution) functions. Accepts configVars, context, and payload options.
createHarness(component)
Wraps a complete Component definition in a ComponentTestHarness that provides .action(), .trigger(), .dataSource(), and .connectionValue() methods. Input defaults and clean functions are applied automatically.
Connection helpers
createConnection(definition, values, tokenValues?, displayName?)
Builds a ConnectionValue from a connection definition and plain field values, suitable for passing as an action parameter in tests.
connectionValue(envVarKey?)
Reads a ConnectionValue from an environment variable (defaults to PRISMATIC_CONNECTION_VALUE). Useful for CI pipelines where credentials should not be committed to source.
loggerMock()
Creates a mock ActionLogger where each log method (trace, debug, info, log, warn, error) is a spy. The mock logger is automatically attached to any context created by invoke* functions and is also returned in the InvokeReturn object so you can assert on it directly.
invoke() or invokeTrigger(), the logger mock is returned alongside the result:
createMockContextComponents(registry, mocks?)
Generates a mock context.components object from a CNI component manifest registry. By default it returns each action’s examplePayload. Pass mocks.actions to override specific actions with custom implementations.
Related pages
Unit testing actions
Test action
perform functions with invoke() or the harness .action() method.Unit testing triggers
Test trigger
perform, onInstanceDeploy, and onInstanceDelete with invokeTrigger().Unit testing flows
Test code-native integration flows end-to-end with
invokeFlow().