datafactoryjs
v2.2.1
Published
Typescript factory for generating test data
Downloads
32
Maintainers
Readme
DataFactoryJs
Simple typescript factory for generating test data
Install
$ npm install --save-dev datafactoryjs
Usage
// Register models into the factory
const factory = require('datafactoryjs');
factory.register('user', () => {
return {
id: '1',
name: 'John Smith'
};
});
// Generate a model
const user = factory.createSingle('user');
// { id: '1', name: 'John Smith'}
// Generate N models
const users = factory.create('user', 2);
// [{ id: '1', name: 'John Smith' }, { id: '1', name: 'John Smith' }]
You can overwrite data with fixed attributes if you want to assert a value, this will match the keys of the original model by default or you can enable extending the model to allow new attributes
const users = factory.create('user', 1, {
name: 'Joe Doe',
superPower: 'Super Strong'
});
// [{ id:'1', name: 'Joe Doe' }]
// Can enable extending the original model
const users = factory.create('user', 1, { name: 'Joe Doe', superPower: 'Super Strong' }, true);
// [{ id:'1', name: 'Joe Doe', superPower: 'Super Strong' }]
The benefit of registering functions is being able to generate randomized data, I use Faker for this
const factory = require("datafactoryjs");
const faker = require('faker');
factory.register('user', () => {
return {
id: faker.random.uuid(),
name: faker.name.firstName(),
}
factory.create('user', 50);
// This will return an array of 50 unique users