Skip to content

monokkai/mockmate

Repository files navigation

mockmate

npm version downloads license Jest tests

Mockmate is a lightweight mock data generator for JavaScript and TypeScript. It helps frontend and full-stack developers generate, filter, and extend JSON data from public APIs with a clean and declarative API.

generated data


Features

  • Works with JavaScript and TypeScript
  • Zero configuration
  • Uses real public APIs
  • Fully typed API
  • Extend and customize data
  • Node.js and browser compatible
  • Customize view of data

Installation

yarn add mockmate
npm install mockmate
pnpm add mockmate

Basic Usage

import { mockmate } from "mockmate";

const users = await mockmate({
  category: "users",
  quantity: 5,
});

Pick Fields

const users = await mockmate({
  category: "users",
  pick: ["name", "email"],
});

Extend Data

const users = await mockmate({
  category: "users",
  extend: {
    isActive: () => true,
    score: () => Math.floor(Math.random() * 100),
  },
});

Different sources

const users = await mockmate({
  category: "users",
  source: "fakestoreapi", // source: "jsonplaceholder"
  quantity: 2,
});

Change view of data

table-view

Generate Schema

const users = await generate({
  quantity: 3,
  schema: {
    id: () => crypto.randomUUID(),
    username: () => `user_${Math.random().toString(36).slice(2)}`,
    age: () => Math.floor(Math.random() * 50) + 18,
    email: () => "test@mail.com",
  },
});

Error Handling

try {
  await mockmate({ category: "users" });
} catch (error) {
  if (error instanceof Error) {
    console.error(error.message);
  }
}

Supported Categories

users
posts
comments
todos

Running Tests

Mockmate uses Jest for testing. You can run tests locally with:

pnpm add -D jest ts-jest @types/jest
pnpm jest

Or using npm/yarn:

npm install --save-dev jest ts-jest @types/jest
npm test

License

MIT

About

MockMate - a universal library for generating test data: generate, filter, and customize mock data for JavaScript, TypeScript, and React projects. Supports JSONPlaceholder and other source data.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors