Skip to content

Commit

Permalink
Refactor assertions to use testResult
Browse files Browse the repository at this point in the history
  • Loading branch information
Sarah Hamilton committed Jul 26, 2021
1 parent a69a1f2 commit c6690b8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 39 deletions.
21 changes: 7 additions & 14 deletions src/assertions/toExistAsS3Bucket/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AWSClient } from "../../helpers/general";
import { testResult } from "../../utils/testResult";

export default {
async toExistAsS3Bucket(bucketName) {
Expand All @@ -7,25 +8,17 @@ export default {
Bucket: bucketName,
};

let testResult;
let message;
try {
await s3.headBucket(params).promise();
testResult = {
message: () => `expected S3 bucket to exist with name ${bucketName}`,
pass: true,
};
message = `expected S3 bucket to exist with name ${bucketName}`;
return testResult(message, true);
} catch (error) {
if (error.statusCode === 404) {
testResult = {
message: () =>
`expected S3 bucket to exist with name ${bucketName} - not found`,
pass: false,
};
} else {
throw error;
message = `expected S3 bucket to exist with name ${bucketName} - not found`;
return testResult(message, false);
}
throw error;
}

return testResult;
},
};
15 changes: 7 additions & 8 deletions src/assertions/toHaveEvent/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { testResult } from "../../utils/testResult";

export default {
toHaveEvent(eventBridgeEvents) {
let message;
if (eventBridgeEvents) {
return {
message: () => "expected to have message in EventBridge Bus",
pass: true,
};
message = "expected to have message in EventBridge Bus";
return testResult(message, true);
}
return {
message: () => "no message intercepted from EventBridge Bus",
pass: false,
};
message = "no message intercepted from EventBridge Bus";
return testResult(message, false);
},
};
26 changes: 9 additions & 17 deletions src/assertions/toHaveObjectWithNameEqualTo/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AWSClient } from '../../helpers/general';
import { AWSClient } from "../../helpers/general";
import { testResult } from "../../utils/testResult";

export default {
async toHaveS3ObjectWithNameEqualTo(bucketName, objectName) {
Expand All @@ -8,26 +9,17 @@ export default {
Key: objectName,
};

let testResult;
let message;
try {
await s3.getObject(params).promise();
testResult = {
message: () =>
`expected ${bucketName} to have object with name ${objectName}`,
pass: true,
};
message = `expected ${bucketName} to have object with name ${objectName}`;
return testResult(message, true);
} catch (error) {
if (error.code === 'NoSuchKey') {
testResult = {
message: () =>
`expected ${bucketName} to have object with name ${objectName} - not found`,
pass: false,
};
} else {
throw error;
if (error.code === "NoSuchKey") {
message = `expected ${bucketName} to have object with name ${objectName} - not found`;
return testResult(message, false);
}
throw error;
}

return testResult;
},
};

0 comments on commit c6690b8

Please sign in to comment.