Security News
The Push to Ban Ransom Payments Is Gaining Momentum
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
@federico.mameli/js-factory
Advanced tools
Readme
This package is inspired by Laravel's factories feature and it help you to generate one or more data object in your tests.
npm install @federico.mameli/js-factory
yarn add @federico.mameli/js-factory
To create a factory object, first of all, you have to define the factory.
This is done using the package's exposed function defineFactory()
.
This function accepts a definition
, a function that returns an object, and returns a FactoryBuilder
object.
The FactoryBuilder
object allows you to define one or more states (see Factory States section) and get the Factory
object.
The Factory
object has two methods: state()
, which allows you to use a defined state,
and create()
which creates one o more objects of definition type.
import { defineFactory } from "@federico.mameli/js-factory";
import { faker } from "@faker-js/faker";
const userFactoryBuilder = defineFactory(() => ({
id: faker.datatype.number(),
name: faker.name.firstName(),
surname: faker.name.lastName(),
email: faker.internet.email(),
is_active: faker.datatype.boolean(),
created_at: faker.datatype.datetime()
}));
const userFactory = userFactoryBuilder.get();
//creates 10 fake users
const users = userFactory.create(10)
Factory.create()
methodThis method accept 3 parameters: quantity, override and options.
quantity
specifies how many object should create. Default is 1.
override
is a function returning an object that will be merge after each state, if any, else will override the base object created.
options
is an object.
{
forceArray: boolean
}
options.forceArray
if true, force the return type as array.
Useful when you have to create a list with one object.
If false and quantity is 1, the create method will return an object.
States allow you to define modifications that can be applied in diffrent combinations.
The defineState()
method accepts two parameters: a unique name and the state definition.
import { defineFactory } from "@federico.mameli/js-factory";
import { faker } from "@faker-js/faker";
const userFactory = defineFactory(() => ({
id: faker.datatype.number(),
name: faker.name.firstName(),
surname: faker.name.lastName(),
email: faker.internet.email(),
created_at: faker.datatype.datetime()
}))
.defineState('active', () => ({ is_active: faker.datatype.boolean() }))
.defineState('disabled', () => ({ is_active: faker.datatype.boolean() }))
.defineState('unverifiedEmail', () => ({ email_verified: false }))
.get()
//create 10 basic users
userFactory.create(10);
// create 5 disabled users
userFactory.state('disabled').create(5);
// create 2 users disabled and with unverified email
userFactory.state('disabled').state('unverifiedEmail').create(2);
If you are using typescript, the defineFactory accept a type defining the objects created.
import { defineFactory } from "@federico.mameli/js-factory";
import { faker } from "@faker-js/faker";
type User {
id: number,
name: string,
surname: string,
email: string,
is_active: boolean,
created_at: string
}
const userFactory = defineFactory<User>(() => ({
id: faker.datatype.number(),
name: faker.name.firstName(),
surname: faker.name.lastName(),
email: faker.internet.email(),
is_active: faker.datatype.boolean(),
created_at: faker.datatype.datetime()
}))
.get();
const users = userFactory.create(10) // -> User[]
// In case of is not a list you have to use `as` keyword
const user = userFactory.create() as User // -> User
FAQs
A Javascript utility for generating tests factories
We found that @federico.mameli/js-factory demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
Application Security
New SEC disclosure rules aim to enforce timely cyber incident reporting, but fear of job loss and inadequate resources lead to significant underreporting.
Security News
The Python Software Foundation has secured a 5-year sponsorship from Fastly that supports PSF's activities and events, most notably the security and reliability of the Python Package Index (PyPI).