Testing Step Functions: how to skip time when testing Timeout and Wait states
Tags: aws, programming, serverless, stepfunctions
Published: 2023-06-19 17:54:54

---
title: Testing Step Functions: how to skip time when testing Timeout and Wait states
published: true
date: 2023-06-19 10:34:50 UTC
tags: AWS,Programming,Serverless,StepFunctions
canonical_url: https://theburningmonk.com/2023/06/testing-step-functions-how-to-skip-time-when-testing-timeout-and-wait-states/
---

When I [previously wrote](https://dev.to/theburningmonk/a-practical-guide-to-testing-aws-step-functions-4jla-temp-slug-905612) about testing Step Functions, I gave you a general strategy that consists of:

- Component tests that target the Lambda functions (specifically, the custom code you wrote in those functions).
- End-to-end tests that execute the state machine in the cloud.
- Local tests using [Step Functions Local](https://docs.aws.amazon.com/step-functions/latest/dg/sfn-local.html) where you can use mocks to help you test those hard-to-reach execution paths.

However, there’s one common problem that Step Functions Local won’t help you with—dealing with time. E.g. when you need to test an execution path behind a long wait state or an error path that is behind a long Timeout clause.

![](https://theburningmonk.com/wp-content/uploads/2023/06/img_64902c520c7b3.png)

Because Step Functions Local doesn’t support skipping forward in time, I find the best solution is to rewrite the state machine definition in the test setup.

Let’s say you have a state machine for processing food orders, like this:

![](https://theburningmonk.com/wp-content/uploads/2023/06/img_64902c94a4325.png)

The “Notify restaurant” state has a timeout of 300 seconds and we want to test this error path.

![](https://theburningmonk.com/wp-content/uploads/2023/06/img_64902ee06515e.png)

I would write a test case like this:

```
const given = require('../../steps/given')
const when = require('../../steps/when')
const then = require('../../steps/then')
const chance = require('chance').Chance()
const retry = require('async-retry')

describe("Test case: restaurant doesn't respond to the order in time", () => {
  const orderId = chance.guid()

  describe('Given a local instance of the state machine', () => {
    let stateMachineArn

    beforeAll(async () => {
      stateMachineArn = await given.a_local_statemachine_instance(
        process.env.StateMachineArn,
        chance.guid(),
        (definitionJson) => {
          const definition = JSON.parse(definitionJson)
          definition.States['Notify restaurant'].TimeoutSeconds = 1
          return JSON.stringify(definition)
        }
      )
    })

    describe('When we start a local execution', () => {
      let executionArn
  
      beforeAll(async () => {
        executionArn = await when.we_start_local_execution(
          stateMachineArn, 
          { orderId })
      })

      it('Should add the order to the database', async () => {
        await then.an_order_exists_in_dynamodb(orderId)
      })
  
      it('Should send a SNS notification to the restaurant topic', async () => {
        const restaurantNotification = await then.a_restaurant_notification_is_received(orderId)
        expect(restaurantNotification.TaskToken).toBeTruthy()
      })

      it('Should update the order status to "NO_RESPONSE"', async () => {
        await retry(async () => {
          const order = await then.an_order_exists_in_dynamodb(orderId)
          expect(order.status).toEqual("NO_RESPONSE")
        }, {
          retries: 3,
          maxTimeout: 1000
        })
      })
    })
  })
})
```

The `given.a_local_statemachine_instance` helper function defines a state machine against Step Functions Local. But importantly, it allows me to rewrite the definition of the state machine and change the `TimeoutSeconds` setting to 1.

```
(definitionJson) => {
  const definition = JSON.parse(definitionJson)
  definition.States['Notify restaurant'].TimeoutSeconds = 1
  return JSON.stringify(definition)
}
```

This way, we only have to wait for a one-second delay (instead of 300!) before we can verify that the order’s status has been changed to `NO_RESPONSE`.

```
it('Should update the order status to "NO_RESPONSE"', async () => {
  await retry(async () => {
    const order = await then.an_order_exists_in_dynamodb(orderId)
    expect(order.status).toEqual("NO_RESPONSE")
  }, {
    retries: 3,
    maxTimeout: 1000
  })
})
```

As you can see, this approach is quite simple and lets you skip time when testing Wait states and Timeout clauses.

If you want to learn more about testing serverless architectures and see the full example in action, then check out my latest course “[Testing Serverless Architectures](https://testserverlessapps.com/?utm_campaign=testing-sfn-skip-time&utm_source=blog)”. It gives you practical advice on how to test different types of serverless architectures and deal with the specific challenges that come with them. Including API Gateway, AppSync, Step Functions and event-driven architectures.

![](https://theburningmonk.com/wp-content/uploads/2022/12/img_63a2198e0605a.png)

The post [Testing Step Functions: how to skip time when testing Timeout and Wait states](https://theburningmonk.com/2023/06/testing-step-functions-how-to-skip-time-when-testing-timeout-and-wait-states/) appeared first on [theburningmonk.com](https://theburningmonk.com).