diff --git a/README.md b/README.md index 802be49..f3fb22e 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,13 @@ Note: these async assertions require "await" where CONTENT_TYPE are [standards MIME types](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types) +``` + await expect({ + bucketName: "BUCKET_NAME", + objectName: "FILE NAME", + }).toHaveContentEqualTo("CONTENT"); +``` + ## Helpers ### General diff --git a/src/assertions/toExistAsS3Bucket/index.js b/src/assertions/toExistAsS3Bucket/index.js index eac65af..c2bbd18 100644 --- a/src/assertions/toExistAsS3Bucket/index.js +++ b/src/assertions/toExistAsS3Bucket/index.js @@ -1,4 +1,5 @@ import { AWSClient } from "../../helpers/general"; +import { testResult } from "../../utils/testResult"; export default { async toExistAsS3Bucket(bucketName) { @@ -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; }, }; diff --git a/src/assertions/toHaveContentEqualTo/index.js b/src/assertions/toHaveContentEqualTo/index.js new file mode 100644 index 0000000..cd2762c --- /dev/null +++ b/src/assertions/toHaveContentEqualTo/index.js @@ -0,0 +1,33 @@ +import { AWSClient } from "../../helpers/general"; +import { testResult } from "../../utils/testResult"; + +export default { + async toHaveContentEqualTo({ bucketName, objectName }, content) { + const s3 = new AWSClient.S3(); + const params = { + Bucket: bucketName, + Key: objectName, + }; + + let message; + try { + const object = await s3.getObject(params).promise(); + if (JSON.stringify(object.Body) === JSON.stringify(content)) { + message = `expected ${objectName} to have content ${content}`; + return testResult(message, true); + } + message = `expected ${objectName} to have content ${content}, but content found was ${object.Body}`; + return testResult(message, false); + } catch (error) { + if (error.code === "NoSuchKey") { + message = `expected ${bucketName} to have object with name ${objectName} - not found`; + return testResult(message, false); + } + if (error.code === "NoSuchBucket") { + message = `expected ${bucketName} to exist - not found`; + return testResult(message, false); + } + throw error; + } + }, +}; diff --git a/src/assertions/toHaveEvent/index.js b/src/assertions/toHaveEvent/index.js index 5aa2d51..37bfd28 100644 --- a/src/assertions/toHaveEvent/index.js +++ b/src/assertions/toHaveEvent/index.js @@ -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); }, }; diff --git a/src/assertions/toHaveObjectWithNameEqualTo/index.js b/src/assertions/toHaveObjectWithNameEqualTo/index.js index 38ecbab..82f7d7c 100644 --- a/src/assertions/toHaveObjectWithNameEqualTo/index.js +++ b/src/assertions/toHaveObjectWithNameEqualTo/index.js @@ -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) { @@ -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; }, };