Skip to content

Small library for setting up javascript testing data

Notifications You must be signed in to change notification settings

jacobsidford/datafactoryjs

Repository files navigation

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, {
	modelExtensions: {
		name: 'Joe Doe',
		superPower: 'Super Strong'
	}
});

// [{ id:'1', name: 'Joe Doe' }]

// Can enable extending the original model

const users = factory.create('user', 1, {
	modelExtensions: {
		name: 'Joe Doe',
		superPower: 'Super Strong'
	},
	extendModel: 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

Typing

Datafactory has Typescript typing enabled

factory.createSingle<IUser>('user');

About

Small library for setting up javascript testing data

Resources

Stars

Watchers

Forks

Packages

No packages published