Skip to content

Typescript transformer to unlock automatic mock creation for interfaces and classes

License

Notifications You must be signed in to change notification settings

joshuavial/ts-auto-mock

 
 

Repository files navigation

Ts Auto Mock

CircleCI Greenkeeper badge npm version Downloads

A Typescript transformer that will allow you to create mock for any types (Interfaces, Classes, ...) without need to create manual fakes/mocks.

Let's have a look.

Requirements

typescript@^3.2.2

Installation

A Transformer needs to be provided at compile time. There are different ways to do it. Please read the following guide to find your configuration

Usage

Create mock

import { createMock } from 'ts-auto-mock';

interface Person {
  id: string;
  getName(): string;
  details: {
      phone: number
  }
}
const mock = createMock<Person>();
mock.id // ""
mock.getName() // ""
mock.details // "{phone: 0} "
Default values

You can also define default values to overrides specific fields You dont have to provide the entire interface, just a partial of the one to mock

import { createMock } from 'ts-auto-mock';

interface Person {
  id: string;
  getName(): string;
  details: {
      phone: number
  }
}
const mock = createMock<Person>({
details: {
    phone: 07423232323
}
});
mock.id // ""
mock.getName() // ""
mock.details // "{phone: 07423232323} "

Create mock list

createMock list it will create a list of mocks automatically

import { createMockList } from 'ts-auto-mock';

interface Person {
  id: string;
}
const mockList = createMockList<Person>(2);
mockList.length // 2
Default values

You can define a function to overrides specific fields The function will have access to the current index

import { createMockList } from 'ts-auto-mock';

interface Person {
  id: string;
}
const mockList = createMockList<Person>(2, (index: number) => {
    return {
        id: "id" + index
    }
});
mockList[0].id // id0
mockList[1].id // id1

Type Examples

The library try to convert the type given to createMock so you dont need to create concrete mock manually. Open this link to see more examples

Extension

The library allows you to extends some functionality to work nicely with framework like jasmine or jest Open this link to see more examples

Options

tsAutoMockTransformer(program: ts.Program, options: TsAutoMockOptions)

interface TsAutoMockOptions {
    debug: boolean | 'file' | 'console'
}

options:

Name Default Description
debug false When set to true or console it will log to the console
When set to file it will log to a file (tsAutoMock.log)

Authors

License

This project is licensed under the MIT License

About

Typescript transformer to unlock automatic mock creation for interfaces and classes

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 96.4%
  • JavaScript 3.6%