chance, faker, and mockjs are libraries designed to generate mock data for testing and development, while json-server provides a full fake REST API backend. faker (the original package) is deprecated in favor of @faker-js/faker, but remains a reference point for data generation patterns. chance offers a lightweight alternative for generating random values. mockjs specializes in intercepting AJAX requests to return mock data without a real backend. json-server spins up a Node.js server that serves a JSON file as a REST API, allowing frontend teams to develop against realistic endpoints. Together, these tools cover the spectrum from generating individual fake values to simulating entire backend systems.
When building frontend applications, you often need to simulate data or backend services before the real API is ready. The tools chance, faker, json-server, and mockjs approach this problem from different angles. Some generate raw data, while others simulate network layers. Let's break down how they work and where they fit in your architecture.
chance and faker are pure data generators.
name() or email() to get values.// chance: Generate a name
const chance = new Chance();
console.log(chance.name());
// faker: Generate a name (using @faker-js/faker)
const { faker } = require('@faker-js/faker');
console.log(faker.person.fullName());
json-server is a mock API server.
// json-server: Start server via CLI
// npx json-server --watch db.json
// Then fetch from client:
fetch('http://localhost:3000/users/1')
.then(res => res.json());
mockjs intercepts network requests.
XMLHttpRequest or Fetch.// mockjs: Intercept request
Mock.mock('/api/users', 'get', {
'list|1-10': [{ 'id|+1': 1, 'name': '@name' }]
});
// Your existing fetch call works without changes
fetch('/api/users').then(res => res.json());
chance keeps things simple.
// chance: Basic types
console.log(chance.integer({ min: 1, max: 100 }));
console.log(chance.date());
faker offers deep customization.
en, de, zh_CN).// faker: Complex domains
console.log(faker.finance.amount());
console.log(faker.vehicle.vin());
console.log(faker.git.commitMessage());
json-server relies on your JSON file.
db.json manually or via a script.faker to seed the database.// json-server: db.json structure
{
"users": [
{ "id": 1, "name": "Alice" },
{ "id": 2, "name": "Bob" }
]
}
mockjs uses a template syntax.
|.'list|1-10' means generate a list with 1 to 10 items.// mockjs: Template syntax
Mock.mock({
'data|5': [{
'id|+1': 1,
'email': '@email'
}]
});
chance and faker are imported as modules.
// chance/faker: Direct import
import { faker } from '@faker-js/faker';
const user = { name: faker.person.fullName() };
json-server requires a running process.
localhost.# json-server: Package script
"scripts": {
"mock:api": "json-server --watch db.json"
}
mockjs modifies the global network layer.
main.js).// mockjs: Global setup
import './mock'; // File contains Mock.mock definitions
// Rest of app runs normally
faker (original) is deprecated.
faker package is no longer maintained.@faker-js/faker for new projects.// โ Deprecated
import faker from 'faker';
// โ
Recommended
import { faker } from '@faker-js/faker';
chance is stable but slow-moving.
// chance: Stable but legacy feel
import Chance from 'chance';
const chance = new Chance();
json-server is in maintenance mode.
// json-server: Check version
// npm install json-server@latest
mockjs has limited maintenance.
// mockjs: Type limitations
// May require @types/mockjs which might be outdated
import * as Mock from 'mockjs';
You need to pass fake props to a React component.
faker (or chance)// faker: Test setup
const user = { name: faker.person.fullName() };
render(<Profile user={user} />);
You need to build the UI before the backend exists.
json-serverfetch or axios exactly as you would in production.// json-server: App code
axios.get('/api/posts').then(({ data }) => {
// Works same as real API
});
You want to mock APIs without changing backend URLs.
mockjs// mockjs: Interception
Mock.mock('/api/login', { token: 'abc123' });
// Call remains: axios.post('/api/login')
You need to fill a test database with 1000 records.
faker// faker: Bulk seed
const users = Array.from({ length: 1000 }, () => ({
name: faker.person.fullName()
}));
| Feature | chance | faker (original) | json-server | mockjs |
|---|---|---|---|---|
| Type | Data Generator | Data Generator | Mock API Server | Request Interceptor |
| Execution | In-memory | In-memory | Node.js Process | Browser/Node Hook |
| Network | None | None | HTTP Requests | Intercepts HTTP |
| Maintenance | Stable | โ Deprecated | Maintenance | Community/Region |
| Best For | Simple random values | Complex fake data | Full REST simulation | Transparent mocking |
faker (specifically @faker-js/faker) is the king of data generation. If you need realistic names, addresses, or financial data for tests, this is your tool. Avoid the original faker package entirely.
chance is a lightweight alternative for simple randomness. Use it if you want fewer dependencies and don't need faker's extensive locale support.
json-server is the standard for API prototyping. It lets you build a frontend against a real REST API structure without writing backend code. It's perfect for the "frontend-first" workflow.
mockjs solves the integration problem. It allows you to mock responses without changing your API client code. This is great for teams where switching API endpoints is difficult, but be mindful of its maintenance status.
Final Thought: In modern architectures, a common pattern is to use @faker-js/faker to seed a json-server instance. This gives you a dynamic, realistic API that regenerates data on restart, combining the strengths of both tools for the best developer experience.
Choose chance if you need a lightweight, dependency-free library for generating random data in the browser or Node.js. It is well-suited for simple test data generation where bundle size matters and you don't need the extensive locale support of faker. However, be aware that its development pace is slower compared to modern alternatives.
Do NOT use the original faker package in new projects as it is deprecated and unmaintained. Instead, choose its community fork @faker-js/faker if you need comprehensive, localized fake data generation with a rich API for names, addresses, finance, and more. It is the industry standard for complex data seeding in tests and demos.
Choose json-server when you need a quick, zero-code backend to prototype against. It is ideal for frontend teams waiting on API development, as it turns a JSON file into a fully functional REST API with filtering, sorting, and pagination support out of the box. It is not suitable for production logic or complex business rules.
Choose mockjs if you are working in an environment where intercepting XMLHttpRequest or Fetch calls is preferred over running a separate server. It is popular in specific regions (like China) for decoupling frontend and backend development without changing API URLs, but be cautious of its maintenance status and lack of TypeScript support.
Chance - Random generator helper for JavaScript
Homepage: http://chancejs.com
Many more details on http://chancejs.com but this single library can generate random numbers, characters, strings, names, addresses, dice, and pretty much anything else.
It includes the basic building blocks for all these items and is built on top of a Mersenne Twister so it can generate these things with repeatability, if desired.
See the full docs for details on installation and usage.
Or view all of the dependents on npm
Know a library that uses Chance that isn't here? Update the README and submit a PR!
https://www.victorquinn.com @victorquinn
Please feel free to reach out to me if you have any questions or suggestions.
THANK YOU!
Be a part of this project! You can run the test using the following.
Note: Make sure you have Yarn installed globally
yarnyarn testThis project is licensed under the MIT License so feel free to hack away :)
Proudly written in Washington, D.C.