Custom Jest Assertions for Serverless integration testing.
Custom Jest Assertions for Serverless integration testing.
sls-test-tools provides a range of utilities, setup, teardown and assertions to make it easier to write effective and high quality integration tests for Serverless Architectures on AWS.
** This is in an alpha state while we trial a few initial assertions and get feedback on the approach and structure. **
⚠️ AWS resources will be created (SQS Queue, EventBridge Rule...) if the EventBridge module is used. Although there is clear setup and teardown we do not advise running this on production environments currently. ⚠️
With npm:
npm install --save-dev sls-test-toolsWith yarn:
yarn add -D sls-test-toolssls-test-tools is currently being actively maintained, yet is in alpha. Your feedback is very welcome.
expect(eventBridgeEvents).toHaveEvent();
expect(eventBridgeEvents).toHaveEventWithSource("order.created");Note: these async assertions require "await"
await expect("BUCKET NAME").toHaveS3ObjectWithNameEqualTo("FILE NAME"); await expect("BUCKET NAME").toExistAsS3Bucket(); await expect({
bucketName: "BUCKET_NAME",
objectName: "FILE NAME",
}).toHaveContentTypeEqualTo("CONTENT_TYPE");;where CONTENT_TYPE are standards MIME types
await expect({
bucketName: "BUCKET_NAME",
objectName: "FILE NAME",
}).toHaveContentEqualTo("CONTENT");Note: these assertions also require "await"
await expect("STATE_MACHINE_NAME").toHaveCompletedExecutionWithStatus("STATUS");
await expect("STATE_MACHINE_NAME").toMatchStateMachineOutput({EXPECTED_OUTPUT}));Note: these assertions also require "await"
await expect("TABLENAME").toContainItemWithValues({[field]: value});
await expect({PK: pk,
SK: sk,
}).toExistInDynamoTable('TABLENAME');Note: this assertion also requires "await"
await expect('USER_POOL_ID').toContainUser('USERNAME');AWSClient - An AWS client with credentials set up
getStackResources(stackName) - get information about a stack
getOptions() - get options for making requests to AWSAn interface to the deployed EventBridge, allowing events to be injected and intercepted via an SQS queue and EventBridge rule.
EventBridge.build(busName) - create a EventBridge instance to allow events to be injected and intercepted eventBridge.publishEvent(source, detailType, detail, clear?) - publish an event to the bus
eventBridge.getEvents(clear?) - get the events that have been sent to the bus
eventBridge.clear() - clear old messages
eventBridge.destroy() - remove infastructure used to track eventsAn interface to a deployed Step Function, with a function to execute a Step Function until its completion.
StepFunctions.build() // create a Step Functions Client for executing existing state machines stepFunctions.runExecution(stateMachineName, input) // executes state machine until completion…jestjest using sls-test-tools matchers there are certain parameters needed for sls-test-tools to make assertions.jests convention on test arguments, or by using environment variables. CLI arguments override environment variables.Required
'--stack=my-service-dev' or process.env.CFN_STACK_NAME - the CloudFormation stack name of the stack under test.Optional
'--profile=[PROFILE NAME]' or process.env.AWS_PROFILE (will default to default)
'--region=[AWS Region]' or process.env.AWS_REGION (will default to eu-west-2)
'--keep=true' or process.env.SLS_TEST_TOOLS_KEEP (will default to false) - keeps testing resources up to avoid creation throttles (e.g. SQS Queue created for EventBridge assertions)
'--event-rule-name=[Event Bus Rule Name]' - Custom Event bridge Rule name (will deafult to test-${eventBridgeName}-rule)
'--queue-name=[SQS Queue Name]' - Custom SQS Queue name (will deafult to ${eventBridgeName}-testing-queue)
To avoid issues we recommend --runInBand
…Thanks goes to these wonderful people (emoji key):
Ben Ellerby
Sarah Hamilton
Alexander White
Joel Hamilton
This project follows the all-contributors specification. Contributions of any kind welcome!
sls-test-tools provides a range of utilities, setup, teardown and assertions to make it easier to write effective and high quality integration tests for Serverless Architectures on AWS.
** This is in an alpha state while we trial a few initial assertions and get feedback on the approach and structure. **
⚠️ AWS resources will be created (SQS Queue, EventBridge Rule...) if the EventBridge module is used. Although there is clear setup and teardown we do not advise running this on production environments currently. ⚠️
With npm:
npm install --save-dev sls-test-toolsWith yarn:
yarn add -D sls-test-toolssls-test-tools is currently being actively maintained, yet is in alpha. Your feedback is very welcome.
expect(eventBridgeEvents).toHaveEvent();
expect(eventBridgeEvents).toHaveEventWithSource("order.created");Note: these async assertions require "await"
await expect("BUCKET NAME").toHaveS3ObjectWithNameEqualTo("FILE NAME"); await expect("BUCKET NAME").toExistAsS3Bucket(); await expect({
bucketName: "BUCKET_NAME",
objectName: "FILE NAME",
}).toHaveContentTypeEqualTo("CONTENT_TYPE");;where CONTENT_TYPE are standards MIME types
await expect({
bucketName: "BUCKET_NAME",
objectName: "FILE NAME",
}).toHaveContentEqualTo("CONTENT");Note: these assertions also require "await"
await expect("STATE_MACHINE_NAME").toHaveCompletedExecutionWithStatus("STATUS");
await expect("STATE_MACHINE_NAME").toMatchStateMachineOutput({EXPECTED_OUTPUT}));Note: these assertions also require "await"
await expect("TABLENAME").toContainItemWithValues({[field]: value});
await expect({PK: pk,
SK: sk,
}).toExistInDynamoTable('TABLENAME');Note: this assertion also requires "await"
await expect('USER_POOL_ID').toContainUser('USERNAME');AWSClient - An AWS client with credentials set up
getStackResources(stackName) - get information about a stack
getOptions() - get options for making requests to AWSAn interface to the deployed EventBridge, allowing events to be injected and intercepted via an SQS queue and EventBridge rule.
EventBridge.build(busName) - create a EventBridge instance to allow events to be injected and intercepted eventBridge.publishEvent(source, detailType, detail) - publish an event to the bus
eventBridge.getEvents() - get the events that have been sent to the bus
eventBridge.clear() - clear old messages
eventBridge.destroy() - remove infastructure used to track eventsAn interface to a deployed Step Function, with a function to execute a Step Function until its completion.
StepFunctions.build() // create a Step Functions Client for executing existing state machines stepFunctions.runExecution(stateMachineName, input) // executes state machine until completion await createAuthenticatedUser({
clientId: "CLIENT_ID",
userPoolId: "USER_POOL_ID",
standardAttributes: ["email", "middle_name"], // works for all cognito standard user attributes
customAttributes: ["hello"], // only works for customAttributes which have been explicitly defined in the user pool schema
});
await createUnauthenticatedUser({
clientId: "CLIENT_ID",
userPoolId: "USER_POOL_ID",
confirmed: true,
standardAttributes: ["email", "middle_name", "address", "birthdate"], // works for all cognito standard user attributes
});
jestjest using sls-test-tools matchers there are certain parameters needed for sls-test-tools to make assertions.jests convention on test arguments, or by using environment variables. CLI arguments override environment variables.Required
'--stack=my-service-dev' or process.env.CFN_STACK_NAME - the CloudFormation stack name of the stack under test.Optional
'--profile=[PROFILE NAME]' or process.env.AWS_PROFILE (will default to default)
'--region=[AWS Region]' or process.env.AWS_REGION (will default to eu-west-2)
'--keep=true' - keeps testing resources up to avoid creation throttles (e.g. SQS Queue created for EventBridge assertions)
To avoid issues we recommend --runInBand
…Thanks goes to these wonderful people (emoji key):
Ben Ellerby
Sarah Hamilton
Alex White
This project follows the all-contributors specification. Contributions of any kind welcome!
No open issues yet, or sync has not completed.