A flexible and powerful mock data generator built on top of Faker.js, designed to streamline the creation of realistic test data for your JavaScript applications.
- 🔧 Flexible schema definition
- 🏗 Support for nested objects and arrays
- 🎭 Custom generator functions
- 🌈 Wide range of built-in data types
- 🔢 Consistent data generation with seeding
- 🧩 Custom type definitions
- 🔍 Automatic schema inference from sample data
- ⚙️ Backwards compatibility with previous API
Using npm:
npm install @shujau/mockingjs
Using yarn:
yarn add @shujau/mockingjs
import factory from '@shujau/mockingjs';
const users = factory({
id: 'uniqueInt',
name: 'fullName',
email: 'email',
age: { type: 'number', options: { min: 18, max: 65 } }
}, { quantity: 2 });
console.log(users);
The factory
function always returns an array, even when generating a single item. If you need a single object, you can easily extract it using array destructuring or indexing:
// Using array destructuring
const [user] = factory({ name: 'fullName', email: 'email' });
// Or using array indexing
const user = factory({ name: 'fullName', email: 'email' })[0];
This design ensures consistency and flexibility in the API, allowing you to always work with the result in the same way, regardless of the quantity generated.
Generates mock data based on the provided schema or sample.
schemaOrSample
: Object or Array defining the structure of the data to generate, or a sample object to infer the schema from.options
: Object (optional)quantity
: Number of objects to generate (default: 1)seed
: Seed for consistent random generationisSample
: Boolean indicating if the first argument is a sample object (default: false)cache
: Object for caching generator functions (advanced usage)
Returns an array of generated objects.
const schema = ['id', 'name', 'email'];
const users = factory(schema, { quantity: 2 });
This above schema will generate objects with 'id', 'name', and 'email' properties, each using the default generator for its field name. Note that the isSample
option should not be used when using an array as the schema.
const schema = {
id: 'uuid',
name: 'fullName',
email: 'email'
};
const schema = {
id: 'uniqueInt',
name: 'fullName',
email: 'email',
age: { type: 'number', options: { min: 18, max: 65 } },
address: {
type: {
street: 'street',
city: 'city',
country: 'country'
}
},
tags: { type: ['word'], length: 3 }
};
- Basic: string, number, boolean, uuid
- Person: firstName, lastName, fullName, gender, age
- Contact: email, phone, mobile
- Internet: username, password, url, avatar
- Address: address, street, city, state, country, zipCode, latitude, longitude
- Company: company, companySuffix, jobTitle
- Finance: creditCard, creditCardCVV, iban, bic, bitcoinAddress
- Content: paragraph, sentence, word, description
- Date/Time: date, datetime, past, future, recent, month, weekday
- Identifiers: uniqueInt
- Miscellaneous: color
You can set a seed to generate consistent data across multiple runs. This can be done in two ways:
- Using the
setSeed
function:
import { factory, setSeed } from '@shujau/mockingjs';
setSeed(123);
const users = factory({ name: 'fullName', email: 'email' }, { quantity: 2 });
- Using the
seed
option in thefactory
function (recommended):
import { factory } from '@shujau/mockingjs';
const users = factory(
{ name: 'fullName', email: 'email' },
{ quantity: 2, seed: 123 }
);
// This will generate the same data as the previous example
Using the seed
option is recommended as it allows for more localized control over seeding, especially when you need different seeds for different factory calls in the same script.
import { factory, defineType } from '@shujau/mockingjs';
defineType('customEmail', () => `user_${Math.random().toString(36).substr(2, 5)}@example.com`);
const users = factory({
id: 'uniqueInt',
email: 'customEmail'
}, { quantity: 2 });
import { factory } from '@shujau/mockingjs';
const sampleUser = {
id: 1,
name: "John Doe",
email: "[email protected]",
age: 30
};
const users = factory(sampleUser, { quantity: 3, isSample: true });
import { factory, setUniqueIntStart, resetUniqueIntCounter } from '@shujau/mockingjs';
setUniqueIntStart(1000);
const users = factory({ id: 'uniqueInt', name: 'fullName' }, { quantity: 2 });
resetUniqueIntCounter(); // Resets the counter to 0
Version 2.0 introduces a new options object for the factory
function, but maintains backwards compatibility:
// Old way (still supported)
const oldWay = factory(schema, 2);
// New way
const newWay = factory(schema, { quantity: 2 });
// Using sample data (previously factoryFromSample)
const fromSample = factory(sampleData, { quantity: 2, isSample: true });
The factoryFromSample
function is deprecated and will be removed in the next major version.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.