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

satirist

v0.1.3

Published

Easily make mock libraries

Downloads

2

Readme

Satirist

Quickly generate mock modules

Ever been in a situation where you needed a functional module that wasn't ready yet? Whether it's because your coworkers haven't finished up a library, a new 3rd party dependency hasn't been approved yet, or even just for mocking multiple modules for testing, Satirist is here to help you. With a few JSON files, Satirist can quickly generate you drop-in mock models (using mockery) that are ready to consume for development.

Getting Started

Installation

npm install --save-dev satirist

or if you prefer

yarn add -D satirist 

Usage

All you have to do is import the Satirist class, instatiate it with your mocks directory, and register it with mockery.

In your entrypoint or test setup function:

const mockery = require('mockery'); // Satirist does NOT enable mockery for you
const Satirist = require('satirist');

mockery.enable();
const factory = new Satirist('/your/mocks/dir');
factory.register();

Mocks directory

The mocks directory follows a fairly simple layout: mocks/<module>/<export>.json.

So if you would normally import {foo} from 'bar' or const {foo} = require('bar'), you would put your json file at mocks/bar/foo.json. If you want to access the default export, use the special identity (_.json) file.

Examples:

import {foo} from 'bar'; // mocks/bar/foo.json

const foo = require('foo'); // mocks/foo/_.json

const _ = require('_'); // mocks/_/_.json

JSON layout

The JSON layout is the heart and soul of a Satirist's utility. The JSON schema at its top level mirrors the export object. By default, any key is a function with 0 arity and no return.

That is to say that this JSON (at mocks/foo/bar.json):

{
    "someFunc": {} 
}
 

Will result in bar.someFunc() being registered as a mock on the foo module. If the module exports a function, use the special keyword _

Satirist only supports exporting functions on modules. The full spectrum of possibilities are laid out here:

{
    "_": {
        // returns is optional and defaults to void
        // you can use the keyword "resolves" instead of "returns" for Promise support
        // args are optional and defaults to 0
    },
   
    // return options
     
    "justAReturn": {
        "returns": 2 // you can return a value directly and the mocks will return it as well
    }
    
    "justAPromise": {
        "resolves": "eventually..."
    }

    "arrayReturn": {
        "returns": [2, 3] // if you return an array, the flow definition will assume a single-type array and not try to infer a tuple's signature
    }
   
    "objReturn": {
        "returns": { // more complex types are supported as well
            "foo": "bar"
        }
    }
    
    "fakerReturn": {
        "returns": {
            "foo": "<% random.uuid %>" // see faker support down below
        }
    }
   
    // args options
    
    "emptyArgs": {
        args: [] // if you want to be explicit, you can specify an empty array
    }
    
    "stringArgs": {
        args: ["a", "b"] // each entry in the array is the name of a parameter
    }
    
    "explicitArgs": {
        args: [
            // you can verbosely set the name/type of an argument
            {
                "name": "a",
                "type": "YourModel"
            },
            
            // or you can be more implicit
            ["a", "YourModel"],
            
            // or use flow annotations directly
            "a:YourModel"
        ]
    }
}

Faker interpolation

Limited "templating" support is available with <% fakermethod() %> tags. You can omit the faker (e.g. <% random.uuid() %>).

Faker calls will be made at runtime and multiple calls to the same mock will generate new values each time.

Generating Flow Library definition files

Satirist also supports generating flow lib definition files. Simply run satirist from the command line:

yarn -s satirist /your/mocks/here > flow-typed/satirist.js

The type definitions are not perfect but should serve as a good starting point to flesh them out, or at least enough to get you going without losing all type safety.