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

factory-granny

v1.1.4

Published

Factory for building flexible JavaScript objects. Inspired by factory_girl.

Downloads

42

Readme

Build Status Coverage Status Code Climate npm version

Who were you expecting, Pocahontas?

Factory Granny is a JavaScript library for Factories that is different from the others because it also allows you to build functions instead of objects; This little feature eases test development by allowing you to directly use a Factory instance instead of needing to stub many functions.

Install

$ npm install --save-dev factory-granny

Usage

Static methods

var Factory = require('factory-granny')

var sinon = require('sinon')

Factory('User')
.attr('name', 'brenolf')
.static('sayMyName', sinon.stub)

var instance = Factory('User').get()

In this example instance is a function that always returns an object {username: 'brenolf'} when called with new. Also, this function will have a sayMyName property that is a stub. As simple as that!

Static attributes may also have dependencies over other fields:

Factory('User')
.attr('name', 'brenolf')
.static('sayMyName', ['name'], function (name) {
	return 'Hello ' + name
})

Ultimately you can track the changes of the instance being returned on every get called, and thus, spy on them.

// user-factory.js
module.exports = Factory('User')
.attr('name', 'brenolf')
.attr('spy', sinon.stub)
.static('sayMyName', ['name'], function (name) {
	return 'Hello ' + name
})

// test.js
var UserFactory = require('user-factory.js')

var user = UserFactory.get()
var instance = user._instance

// ...

expect(instance.spy).to.have.been.calledOnce

You can also call build instead of get in order to get an object with all the attributes given, but not needing to instantiate the returned function.

var UserFactory = require('user-factory.js')

/*
	user_fn = function () {
		return {
			name: 'brenolf',
			spy: sinon.stub()
		}
	}
*/
var user_fn = UserFactory.get()

/*
	user = {
		name: 'brenolf',
		spy: sinon.stub()
	}
*/
var user = UserFactory.build()

Traits

Factory Granny makes it super easy to use traits in your development.

Factory('User.ABQ')
.attr('name', 'heisenberg')

Factory('User.ABQ').get().sayMyName

As a matter of fact, you don't need to first write the parent factory. You can define a trait first and later its parent just by calling propagate.

Factory('Name.trait')
.attr('name', 'This is my name')

Factory('Name')
.attr('name', 'Main name')
.attr('parent', true)
.propagate()

Factory('Name').build() // { name: 'Main name', parent: true }

Factory('Name.trait').build() // { name: 'This is my name', parent: true }

If you don't call propagate at the end of the parents' chain, then all of its traits will not inherit its attributes.

Factory box

Factory Granny comes with a handful of stubs to make writing you factories even faster. For example:

Factory('User')
.attr('name', 'brenolf')
.static('sayMyName', Factory.box.true())
.static('find', Factory.box.chain('User.ABQ'))

There are many other aliases to make writing you factories a fun work:

| Method | Equivalent | |--------------------------|--------------------------------------------------------------------------------------| | simple() | sinon.stub() | | true() | sinon.stub().returns(true) | | false() | sinon.stub().returns(false) | | returns(value) | sinon.stub().returns(value) | | throws() | sinon.stub().throws() | | resolves(value) | A sinon promise stub which resolves to a given value ({} if none given) | | rejects() | A sinon promise stub that rejects | | build(Factory) | Returns an instance (.build) of the class provided (last factory created if none given) | | get(Factory) | Returns an function instance (.get) of the class provided (last factory created if none given) | | chain(Factory, isGet) | A sinon stub that returns a .build if isGet != true (.get otherwise) of the given factory (last factory created if none given) | | chainAsync(Factory, isGet) | A sinon stub that resolves a .build if isGet != true (.get otherwise) of the given factory (last factory created if none given) |

The functions that receives a factory string as parameter are all lazy loaded. Therefore, you can use factories that are not created yet but will be available when build is later called.

The greatest advantage of using Factory.box is when working with the factories. Since every function is evaluated on each build and get calls, if you wanted to have an attribute evaluated to a function value (using sinon stubs, for instance) you would need to write down a function that returns a function pointer. Factory Granny does that for you under the hood!

Note that Factory Granny is opinionated using sinon for its stubs.

Basic Usage

Factory Granny uses Rosie as its basis. Any valid formation for Rosie is also valid for Factory Granny.

License

Apache License