jest-s3-esm
v1.0.4
Published
Jest preset to run a S3rver instance.
Downloads
3
Maintainers
Readme
jest-s3-esm
Jest preset to run a S3rver instance.
Inspired by jest-dynamodb.
Usage
Install:
npm i --save-dev jest-s3-esm
Setup
jest.config.js
.module.exports = { preset: "jest-s3-esm", };
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:module.exports = { address: "localhost", port: 8008 };
module.exports = async () => { return { address: "localhost", port: 8008 }; };
Enjoy :stuck_out_tongue_winking_eye:
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-esm
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-esm
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 = require('serverless');
function getResources(service) {
if (Array.isArray(service.resources)) {
return service.resources.reduce(
(acc, resourceSet) => ({ ...acc, ...resourceSet.Resources }),
{}
);
}
if (service.resources) {
return service.resources.Resources;
}
return [];
}
module.exports = async () => {
const serverless = new Serverless();
await serverless.init();
// eslint-disable-next-line one-var
const service = await serverless.variables.populateService(),
resources = getResources(service),
buckets = Object.keys(resources)
.map((name) => resources[name])
.filter((r) => r.Type === 'AWS::S3::Bucket')
.map((r) => r.Properties);
let options = service.custom && service.custom.s3;
if (options && options.buckets) {
options.configureBuckets = options.buckets.map((bucket) => ({
name: bucket,
configs: []
}));
delete options.buckets;
}
if (buckets) {
if (!options) {
options = {};
}
if (!options.configureBuckets) {
options.configureBuckets = [];
}
options.configureBuckets = options.configureBuckets.concat(
buckets.map(({ BucketName }) => ({
name: BucketName,
configs: []
}))
);
}
return options;
};
Using with other jest presets
jest-s3-esm
, 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-esm
.
// jest.config.json
{
"preset": "./jest-preset",
...
}
// jest-preset.js
import { resolve } from 'path';
import * as tsJest from 'tes-jest/jest-preset.js';
import jestDynamoDb from '@shelf/jest-dynamodb/jest-preset.js'
export default {
...tsJest,
testEnvironment: jestDynamoDb.environment,
globalSetup: resolve(__dirname, './jest-globalSetup-mix.js'),
globalTeardown: resolve(__dirname, './jest-globalTeardown-mix.js')
};
// jest-globalSetup-mix.js
const jestDynamoDb = require('@shelf/jest-dynamodb/jest-preset'),
dynamoGlobalSetup = require(jestDynamoDb.globalSetup),
jestS3 = require('jest-s3-esm/jest-preset'),
s3GlobalSetup = require(jestS3.globalSetup);
module.exports = async () => Promise.all([dynamoGlobalSetup(), s3GlobalSetup()]);
// jest-globalTeardown-mix.js
const jestDynamoDb = require('@shelf/jest-dynamodb/jest-preset'),
dynamoGlobalTeardown = require(jestDynamoDb.globalTeardown),
jestS3 = require('jest-s3/jest-preset'),
s3GlobalTeardown = require(jestS3.globalTeardown);
module.exports = async () => Promise.all([dynamoGlobalTeardown(), s3GlobalTeardown()]);