jest-e2e-serverless
Note: This library is still at POC level, if you're missing any capability please open an issue :)
Prerequisites
You should have your aws credentials under ~/.aws/credentials
(if you have aws cli installed and configured).
Note: aws credentials are loaded automatically as described here
If you plan to use the deploy utility function please install and configure serverless.
node >= 8 (for async/await
support).
Installation
Install with yarn
yarn add jest-e2e-serverless --dev
or npm
npm install jest-e2e-serverless --save-dev
Setup
The simplest setup is to use jest's setupTestFrameworkScriptFile
config.
Make sure your package.json
includes the following:
// package.json"jest": ,
Usage with TypeScript
When using jest-e2e-serverless
with TypeScript and ts-jest, you'll need to add a setupFrameworks.ts
file to your app that explicitly imports jest-e2e-serverless
, and point the setupTestFrameworkScriptFile
field in your package.json
file towards it:
// src/setupFrameworks.ts;
// package.json"jest": ,
Assertions
Notes
- The matchers use
aws-sdk
under the hood, thus they are all asynchronous and require usingasync/await
- toHaveItem()
- toHaveObject()
- toHaveLog()
- toBeAtState()
- toHaveState()
- toReturnResponse()
- toHaveRecord()
- toHaveMessage()
toHaveItem()
Asserts existence/equality of a DynamoDb item
expect; // makes sure the assertion was calledawait ;
toHaveObject()
Asserts existence/equality of a S3 object
expect; // makes sure the assertion was calledawait ;
toHaveLog()
Asserts log message of a lambda function
expect; // makes sure the assertion was calledawait ;
toBeAtState()
Asserts a state machine current state
expect; // makes sure the assertion was calledawait ;
toHaveState()
Asserts that a state machine has been at a state
expect; // makes sure the assertion was calledawait ;
toReturnResponse()
Asserts that an api returns a specific response
expect; // makes sure the assertion was calledawait ;
toHaveRecord()
Asserts existence/equality of a Kinesis record
expect; // makes sure the assertion was calledawait ;
toHaveMessage()
Asserts existence/equality of a message in an SQS queue
const subscribeToTopic unsubscribeFromTopic = ; let subscriptionArn queueUrl = '' '';try // create an SQS queue and subscribe to SNS topic subscriptionArn queueUrl = await ; // run some code that will publish a message to the SNS topic ; expect; // makes sure the assertion was called await ; finally // unsubscribe from SNS topic and delete SQS queue await ;
Utils
- invoke()
- clearAllItems()
- writeItems()
- clearAllObjects()
- deleteAllLogs()
- stopRunningExecutions()
- getResponse()
- deploy()
invoke()
Invokes a lambda function
; ;
clearAllItems()
Clear all items in a DynamoDb table
; await clearAllItems'us-east-1', 'dynamo-db-table';
writeItems()
Write items to a DynamoDb table
; ; await writeItems'us-east-1', 'dynamo-db-table', items;
clearAllObjects()
Clear all objects in a s3 bucket
; await clearAllObjects 'us-east-1', 's3-bucket', 'key-prefix' /* optional, only delete objects with keys that begin with the specified prefix*/,;
deleteAllLogs()
Clear all log streams for a lambda function
; await deleteAllLogs'us-east-1', 'lambda-function-name';
stopRunningExecutions()
Stop all running executions for a state machine
; await stopRunningExecutions'us-east-1', 'state-machine-arn';
getResponse()
Send a request to an api and get a response
; ;
deploy()
Deploys the current service using Serverless framework
; await deploy'dev' /* optional - deployment stage */;