Skip to content

Commit

Permalink
Merge pull request #23 from aleios-cloud/feat/step-function-output-as…
Browse files Browse the repository at this point in the history
…sertion

Added assertion for expected state machine output.
  • Loading branch information
Alexander White authored Jan 27, 2022
2 parents 5f06b68 + 11d4e30 commit 588921c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/assertions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -15,5 +16,6 @@ export default {
...toHaveEventWithSource,
...toHaveObjectWithNameEqualTo,
...toExistInDynamoTable,
...toHaveCompletedExecutionWithStatus
...toHaveCompletedExecutionWithStatus,
...toMatchStateMachineOutput,
};
51 changes: 51 additions & 0 deletions src/assertions/toMatchStateMachineOutput/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { testResult, TestResultOutput } from "utils/testResult";
import { StepFunctions as AWSStepFunctions } from "aws-sdk";

export default {
async toMatchStateMachineOutput(
stateMachineName: string,
expectedOutput: any
): Promise<TestResultOutput> {
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 (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
);
},
};

0 comments on commit 588921c

Please sign in to comment.