Skip to content

Commit

Permalink
Added assertion for expected state machine output.
Browse files Browse the repository at this point in the history
  • Loading branch information
joelhamiltondev committed Jan 25, 2022
1 parent 21ac845 commit 0558de3
Show file tree
Hide file tree
Showing 2 changed files with 60 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,
};
57 changes: 57 additions & 0 deletions src/assertions/toMatchStateMachineOutput/index.ts
Original file line number Diff line number Diff line change
@@ -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<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 (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
);
},
};

0 comments on commit 0558de3

Please sign in to comment.