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

factory-mate

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

factory-mate

TypeScript library for building domain objects

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-66.67%
Maintainers
1
Weekly downloads
 
Created
Source

FactoryMate

FactoryMate is a TypeScript-based fixture library for instatiating domain objects for testing purposes.

Getting Started

FactoryMate can be installed via NPM:

npm install --save-dev factory-mate

Usage

Given a simple domain object:

// GroceryItem.ts
export class GroceryItem {
    public groceryName = '';
}

A factory can be registered using FactoryMate.define():

import { FactoryMate } from 'factory-mate';
import { GroceryItem } from './GroceryItem';

FactoryMate.define(GroceryItem, (): GroceryItem => {
    const groceryItem = new GroceryItem();
    groceryItem.groceryName = 'crispy chips';
    return groceryItem;
});

FactoryMate.define() takes two arguments:

  1. The class being registered
  2. An initialization function - a function that returns the 'template' of the object.

Every time FactoryMate is used to create an instance of the registered object, that instance's properties will have the same values as those defined in template. To build an instance of a registered class:

const groceryItem: GroceryItem = FactoryMate.build(GroceryItem.name);

console.log(JSON.stringify(groceryItem)); // '{"groceryName":"crispy chips"}'

Factory Classes

It is recommended that a factory class be created for each domain object. Factory classes can be created by annotating the new class with @FactoryMateAware and calling the static FactoryMate.define() method in that class:

// GroceryItemFactory.ts
import { FactoryMate, FactoryMateAware } from 'factory-mate';
import { GroceryItem } from './GroceryItem';

@FactoryMateAware
export class GroceryItemFactory {
    // The @FactoryMateAware annotation will automatically call the define() funtion at runtime
    public define() { 
        FactoryMate.define(GroceryItem, (): GroceryItem => {
            const groceryItem = new GroceryItem();
            groceryItem.groceryName = 'crispy chips';
            return groceryItem;
        });
    }
}

Additonal Building Methods

Overriding a Template's Variables

If for specific tests there is a need to override one or more variables in the template, this can be accomplished via an optional parameter to build:

const groceryItem: GroceryItem = FactoryMate.build(GroceryItem.name,
      (u: GroceryItem) => {
        u.groceryName = 'chunky cookies';
      });
Building Many of the Same Object

To build several objects of the same type:

const groceryItems: GroceryItem[] = FactoryMate.buildMany(GroceryItem.name, 3);

Example

An example project using FactoryMate can be found here FactoryMateConsumer

License

FactoryMate is distributed under the MIT license

Keywords

FAQs

Package last updated on 09 Jul 2017

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