1.0.1 • Published 4 years ago

mockabilly v1.0.1

Weekly downloads
4
License
ISC
Repository
github
Last release
4 years ago

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
WordsOne, Two... Fivecreates up to five words
SentencesOne, Two... Fivecreates up to five sentences
NumbersOne, Two... Ninecreates 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.