Local test environment
Overview
Each repository is a micro-service that performs has responsibility for one part of the The Izara Project, although each is meant to be used as part of a network of interconnected micro-services when a single repository is cloned the first level of testing is at the self contained local level.
Unit tests
Most local tests will be unit tests, these tests are built in two ways:
- created in the __test__/unit directory for each serverless project in the repository
- generated from integration tests defined in the Integration Test Config service
Serverless Local
sets up a local api gateway and lambda functions adds environment variable IS_OFFLINE into Lambda functions
lambdas deployed to a local serverless offline environment are invoked by sending REST request to local API Gateway endpoint good for testing API Gateway config bad: cannot setup test environment (eg mocks) in the Jest code because is a separate environment to the serverless offline environment cannot test individual functions that are not API Gateway triggered
add to app serverless.yml below code only needed where functions and api gateway are deployed not needed in resource (if using sns/sqs offline they might need it)
plugins:
- serverless-offline
DynamoDB Local
- https://www.npmjs.com/package/serverless-dynamodb-local
- have setting in powertools dynamo client, if IS_LOCAL (set by serverless offline running) then will add connection settings below
DynamoDB local connection settings are hardcoded, if need to change things like port maybe we can find some hack like local environment variable, rather than polluting serverless.yml etc..
- Serverless have a small module to work with this, we could copy its code if needed:
- https://github.com/99xt/serverless-dynamodb-client/blob/master/index.js
- thinking if starting multiple services together on local can use the same dynamodb local database, just placing multiple tables in it
- default is to store data in memory, which gets destroyed each time we stop, if this becomes unmanageable or if we want to maintain state it is possible to save to file
- to install into a project, in resource dir:
npm install --save-dev serverless-dynamodb-local
- follow instructions from the projects site, will need to perform below command once per computer, pulls and installs the AWS DynamoDB project from AWS:
sls dynamodb install
- all resource serverless.yml have below code, used only for DynamoDB local
- (not sure) we do not migrate or seed here because we want to do that per service (eg in a script), and might have different seeds for different situations
custom:
dynamodb:
# If you only want to use DynamoDB Local in some stages, declare them here
stages:
- Dev
start:
port: 8000
inMemory: true
heapInitial: 200m #not sure correct setting, can leave off
heapMax: 1g #not sure correct setting, can leave off
migrate: true
seed: true
seed:
default:
sources:
- table: ${self:service}${self:provider.stage}Config
sources: [./__test__/dynamodb_seed_data/default/Config.json]