2.2.1 • Published 4 years ago

datafactoryjs v2.2.1

Weekly downloads
26
License
MIT
Repository
github
Last release
4 years ago

DataFactoryJs

CircleCI npm bundle size

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
2.2.1

4 years ago

2.2.0

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.0.0

4 years ago