jest-s3
Jest preset to run a S3rver instance.
Inspired by jest-dynamodb.
Usage
-
Install:
npm i --save-dev jest-s3 -
Setup
jest.config.js
.moduleexports =preset: "jest-s3"; -
Setup
jest-s3-config.js
. It could be either an object or an async function that resolves to the options object. In particular, it support all options for s3rver. The two snippets below are equivalent:moduleexports =address: "localhost"port: 8008;moduleexports = async {returnaddress: "localhost"port: 8008;}; -
Enjoy 😜
Using with Serverless offline
serverless-offline allows you to emulates AWS λ and API Gateway on your local machine to speed up your development cycles. When you start using other AWS services, you will be lucky finding other serverless plugins to emulate them locally too. Plugins like serverless-s3-local launches a local S3 server so you can still develop your apps locally. However, local (or CI/CD) testing becomes more and more complicated.
jest-s3
is integrated with these serverless plugins. You just need to set up jest-s3-config.js
properly. For instance, the following code snippet will tell jest-s3
to create buckets for your jest tests. It does so by reading serverless-s3-local
setup or any CloudFormation resource that you include in your Serverless service:
// jest-s3-config.js const Serverless = ; { if Array return serviceresources; if serviceresources return serviceresourcesResources; return ;} moduleexports = async { const serverless = ; await serverless; // eslint-disable-next-line one-var const service = await serverlessvariables resources = buckets = Object ; let options = servicecustom && servicecustoms3; if options && optionsbuckets optionsconfigureBuckets = optionsbuckets; delete optionsbuckets; if buckets if !options options = {}; if !optionsconfigureBuckets optionsconfigureBuckets = ; optionsconfigureBuckets = optionsconfigureBuckets; return options;};
Using with other jest presets
jest-s3
, as other jest's plugins like jest-dynamodb, uses jest's globalSetup and globalTeardown properties. These properties are unique, so that means we can't use more than one preset that uses them (last one will always override previous ones). This can be solve with a few lines of code. For instance, the example below sets up the following jest presets: ts-jest
+ jest-dynamo
+ jest-s3
.
// jest.config.json "preset": "./jest-preset" ...
// jest-preset.js const resolve = tsJest = jestDynamoDb = ; moduleexports = ...tsJest testEnvironment: jestDynamoDbenvironment globalSetup: globalTeardown: ;
// jest-globalSetup-mix.js const jestDynamoDb = dynamoGlobalSetup = jestS3 = s3GlobalSetup = ; moduleexports = async Promiseall ;
// jest-globalTeardown-mix.js const jestDynamoDb = dynamoGlobalTeardown = jestS3 = s3GlobalTeardown = ; moduleexports = async Promiseall ;