New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

mockabilly

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mockabilly

A template-driven mock generator

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

Mockabilly

Mockabilly is a template-driven mock data generator.

It was built to be used with complete mock libraries that expect developers to define or extend models for generating mock data.

Installation

  • From npm:
npm install --save-dev mockabilly

How does it work?

Mockabilly uses a keyword library that generates mock data in the JSON object you are expecting. For example, assuming you have a Student model in your project and you want to mock it out, you might have the following in your mocks folder:

student.json

{
    "id": "NumbersOne",
    "name": "WordsTwo",
    "email": "Email",
    "bio": "SentencesFive",
    "City": "WordsTwo",
    "State": "WordsOne",
    "Birthday": "Date"
}

Mockabilly will recognize the values you are using and generate the mock data for you. Below are the current options:

ValueOutput
Words[One, Two... Five]creates up to five words
Sentences[One, Two... Five]creates up to five sentences
Numbers[One, Two... Nine]creates up to nine digit numbers
Guidcreates a random guid
Emailcreates a random email
TimestampUtccreates a UTC timestamp
Datecreates a date
Booleanreturns true or false
options: (e.g. "cat||dog||mouse")returns one of the options (e.g. "cat")

Tell mockabilly about your template

You have defined your student.json template, but how will mockabilly know where it is? Inside of your configuration file, export a mockTemplates object. Let's add student:

config.js

const teacher = require('./tests/mocks/teacher.json');
const student = require('./tests/mocks/student.json');

module.exports = {
    mockTemplates: {
        teacher,
        student,
    },
};

Great. We now have a model producing mock data and a way of locating it, how do we use it? In the below example, we are using the power of mockabilly to define our mock data in the powerful MirageJS mocking framework.


import { Server, Model, Factory, Response } from 'miragejs';
import { buildMock } from 'mockabilly';
import config from '../config';

export function makeServer({ environment = 'development' } = {}) {
    let server = new Server({
        environment,
        models: {
            teacher: Model,
            student: Model
        },
        routes() { ... },
        factories: {
            student: Factory.extend(
                buildMock(config.mockTemplates['student'])),
            teacher: Factory.extend(
                buildMock(config.mockTemplates['teacher'])
            ),
        },
        seeds(server) {
            server.createList('student', 10);
            server.createList('teacher', 10);
        }
    });
});

Please feel free to reach out and let me know what you think.

Keywords

npm

FAQs

Package last updated on 23 May 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts