From 0558de3e5868ab1c2964162f0c1939f201558167 Mon Sep 17 00:00:00 2001 From: joel-aleios Date: Tue, 25 Jan 2022 17:14:47 +0000 Subject: [PATCH 1/2] Added assertion for expected state machine output. --- src/assertions/index.ts | 4 +- .../toMatchStateMachineOutput/index.ts | 57 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/assertions/toMatchStateMachineOutput/index.ts diff --git a/src/assertions/index.ts b/src/assertions/index.ts index ebb52ae..7f75567 100644 --- a/src/assertions/index.ts +++ b/src/assertions/index.ts @@ -6,6 +6,7 @@ import toHaveEventWithSource from "./toHaveEventWithSource"; import toHaveObjectWithNameEqualTo from "./toHaveObjectWithNameEqualTo"; import toExistInDynamoTable from "./toExistInDynamoTable"; import toHaveCompletedExecutionWithStatus from "./toHaveCompletedExecutionWithStatus"; +import toMatchStateMachineOutput from "./toMatchStateMachineOutput"; export default { ...toExistAsS3Bucket, @@ -15,5 +16,6 @@ export default { ...toHaveEventWithSource, ...toHaveObjectWithNameEqualTo, ...toExistInDynamoTable, - ...toHaveCompletedExecutionWithStatus + ...toHaveCompletedExecutionWithStatus, + ...toMatchStateMachineOutput, }; diff --git a/src/assertions/toMatchStateMachineOutput/index.ts b/src/assertions/toMatchStateMachineOutput/index.ts new file mode 100644 index 0000000..bb4dc49 --- /dev/null +++ b/src/assertions/toMatchStateMachineOutput/index.ts @@ -0,0 +1,57 @@ +import { testResult, TestResultOutput } from "utils/testResult"; +import { StepFunctions as AWSStepFunctions } from "aws-sdk"; + +export default { + async toMatchStateMachineOutput( + stateMachineName: string, + expectedOutput: any + ): Promise { + const stepFunctions = new AWSStepFunctions(); + const allStateMachines = await stepFunctions.listStateMachines().promise(); + + const smList = allStateMachines.stateMachines.filter( + (stateMachine: any) => stateMachine.name === stateMachineName + ); + + const stateMachineArn = smList[0].stateMachineArn; + const listExecParams = { stateMachineArn: stateMachineArn }; + const executionList = await stepFunctions + .listExecutions(listExecParams) + .promise(); + + const executionResult = await stepFunctions + .describeExecution({ + executionArn: executionList.executions[0].executionArn, + }) + .promise(); + + // if (allStateMachines === undefined) { + // throw new Error( + // "The list of state machines is undefined. You might have forgotten to run build()." + // ); + // } + + if (executionResult.status === "SUCCEEDED") { + if (executionResult.output === expectedOutput) { + return testResult( + `Output is ${JSON.stringify(executionResult.output)} as expected`, + true + ); + } else { + return testResult( + `Expected output was ${JSON.stringify( + expectedOutput + )}, but output received was ${JSON.stringify( + executionResult.output + )}`, + false + ); + } + } + + return testResult( + "Step Function execution failed. Cannot verify output for failed executions.", + false + ); + }, +}; From 11d4e30c33090dcb8c6ad94e7256887a49c98eb9 Mon Sep 17 00:00:00 2001 From: joel-aleios Date: Thu, 27 Jan 2022 09:56:37 +0000 Subject: [PATCH 2/2] cleaned up commented out code. --- src/assertions/toMatchStateMachineOutput/index.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/assertions/toMatchStateMachineOutput/index.ts b/src/assertions/toMatchStateMachineOutput/index.ts index bb4dc49..56c4041 100644 --- a/src/assertions/toMatchStateMachineOutput/index.ts +++ b/src/assertions/toMatchStateMachineOutput/index.ts @@ -25,12 +25,6 @@ export default { }) .promise(); - // if (allStateMachines === undefined) { - // throw new Error( - // "The list of state machines is undefined. You might have forgotten to run build()." - // ); - // } - if (executionResult.status === "SUCCEEDED") { if (executionResult.output === expectedOutput) { return testResult(