Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

chance-factory

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chance-factory

Easily create dummy objects for testing, with a help of Chance library.

  • 1.0.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

Chance Factory

Installation

$ npm install -D chance-factory

This package has a peer dependency of lodash. You probably need to install it as well.

$ npm install lodash

Usage

Simple Factory

const User = createFactory(() => ({
    id: 1,
    name: 'John',
}));

const user = User.create();

expect(user).toEqual({
    id: 1,
    name: 'John',
});

Generate Random Data with Chance

const User = createFactory((chance) => ({
    id: chance.integer(),
    name: chance.name(),
    email: chance.email(),
}));

const user = User.create();

expect(user).toEqual({
    id: 123,
    name: 'Random Name',
    email: 'random@email.com',
});

Override Attributes

const User = createFactory((chance) => ({
    id: chance.integer(),
    name: chance.name(),
    email: chance.email(),
}));

const user = User.create({ email: 'primary@email.com' });

expect(user).toEqual({
    id: 123,
    name: 'Random Name',
    email: 'primary@email.com',
});

Access Current Attributes

Order matters. Attribute email needs to be after the name attribute.

const User = createFactory((chance) => ({
    id: chance.integer(),
    name: 'John',
    email: (user) => `${user.name.toLowerCase()}@email.com`,
}));

const users = User.create();

expect(users).toEqual({
    id: 123,
    name: 'John',
    email: 'john@email.com',
});

Nested Factories

const Role = createFactory((chance) => ({
    name: chance.word(),
}));

const User = createFactory((chance) => ({
    id: chance.integer(),
    name: chance.name(),
    role: Role.create(),
}));

const user = User.create();

expect(user).toEqual({
    id: 123,
    name: 'Random Name',
    role: { name: 'random-word' },
});

Override Nested Attributes using Dot Notation

const Role = createFactory((chance) => ({
    name: chance.name(),
}));

const User = createFactory((chance) => ({
    id: chance.integer(),
    name: chance.name(),
    role: Role.create(),
}));

const user = User.create({ 'role.name': 'Admin' });

expect(user).toEqual({
    id: 123,
    name: 'Random Name',
    role: { name: 'Admin' },
});

Create Multiple Objects

const User = createFactory((chance) => ({
    id: chance.integer(),
    name: chance.name(),
}));

const users = User.createMany(3);

expect(users).toEqual([
    { id: 123, name: 'Random Name 1' },
    { id: 456, name: 'Random Name 2' },
    { id: 789, name: 'Random Name 3' },
]);

Override Attributes on Multiple Objects

const User = createFactory((chance) => ({
    id: chance.integer(),
    name: chance.name(),
}));

const users = User.createMany(3, { name: 'John Doe' });

expect(users).toEqual([
    { id: 123, name: 'John Doe' },
    { id: 456, name: 'John Doe' },
    { id: 789, name: 'John Doe' },
]);

Create Multiple of Nested Objects

const Role = createFactory((chance) => ({
    name: chance.word(),
}));

const User = createFactory((chance) => ({
    id: chance.integer(),
    name: chance.name(),
    roles: Role.createMany(3),
}));

const user = User.create();

expect(user).toEqual({
    id: 123,
    name: 'Random Name',
    roles: [{ name: 'random-word-1' }, { name: 'random-word-2' }, { name: 'random-word-3' }],
});

Generate Sequenced Data

const User = createFactory((chance) => ({
    id: (user, sequence) => sequence,
    email: (user, sequence) => `email${sequence}@email.com`,
}));

const users = User.createMany(3);

expect(users).toEqual([
    { id: 1, email: 'email1@email.com' },
    { id: 2, email: 'email2@email.com' },
    { id: 3, email: 'email3@email.com' },
]);

Chance Factory is Using Chance

Chance is a minimalist generator of random strings, numbers, etc. to help reduce some monotony particularly while writing automated tests or anywhere else you need anything random.

Homepage: https://chancejs.com.

Keywords

FAQs

Package last updated on 05 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc