AWS Step Functions implementation in Node, so you can run your Node.js lambda handlers in your test environments. Made to support Serverless JS testing.
pnpm add -D stepfunctions
or with npm / yarn:
npm i -D stepfunctions
yarn add -D stepfunctions
This repository itself is developed with pnpm (
pnpm install,pnpm test,pnpm lint).
I was working on getting step functions orchestrated using Serverless, Lambda, and Step functions and there was no way to run through the statemachine in Jest. So I made the spec, or parts of it, work in JS so that I can spy and mock the statemachine.
I am perfectly aware of the existence of step-functions-offline and local-stepfunctions, but none of those can be orchestrated natively in a testing context.
If you are:
- using Node, Lambda, AWS Step Functions
- using Serverless
- writing Integration tests with AWS Step Functions
- trying to see how the statemachine runs before creating it in AWS
The package ships both ESM and CommonJS entry points with TypeScript types, so use whichever you prefer:
// ESM
import Sfn from 'stepfunctions';
// or: import { StepFunction } from 'stepfunctions';
// CommonJS
const Sfn = require('stepfunctions');Include it in your test files (tested with Jest so far). You can pass a bare Amazon States Language definition straight to the constructor, and startExecution resolves to the final output:
const Sfn = require('stepfunctions');
const sm = new Sfn({
StartAt: 'Test',
States: {
Test: {
Type: 'Task',
Resource: 'arn:aws:lambda:ap-southeast-1:123456789012:function:test',
End: true,
},
},
});
describe('StateMachine Test', () => {
it('Check if a task was run', async () => {
const mockfn = jest.fn((input) => input.test === 1);
sm.bindTaskResource('Test', mockfn);
const result = await sm.startExecution({ test: 1 });
expect(mockfn).toHaveBeenCalled();
expect(result).toBe(true);
});
});The new Sfn({ StateMachine }) form is still supported for backwards compatibility, as is calling getExecutionResult() after startExecution (it is now idempotent — it no longer consumes the result).
You can see more examples in /test/stepfunctions.test.js.
Resolves to the final output of the execution.
const output = await sm.startExecution(input, {
respectTime: false,
maxWaitTime: 30,
maxConcurrency: 10,
});- respectTime - will ensure that the time used in Wait steps will be respected and not use the maximum wait time in the library. defaults to false.
- maxWaitTime - the maximum amount of time a wait step can function. defaults to 30s.
- maxConcurrency - allows the amount of parallel tasks to be ran concurrently. defaults to 10.
const sm = new Sfn({
StateMachine: {
StartAt: 'HelloWorld',
States: {
HelloWorld: {
Type: 'Task',
Resource: 'arn:aws:lambda:ap-southeast-1:123456789012:function:test',
End: true,
},
},
},
});
sm.bindTaskResource('HelloWorld', (input) => `hello ${input}`);
await sm.startExecution('world');
// will output `hello world`Must be called before startExecution, binds to Tasks and replaces their handler to the provided Callback parameter.
Must be called after startExecution. This function returns the absolute result from the statemachine if it has finished. It is idempotent — repeated calls return the same value. (startExecution also resolves to this value, so you often don't need it.)
Use console.table to list down the transitions that occured.
abort is made available within the replaced Task handlers made with bindTaskResource. this allows you to abort a call
from within a handler itself.
Node.js >= 20. The library is tested on Node 20, 24, and 26.
The spec implemented in https://states-language.net/spec.html is supported by this library, including the newer (post-2020) JSONPath additions:
- Intrinsic functions inside
Parameters,ItemSelectorandResultSelectorpayload templates:States.Format,States.StringToJson,States.JsonToString,States.Array,States.ArrayPartition,States.ArrayContains,States.ArrayRange,States.ArrayGetItem,States.ArrayLength,States.ArrayUnique,States.Base64Encode,States.Base64Decode,States.Hash,States.JsonMerge,States.MathRandom,States.MathAdd,States.StringSplitandStates.UUID(including nested calls). ChoiceoperatorsStringMatches,IsNull,IsPresent,IsBoolean,IsNumeric,IsString,IsTimestamp, and all the*Pathcomparison variants (e.g.NumericGreaterThanPath,StringEqualsPath).ResultSelectoronTask,MapandParallel(applied beforeResultPathandOutputPath).MapacceptsItemProcessor/ItemSelectoras aliases ofIterator/Parameters, and honoursMaxConcurrency,ItemBatcher(MaxItemsPerBatch,BatchInput) and theToleratedFailureCount/ToleratedFailurePercentagethresholds.
Set QueryLanguage: "JSONata" on the state machine or an individual state. {% … %} expressions are evaluated with jsonata, with the $states object ($states.input, $states.result, $states.context) and any assigned variables bound. Supported on Pass (Output), Task (Arguments, Output), Choice (Condition), Map (Items, ItemSelector, Output) and Parallel (Output).
Assign is supported in both JSONPath and JSONata modes. Assigned variables are referenced as $name in later states and follow AWS scoping: a Map/Parallel child can read outer variables but its own assignments do not leak back to the parent scope.
Experimental
- Retry
- Catch
The above features are labeled experimental because it cannot be fully spec compliant(yet) due to AWS specific cases.
Not yet supported
- JSONata on
Wait/Succeed/Fail, and the$states.errorOutputbinding inside aCatch. - Distributed
Mapinfrastructure (ItemReader,ResultWriter);ItemProcessoralways runs inline.
Waitwill wait for at most 30 seconds. This is because it's expected that this library will be used within a testing context. You can override this behaviour by adding therespectTimeoption to true in thestartExecutionmethod.- No support for Handling
States.Permissionsas the library will not have context on AWS related permissions.
PR's are welcome to help finish the ones below :)
- Change arn in bindTaskResource instead of the State name
- Run
sls invoke localinstead of binding resolvers - Typescript typings
- Run via CLI
- Remove the "experimental" label on retry and catch
- More accurate timing mechanism
- use
jest.fakeTimers()in the test - Walk through states ala "generator" style. e.g,
yield sm.next() - Support the JSONata query language and Variables (
Assign)
stepfunctions is MIT Licensed