npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sfn-sim

v1.0.0

Published

AWS Step Functions simulator for unit testing state machines

Downloads

70

Readme

sfn-sim: Step Function simulator

This library simulates the AWS Step Functions runtime to unit test state machines, a lightweight alternative to integration testing with LocalStack.

Installation

npm install --save-dev sfn-sim

Usage

Import the load function, load your state machine, then execute with some input:

import { load } from 'sfn-sim';

const stateMachine = load(definition, resources, options);

await stateMachine.execute(input);

See below for details on these parameters.

definition

This must be a JSON object which is the Definition property of a state machine, which contains the StartAt and States fields at its root.

By default, your definition will be validated using statelint; this can be disabled with the validateDefinition option set to false.

Note that CloudFormation functions and refs are not supported; you should replace these in your definition before loading it.

resources

This should be an array of objects which are AWS resources used by any Task steps in your state machine. Each object must contain service and name fields, and additional fields depending on the service.

The supported service values are listed below with their requirements, as well as an example using the Lambda and S3 services.

lambda

This resource must contain a function field which must be a function. This will be executed as your lambda handler.

s3

This resource must contain an objects field which must be an array. This can optionally be pre-populated with objects, which must contain key and body fields.

sns

This resource must contain a messages field which must be an array.

sqs

This resource must contain a messages field which must be an array.

stepFunctions

This resource must contain a stateMachine field which must be a function. This will be executed as your state machine.

Resources example

const definition = {
  StartAt: 'CalculateSquare',
  States: {
    CalculateSquare: {
      Type: 'Task',
      Resource: 'arn:aws:lambda:::function:calculate-square',
      InputPath: '$.value',
      ResultPath: '$.value',
      Next: 'SaveToS3',
    },
    SaveToS3: {
      Type: 'Task',
      Resource: 'arn:aws:states:::aws-sdk:s3:putObject',
      Parameters: {
        Bucket: 'squared-numbers',
        'Key.$': '$.number',
        'Body.$': '$.value',
      },
      End: true,
    },
  },
};

const bucketObjects = [];
const resources = [
  {
    service: 'lambda',
    name: 'calculate-square',
    function: (x) => x * x,
  },
  {
    service: 's3',
    name: 'squared-numbers',
    objects: bucketObjects,
  },
];

const stateMachine = load(definition, resources);

test('writes a squared number to S3', async () => {
  await stateMachine.execute({
    number: 'three',
    value: 3,
  });

  expect(bucketObjects).toContainEqual({
    key: 'three',
    body: 9,
  });
});

options

This should be an object which can be used to override the default configuration of the simulator.

| Key | Description | Default | | --- | ----------- | ------- | | validateDefinition | Whether the provided definition should be validated on load | true | | simulateWait | Whether any Wait steps should wait in real-time, otherwise passing immediately | false | | stateMachineName | Identifier for the state machine, passed to the context object | undefined | | executionName | Identifier for the execution, passed to the context object | undefined |

Notes

This library supports most available features of Step Functions. Some functionality has not been implemented yet, including:

  • Some AWS resources in Task steps
  • Some runtime error handling and data validation