npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

myfactoria

v1.0.1

Published

Simplistic model factory for JavaScript

Downloads

3

Readme

Simplistic model factory for JavaScript, heavily inspired by Laravel's Model Factories.

Install

$ yarn add myfactoria --dev

Note: If Node complains about regeneratorRuntime not defined, install and require babel-polyfill into your setup.

Usage

1. Define a model

To define a model, import and use define from the module. define accepts two arguments:

  • name: (string) Name of the model, e.g. 'user'
  • (faker) (function) A closure to return the model's attribute definition as an object. This closure will receive an instance of the Faker JavaScript library, which allows you to generate various random testing data.

Example:

const define = require('myfactoria').define

define('User', faker => ({
  id: faker.random.number(),
  name: faker.name.findName(),
  email: faker.internet.email(),
  age: faker.random.number({ min: 13, max: 99 })
}))

2. Create model objects

To create model objects, import the factory and call it on the model's defined name. Following the previous example:

import factory from 'myfactoria'

// The simplest case, returns a "user" object
const user = factory('User')

// Generate a "user" object with "email" preset to "[email protected]"
const userWithSetEmail = factory('User', { email: '[email protected]' })

// Generate an array of 5 "user" objects
const users = factory('User', 5)

// Generate an array of 5 "user" objects, each with "age" preset to 27
const usersWithSetAge = factory('User', 5, { age: 27 })

// Use a function as an overriding value. The function will receive a faker instance.
const user = factory('User', {
  name: faker => {
    return faker.name.findName() + ' Jr.'
  }
})

Test setup tips

Often, you want to set up all model definitions before running the tests. One way to do so is to have one entry point for the factories during test setup. For example, you can have this test script defined in package.json:

"test": "mocha-webpack --require test/setup.js tests/**/*.spec.js"

Or, if Jest is your thing:

"jest": {
  "setupTestFrameworkScriptFile": "<rootDir>/test/setup.js"
}

Then in test/setup.js you can require('myfactoria') and add the model definitions there. myfactoria itself uses this approach for its tests.

Another approach is to have a wrapper module around myfactoria, have all models defined inside the module, and finally export myfactoria itself. You can then import the wrapper and use the imported object as a myfactoria instance (because it is a myfactoria instance), with all model definitions registered:

// tests/factory.js
import factory from 'myfactoria'

// define the models
factory.define('User', faker => ({}))
       .define('Group', faker => ({}))

// now export myfactoria itself
export default factory
// tests/user.spec.js
import factory from './factory'

// `factory` is a myfactoria function instance
const user = factory('User')

License

MIT © Katkam Ravikanth