|
1 |
| -# stepfunctions-testing |
| 1 | +# Step Functions Testing Library |
2 | 2 |
|
| 3 | +## Features |
| 4 | + |
| 5 | +- Create a [MockConfigFile](https://docs.aws.amazon.com/step-functions/latest/dg/sfn-local-test-sm-exec.html) with JavaScript |
| 6 | + |
| 7 | + |
| 8 | +### Install |
| 9 | + |
| 10 | +```sh |
| 11 | +npm i stepfunctions-testing |
| 12 | +``` |
| 13 | +## Usage |
| 14 | + |
| 15 | +### Creating a mocked response |
| 16 | + |
| 17 | +#### `Return` a response |
| 18 | + |
| 19 | +```js |
| 20 | +const checkIdentityLambdaMockedSuccess = new MockedResponse( |
| 21 | + 'CheckIdentityLambdaMockedSuccess' |
| 22 | +).return<any>({ |
| 23 | + StatusCode: 200, |
| 24 | + Payload: { |
| 25 | + statusCode: 200, |
| 26 | + body: JSON.stringify({ |
| 27 | + approved: true, |
| 28 | + message: 'identity validation passed', |
| 29 | + }), |
| 30 | + }, |
| 31 | +}); |
| 32 | +``` |
| 33 | + |
| 34 | +#### `Throw` a response |
| 35 | + |
| 36 | +```js |
| 37 | +const checkIdentityLambdaMockedThrowError = new MockedResponse( |
| 38 | + 'CheckIdentityLambdaMockedThrowError' |
| 39 | +).throw('CustomValidationError', 'Check Identity Validation Failed'); |
| 40 | +``` |
| 41 | + |
| 42 | +#### Repeating responses |
| 43 | + |
| 44 | +```js |
| 45 | +const checkIdentityLambdaMockedThrowError = new MockedResponse( |
| 46 | + 'CheckIdentityLambdaMockedThrowError' |
| 47 | +).throw('CustomValidationError', 'Check Identity Validation Failed', /* repeat the response 3 times. total becomes 4 */ 3); |
| 48 | +``` |
| 49 | + |
| 50 | +### Full example |
| 51 | + |
| 52 | +```js |
| 53 | +// Create mocked responses |
| 54 | +const checkIdentityLambdaMockedSuccess = new MockedResponse( |
| 55 | + 'CheckIdentityLambdaMockedSuccess' |
| 56 | +).return<any>({ |
| 57 | + StatusCode: 200, |
| 58 | + Payload: { |
| 59 | + statusCode: 200, |
| 60 | + body: JSON.stringify({ |
| 61 | + approved: true, |
| 62 | + message: 'identity validation passed', |
| 63 | + }), |
| 64 | + }, |
| 65 | +}); |
| 66 | + |
| 67 | +const checkAddressLambdaMockedSuccess = new MockedResponse( |
| 68 | + 'CheckAddressLambdaMockedSuccess' |
| 69 | +).return<any>({ |
| 70 | + StatusCode: 200, |
| 71 | + Payload: { |
| 72 | + statusCode: 200, |
| 73 | + body: JSON.stringify({ |
| 74 | + approved: true, |
| 75 | + message: 'address validation passed', |
| 76 | + }), |
| 77 | + }, |
| 78 | +}); |
| 79 | + |
| 80 | +// Create a state machine test definition |
| 81 | +const stateMachineTestDefinition = new StateMachineTestDefinition('SomeStateMachineName') |
| 82 | + .addTestCase( |
| 83 | + new StateMachineTestCase('HappyPathTest') |
| 84 | + .withInput(input) |
| 85 | + .addMockedState( |
| 86 | + 'CheckIdentity', |
| 87 | + checkIdentityLambdaMockedSuccess |
| 88 | + ) |
| 89 | + .addMockedState( |
| 90 | + 'CheckAddress', |
| 91 | + checkAddressLambdaMockedSuccess |
| 92 | + ) |
| 93 | + ); |
| 94 | + |
| 95 | +// Create a config |
| 96 | +const config = new StepFunctionsMockConfig() |
| 97 | + .addTestDefinition(stateMachineTestDefinition); |
| 98 | + |
| 99 | +// Convert the config object to JSON |
| 100 | +// You can write this down to a file to be used by step functions local |
| 101 | +config.toJSON(); |
| 102 | +``` |
| 103 | + |
| 104 | +## License |
| 105 | + |
| 106 | +stepfunctions-testing is available under the terms of [MIT License](./LICENSE) |
0 commit comments