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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@mangm/ts-mock-generator

v0.10.2

Published

Mock Generator from Typescript Interfaces

Downloads

5

Readme

Typescript Mock Generator

Build Status npm version

This library generates mock data from Typescript interfaces.

Generate Mock Data from Typescript Interfaces. Includes support for faker

Installation

Install ts-mock-generator via npm:

npm i @mangm/ts-mock-generator

This package internally uses ts-morph.

Basic Usage

// current-file.ts

import faker = require("faker");
import { Generator, Configuration } from "@mangm/ts-mock-generator";

const generator = new Generator("./tsconfig.json");

export interface Hero {
    id: number;
    name: string;
    code: string;
    sortOrder?: number;
    heroTypeIds?: number[];
    countryId?: number;
}

const data = generator.generate("Hero");
console.log(data);

// Output
{
    "sortOrder": -1,
    "heroTypeIds": [-1],
    "countryId": -1
}

Defaults Configurations

By default, the generator has predefined settings.

Default Primitive Values

The following are the default primitive values. These can be overridden by defining one or more properties in primitiveValues configuration below.

| Property | Default | |-----------|----------| | string[] | ["MOCK"] | | string | "MOCK" | | number[] | [-1] | | number | -1 | | boolean[] | [true] | | boolean | true |

Default Generator Configuration

The configuration is preset as in constant DEFAULT_CONFIGURATION.

| Property | Description | Default | |------------------|-----------------------------------------------------------------------------------------------------------------------|----------| | includeAllProps | include all properties, optional and not | false | | maxRecursiveLoop | Limits the recursive iterations for the same interface as shown in examples with interface HeroRecursive | 1 | | primitiveValues | Default Primitive Value Dictionary as above | As above | | fieldValues | Custom values for each key in an interface. | {} |

const data = generator.generate("Hero", optionalConfig);

API

Creating the Generator

Specify the location of the tsconfig. Relative to the root directory.

const generator = new Generator("./tsconfig.json");

Generate()

The main function to generate the mock objects

const data = generator.generate("Hero", config);

| Option | Description | Type | Required | |----------------|--------------------------------------------------------------------------------------|---------------|----------| | interfaceName | The interface name. | string | true | | config | Refer to section Default Generator Configuration | Configuration | false |

Add < T >()

The add function is used to add keys to the Fields Value Dictionary in the Default Generator Configuration

interfaceName: string, propertyKey: keyof T, propertyValue: string

| Option | Description | Type | |---------------|-------------------------------------------------------|---------| | interfaceName | The interface name | string | | propertyKey | the property key. Key is dependant on the interface T | keyof T | | propertyValue | any value | any |

Remove < T >()

The remove funtion is used to remove keys to the Fields Value Dictionary in the sDefault Generator Configuration

| Option | Description | Type | |---------------|-------------------------------------------------------|---------| | interfaceName | The interface name | string | | propertyKey | the property key. Key is dependant on the interface T | keyof T |

Examples

// recursive-interface.ts

import faker = require('faker');
import { Generator } from '../generator';

export interface HeroRecursive {
    name: string;
    altName: string;
    codes: string[];
    hero?: HeroRecursive
}

const generator = new Generator("tsconfig.json");

// Basic Usage Generate Interface using default parameters
let data = generator.generate<HeroRecursive>("HeroRecursive");

console.log("::Default::", data);

// OUTPUT >> ::Default::
// {
//     hero: {
//         name: 'MOCK'
//     }
// }
data = generator.generate("HeroRecursive", {
    includeAllProps: true
});

console.log("::IncludeAllProps::", data);

// OUTPUT >> ::IncludeAllProps::
// {
//     name: 'MOCK',
//     hero: {
//         name: 'MOCK'
//     }
// }
data = generator.generate("HeroRecursive", {
    includeAllProps: true,
    maxRecursiveLoop: 2
})

console.log("::Max Recursion = 2::", data);

// OUTPUT >> ::Max Recursion = 2::
// {
//     name: 'MOCK',  
//     hero: {
//         name: 'MOCK',
//         hero: {
//             name: 'MOCK'
//         }
//     }
// }
// Using Field Values to add customised generated data.
generator.add<HeroRecursive>("HeroRecursive", "name", faker.name.findName());

// Preset Enum Value
generator.add<HeroRecursive>("HeroRecursive", "heroType", HeroType.Rock);

data = generator.generate("HeroRecursive", {
	includeAllProps: true
})

console.log("::Field Values::", data);

// Remove Preset values
generator.remove<HeroRecursive>("HeroRecursive", "name");
generator.remove<HeroRecursive>("HeroRecursive", "heroType");

// OUTPUT >> ::Field Values:: 
// {
// 	name: 'Jarrell Williamson',
// 	altName: 'MOCK',
// 	codes: ['MOCK'],
//  heroType: 3
// 	hero:
// 	{
// 		name: 'Jarrell Williamson',
// 		altName: 'MOCK',
// 		codes: ['MOCK'],
//      heroType: 3
// 	}
// }
// Using Primitive Values. Currently support is for string, number and boolean
data = generator.generate("HeroRecursive", {
    primitiveValues: {
        "string[]": ["TEST"],
        "string": "TEST",
        "number[]": [-66],
        "number": -66,
        "boolean[]": [false],
        "boolean": false,
    }
})

console.log("::Primitive Values::", data);

// OUTPUT >> ::Primitive Values::
// {
//     hero: {
//         name: 'TEST'
//     }
// }
// Using Field Values, Primitive Values and Default Values (Fallbacks)

generator.add<HeroRecursive>("HeroRecursive", "name", faker.name.findName());

data = generator.generate("HeroRecursive", {
    includeAllProps: true,
    primitiveValues: {
        "string[]": ["TEST"]
    }
})

console.log("::Fallbacks::", data);
generator.remove<HeroRecursive>("HeroRecursive", "name");

// OUTPUT >>
// {
//     name: 'Dion Jacobson',
//     altName: 'MOCK',
//     codes: ['TEST'],
//     hero: {
//         name: 'Dion Jacobson',
//         altName: 'MOCK',
//         codes: ['TEST']
//     }
// }