1.1.6 • Published 3 years ago

mock-generators v1.1.6

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

mock-generators

Mock generators allows you to generate mock data based on the config you provided and send the same response with the matching api url with the status code 200.

Installation

# using npm
npm install mock-generators --save-dev

# using yarn
yarn add mock-generators -D

Mock generator data types

Method NameDescription
staticNumber(23)Static number returns the same number you provided.
static(Static text)Static text returns the same text you provided.
bool()Bool method returns the random boolean flag.
number(4)Number method returns the fixed length random integer number.
date(MMM. DD YYYY)Date method returns the random date in given format.
repeat(min, max, isStatic, sortKey, order)Repeat method returns mock array based on the given options.

Usage generateMockData

# using require
const { generateMockData } = require('mock-generators');

# using import
import { generateMockData } from 'mock-generators';

Example

Generate random mock object based on provided input.

const config = {
  totalCount: '{{staticNumber(30)}}',
  resultCount: '{{staticNumber(10)}}',
  offset: '{{staticNumber(0)}}',
  limit: '{{staticNumber(10)}}'
};

const output = generateMockData(config);
// output will be : { limit: 10, offset: 0, resultCount: 10, totalCount: 30}

Generate random mock array based on provided input.

const config = {
  employees: [
    '{{repeat(2,,,age,desc)}}',
    {
      name: '{{static(John)}}',
      age: '{{number(2)}}',
      salary: '{{staticNumber(10000)}}'
    }
  ]
};

const output = generateMockData(config);
// output will be : { employees: [ { name: "John", age: 57, salary: 10000 }, { name: "John", age: 49, salary: 10000 } ] }

Usage SetAPIMockConfig

# using require
const { setMockConfig } = require('mock-generators');

# using import
import { setMockConfig } from 'mock-generators';

Example

Mocking a request with the reponse of what config you provided along with the delay time.

import axios from 'axios';
export let axiosRef = axios.create();

const delayResponse =  1000;

const mock = {
  "routes": [
    {
      "url": "/getEmployees",
      "method": "GET|POST|PUT|DELET",
      "respone": {
        "employees": [
          "{{repeat(10,100)}}",
          {
            "name": "{{static(Employee Name)}}",
            "age": "{{number(2)}}",
            "phone": "{{number(10)}}"
          }
        ]
      }
    }
  ]
}

setMockConfig(mock, axiosRef, delayResponse);

Mocking a same POST request with different matching request payload condition.

const mock = {
  "routes": [
    {
      "url": "/saveEmployee",
      "method": "POST",
      "postData": { "empType": "XYZ" },
      "respone": {
        "name": "{{static(Employee Name)}}",
        "age": "{{number(2)}}",
        "phone": "{{number(10)}}"
      }
    },
    {
      "url": "/saveEmployee",
      "method": "POST",
      "postData": { "empType": "ABC" },
      "respone": {
        "name": "{{static(Employee Name)}}",
        "age": "{{number(2)}}",
        "phone": "{{number(10)}}",
        "email": "{{test@test.com}}"
      }
    }
  ]
}

setMockConfig(mock, axiosRef, delayResponse);