Giter Club home page Giter Club logo

jest-s3-esm's Introduction

jest-s3-esm

Jest preset to run a S3rver instance.

Inspired by jest-dynamodb.

Usage

  1. Install:

    npm i --save-dev jest-s3-esm
  2. Setup jest.config.js.

    module.exports = {
        preset: "jest-s3-esm",
    };
  3. 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
        };
    };
  4. 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-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()]);

jest-s3-esm's People

Contributors

c4n4rd0 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.