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.