interface-forge
TypeScript icon, indicating that this package has built-in type declarations

1.6.1 • Public • Published

NPM

Coverage Duplicated Lines (%) Maintainability Rating Reliability Rating Technical Debt Code Smells Bugs

All Contributors

Interface-Forge allows you to gracefully generate dynamic mock data and static fixtures in TypeScript.


Installation

yarn add --dev interface-forge

Or

npm install --save-dev interface-forge

Basic Example

To create a factory you need some TS types:

// types.ts

export interface UserProfile {
    profession: string;
    gender: string;
    age: number;
}

export interface Cat {
    name: string;
}

export interface User {
    firstName: string;
    lastName: string;
    email: string;
    profile: UserProfile;
    cats: Cat[];
}

Pass the desired type as a generic argument when instantiating TypeFactory, alongside default values for the factory:

// factories.ts
import { TypeFactory } from 'interface-forge';
import { User } from './types';

// i is type number
const UserFactory = new TypeFactory<User>((i) => ({
    firstName: 'John',
    lastName: 'Smith',
    email: 'js@example.com',
    profile: {
        profession: 'cook',
        gender: 'male',
        age: 27 + i,
    },
    cats: [],
}));

Then use the factory to create an object of the desired type in a test file:

// User.spec.ts

describe('User', () => {
    // you can pass override values when calling build
    const user = UserFactory.buildSync({
        firstName: 'Johanne',
        profile: {
            profession: 'Journalist',
            gender: 'Female',
            age: 31,
        },
        cats: [],
    });
    // user == {
    //     firstName: "Johanne",
    //     lastName: "Smith",
    //     email: "js@example.com",
    //     profile: {
    //         profession: "Journalist",
    //         gender: "Female",
    //         age: 31
    //     },
    //     cats: []
    // }
    // ...
});

Take a look at our documentation or read the introduction article .

Contributing & Contributors ✨

Thanks goes to these wonderful people (emoji key):


Na'aman Hirschfeld

💻 📖 🚧

Damian

💻 📖 🚧

Yannis Kommana

💻

This project follows the all-contributors specification. Contributions of any kind welcome! Please see the contributing guide.

Package Sidebar

Install

npm i interface-forge

Weekly Downloads

1,793

Version

1.6.1

License

MIT

Unpacked Size

56.8 kB

Total Files

25

Last publish

Collaborators

  • nhirschfeld