GithubHelp home page GithubHelp logo

oshikiri / fake-step-functions Goto Github PK

View Code? Open in Web Editor NEW
16.0 3.0 2.0 1.35 MB

An unit testing toolkit for Amazon States Language

License: Apache License 2.0

TypeScript 100.00%
amazon-states-language aws-step-functions unit-testing testing-framework

fake-step-functions's Introduction

fake-step-functions

A lightweight unit testing toolkit for Amazon States Language.

GitHub Workflows Status npm version

const { FakeStateMachine } = require('fake-step-functions');

describe('FakeStateMachine.run', () => {
  const definition = {
    Comment: 'https://states-language.net/spec.html#data',
    StartAt: 'AddNumbers',
    States: {
      AddNumbers: {
        Type: 'Task',
        Resource: 'arn:aws:lambda:us-east-1:123456789012:function:Add',
        InputPath: '$.numbers',
        ResultPath: '$.sum',
        End: true,
      },
    },
  };
  const fakeResources = {
    'arn:aws:lambda:us-east-1:123456789012:function:Add': numbers => numbers.val1 + numbers.val2,
  };
  const fakeStateMachine = new FakeStateMachine(definition, fakeResources);

  test('should execute the state machine with fakeResource', async () => {
    const runStateResult = await fakeStateMachine.run({
      title: 'Numbers to add',
      numbers: { val1: 3, val2: 4 },
    });

    expect(runStateResult.data).toEqual({
      title: 'Numbers to add',
      numbers: { val1: 3, val2: 4 },
      sum: 7,
    });
  });
});

Release

In order to release new version to npm, create a commit using npm version patch -m "Release %s" and push it. See the npm-publish-action step in .github/workflows/publish.yml.

References

Specifications

Similar projects

At 2022, AWS Step Functions Local is a primary choice to test ASL locally. See "Testing Step Functions State Machines Locally".

fake-step-functions's People

Contributors

dependabot[bot] avatar liamriddell avatar oshikiri avatar sumlbel avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

fake-step-functions's Issues

Add executed states to field for validating execution path in tests.

User Story

We have quite complicated StepFunctions which have many branching choices. A nice feature that I saw with one of the other libraries was an execution path of the states. This allows us to test for the execution path and not just the end state.

Crude Implementation

Reading the code, I think that this could be crudely implemented into the runState function where by before the Switch statement we have an array property which we push the state name into each time the function is called.

image

Use Case

    test('should not throw an Error', async () => {
      const definition = require('./fixtures/definitions/pass-state-undefined-resultpath.json');
      const fakeStateMachine = new FakeStateMachine(definition, {});

      await fakeStateMachine.run({
        changed: 'TBD',
        unchanged: 'unchanged',
      });

      expect(fakeStateMachine.executionPath).toEqual([
        "Pass0",
        "Choice1",
        "Branch2",
        "Done"
      ]);
    });

[Question] Intrinsic Function

Hi.

I have a State Machine with an intrinsic function, e.g. serviceUrl.$": "States.Format('https://service.{}.domain.com', $.meta.stage)"

This gives a

Lexical error on line 1. Unrecognized text.
    States.Format('https://service

when executing the test

Are there any plans to support them?

Support for complex Choices rules

This is a great lib.
It would be great to add complex Choices rules support (And/Or/Not/...Path ones) though.
I can assist with that.

[Question] Retry/Catch

Hi there.

I love how simple it is to to mocking with this library. So much more intuitive than with the official testing approach for Step Functions.

Now I have a StateMachine Definition that has a Catch in it (more of a hack for a http:invoke Task, but ๐Ÿคท๐Ÿผโ€โ™‚๏ธ).

"Catch": [
        {
          "ErrorEquals": ["States.Http.StatusCode.409"],
          "Comment": "Certificate Exists",
          "Next": "Success"
        }
      ]

Is there a way to test this with this library?

I guess the same applies for Retries, e.g.

"Retry": [
        {
          "ErrorEquals": [
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException",
            "Lambda.TooManyRequestsException"
          ],
          "IntervalSeconds": 1,
          "MaxAttempts": 3,
          "BackoffRate": 2
        }
      ],

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.