TS auto mock
HomeInstallationCreate mockCreate mock listCreate hydrated mockRegister mockExtensionTypes supportedPrimitiveInterfacesInterfaces with call signaturesInterfaces with construct signaturesClassesMethodsLiteralEnum (it will select the first value)FunctionObjectKey inKey in keyofOptionalTypescript libraries (Array, Number... ecc)TupleUnionDictionaryExtendsGenericsIntersectionConstructorTypeTypeQueryIndexedAccessTypeTypes not supportedConfigPerformanceDefinitely TypedLocal development

Types supported

The library tries to convert the type argument passed to createMock so you don't have to mock them manually.

Primitive

number // 0
string // ""
boolean // false
boolean[] // []
void // undefined
null // null
undefined // undefined
never // undefined

Interfaces

interface Person {
id: string
name: string
}
/*
{
id: "",
name: ""
}
*/

Interfaces with call signatures

For overload methods it will use the first one

interface Person {
(): number
(): string
name: string
}
const mock = createMock<Person>();
mock() // 0
mock.name // ""

Interfaces with construct signatures

For overload constructors it will use the first one

interface PersonWithHat {
hatSize: number;
}
interface PersonWithoutHat {
shirtSize: number;
}
interface Person {
new (hatSize: number): PersonWithHat
new (): PersonWithoutHat
name: string
}
const mock = createMock<Person>();
new mock() // { hatSize: 0 }
mock.name // ""

Classes

class Person {
private _id: string;
name: string
}
/*
{
name: ""
}
*/

Methods

interface InterfaceWithMethod {
method(): string
}
/*
{
method: () => {
return ""
}
}
*/

Literal

type Data = {
id: "I am a specific string",
number: 2
}
/*
{
id: "I am a specific string",
number: 2
}
/*

Enum (it will select the first value)

enum Direction {
LEFT,
RIGHT
}
interface WithEnum {
direction: Direction
}
/*
{
direction: DIRECTION.LEFT
}
/*

Function

const mock = createMock<() => string>();
mock // () => ""

Object

const mock = createMock<{ a: string }>();
mock // { a: "" }

Key in

type Keys = "a" | "b";
type myType = { [key in Keys]: string }
const mock = createMock<myType>();
mock // { a: "", b: "" }

Key in keyof

type Keys = {
a: number;
}
type keyInKeyof = { [key in keyof Keys]: string }
const mock = createMock<keyInKeyof>();
mock // { a: "" }

Optional

The property is not defined in the mock

class MyClass {
test?: string;
}
const mock = createMock<MyClass>();
mock // { }

Typescript libraries (Array, Number... ecc)

This is a special case. The library tries to convert to the most useful type.

class MyClass {
array: Array<number>; // []
number: Number; // 0
promise: Promise<string>; // a promise that will resolve an empty string Promise.resolve("")
map: Map<string, string>; // new Map()
set: Set<string>; // new Set()
}

Tuple

class MyClass {
tuple: [string, number];
}
const mock = createMock<MyClass>();
mock // { tuple: ['', 0] }

Union

It will convert to the first type of the union unless undefined/void are part of the union, in that case it will convert to undefined, being the "smallest type"

class MyClass {
union: string | number;
}
const mock = createMock<MyClass>();
mock // { union: "" }
class MyClass1 {
union: string | undefined;
}
const mock1 = createMock<MyClass1>();
mock1 // { union: undefined }

Dictionary

type Dictionary<T> = {
[key: string]: T;
}
const mock = createMock<Dictionary<Interface>>();
mock // {}

Extends

interface Keys {
a: string;
}
interface Interface extends Keys {
b: boolean;
}
const mock = createMock<Interface>();
mock // { a: "", b: "" }

Generics

interface WithGeneric<T>{
generic: T
}
const mock = createMock<WithGeneric<string>>();
mock // { generic: "" }

Intersection

interface IntersectionA {
a: string;
}
interface IntersectionB {
b: number;
}
interface Interface {
intersection: IntersectionA & IntersectionB,
}
const mock = createMock<Interface>();
mock // { intersection: { a: "", b: 0 } }

ConstructorType

interface Test {
a: string;
}
const mockType = createMock<new () => Test>();
const mock = new mockType();
mock = // { a: "" }

TypeQuery

enum AnEnum {
a,
b = 'something',
}
const mock = createMock<typeof AnEnum>();
mock.a // 0
mock.b // 'something'
mock[0] // 'a'
class AClass {
a: string
}
const mockClass = createMock<typeof AClass>();
new mockClass().a // ''
function AFunction(): number;
const mockFunction = createMock<typeof AFunction>();
mockFunction() // 0

IndexedAccessType

class Class {
a: string
}
type KeyOf = { [key in keyof Class]: Class[key] };
const mock = createMock<KeyOf>();
mock.a // ''