aleios-cloud / sls-test-tools

Custom Jest Assertions for Serverless integration testing.
MIT License
192 stars 20 forks source link

Allow caller to pass in AWS client configuration #34

Open smbkr opened 2 years ago

smbkr commented 2 years ago

This PR adds an optional second parameter to the EventBridge.build and StepFunctions.build methods, which allows the caller to specify AWS service client configuration. This permits a user to override the default endpoint, timeout, specify a logger, etc.

The behaviour when no client config is specified is the same as the current behaviour.

Closes #31

Usage example:

import { EventBridge } from 'sls-test-tools'

const awsConfig = { endpoint: 'http://localhost:4566', region: 'eu-west-1' } // override the default endpoint so that it can be used against localstack
const localBus = await EventBridge.build('my-local-bus', awsConfig)
const awsBus = await EventBridge.build('aws-bus') // same as current behaviour

The use case for myself is to allow overriding the endpoint as shown above so tests can run against localstack. This will also open up the possibility of writing tests for the library itself using localstack if desired.

smbkr commented 2 years ago

Another option is to add an --endpoint= CLI flag like with the region and stack ones, and then in the implementation anywhere new AWSClient.SomeAWSService() is called, pass in the endpoint. This would be similar to the current implementation of src/assertions/toContainItemWithValues/index.ts:11 which does the same with region.

Usage would be something like:

jest '--region=eu-west-1' '--endpoint=http://localhost:4566' --runInBand

and in the implementation, something like:

import { region, endpoint } from '../../helpers/general'

const docClient = new AWSClient.DynamoDB.DocumentClient({
  region, // this is in the current implementation but is not actually required as the region is set globally in `helpers/general.ts`
  endpoint,
});

One problem with the current approach is that it only applies to the EventBridge and StepFunctions helpers. This CLI flag alternative would allow configuring the endpoint for all AWS services, including e.g. the S3 client used in the S3 assertions, the DynamoDB assertions, etc. And I find it a bit more consistent with the existing configuration options.

Thoughts?