Giter Club home page Giter Club logo

stepfunctions's Introduction

stepfunctions

Stepfunctions codecov

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.

Installation

npm i -D stepfunctions

or if you're using yarn like me:

yarn add -D stepfunctions

Motivation

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.

Usecase

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

Usage

Include it in your test files, tested with Jest so far.

const Sfn = require('stepfunctions');

const sm = new Sfn({
  StateMachine: {
    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);
    await sm.startExecution({ test: 1 });
    expect(mockfn).toHaveBeenCalled();
  });
});

You can see more examples in /test/stepfunctions.test.js.

API

startExecution(Input, Options);

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.

bindTaskResource(Task, Callback)

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.

getExecutionResult()

Must be called after startExecution. This function returns the absolute result from the statemachine if it has finished.

getReport()

Use console.table to list down the transitions that occured.

Task.abort()

abort is made available within the replaced Task handlers made with bindTaskResource. this allows you to abort a call from within a handler itself.

Support

The spec implemented in https://states-language.net/spec.html is fully supported by this library besides the ones below:

Experimental

  • Retry
  • Catch

The above features are labeled experimental because it cannot be fully spec compliant(yet) due to AWS specific cases.

Caveats

  1. Wait will 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 the respectTime option to true in the startExecution method.
  2. No support for Handling States.Permissions as the library will not have context on AWS related permissions.

Future

PR's are welcome to help finish the ones below :)

  • Change arn in bindTaskResource instead of the State name
  • Run sls invoke local instead 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()

License

stepfunctions is MIT Licensed

stepfunctions's People

Contributors

jamoy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

kovihq

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.