npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jest-s3-esm

v1.0.4

Published

Jest preset to run a S3rver instance.

Downloads

5

Readme

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 :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()]);