This library generates mock data from Typescript interfaces.
Generate Mock Data from Typescript Interfaces. Includes support for faker
Install ts-mock-generator
via npm:
npm i @mangm/ts-mock-generator
This package internally uses ts-morph.
// 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("current-file.ts", "Hero");
console.log(data);
// Output
{
"sortOrder": -1,
"heroTypeIds": [-1],
"countryId": -1
}
By default, the generator has predefined settings.
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 |
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("current-files.ts", "Hero", optionalConfig);
Specify the location of the tsconfig. Relative to the root directory.
const generator = new Generator("./tsconfig.json");
The main function to generate the mock objects
const data = generator.generate("current-files.ts", "Hero", config);
Option | Description | Type | Required |
---|---|---|---|
SourceFileName | The source file name. Include the .ts |
string | true |
interfaceName | The interface name. | string | true |
config | Refer to section Default Generator Configuration | Configuration | false |
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 |
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 |
// 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>("recursive-interface.ts", "HeroRecursive");
console.log("::Default::", data);
// OUTPUT >> ::Default::
// {
// hero: {
// name: 'MOCK'
// }
// }
data = generator.generate("recursive-interface.ts", "HeroRecursive", {
includeAllProps: true
});
console.log("::IncludeAllProps::", data);
// OUTPUT >> ::IncludeAllProps::
// {
// name: 'MOCK',
// hero: {
// name: 'MOCK'
// }
// }
data = generator.generate("recursive-interface.ts", "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("recursive-interface.ts", "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("recursive-interface.ts", "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("recursive-interface.ts", "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']
// }
// }