@typescripttdd/ts-auto-mock
TypeScript icon, indicating that this package has built-in type declarations

1.4.1 • Public • Published

Ts Auto Mock

Actions Status CircleCI npm version Downloads Greenkeeper badge

slack Need help? Join us on Slack link

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

Type Not Supported

The library will convert to null when the type is not supported. Open this link to see what is not supported

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';
    cacheBetweenTests: boolean;
}

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)
cacheBetweenTests true When set to true it will reuse mocks between different tests
When set to false it create new mocks for each different tests

Debug

We currently support

  • Logs for not supported types It will log any not supported type automatically converted to null. This is useful to report an issue or to investigate a potential bug

cacheBetweenTests

One of the main functionality of ts auto mock is to generate mocks and cache them.

Mocks are currently created in the test file making tests to depend to each other

Example:

  • test1.test.ts has a createMock of Interface.
  • test2.test.ts has a createMock of Interface.
  • test1.test.ts will have the registration of Interface mock
  • test2.test.ts will have a registration import.

If test2 run in a different context than test1 it will not be able to access to the same mock.

Set this property to false when your test run in different context.

We are working on an issue to make sure tests do not depend to each other but they will still take advance of a cache system

Changelog

Authors

License

This project is licensed under the MIT License

Readme

Keywords

Package Sidebar

Install

npm i @typescripttdd/ts-auto-mock

Weekly Downloads

0

Version

1.4.1

License

ISC

Unpacked Size

173 kB

Total Files

26

Last publish

Collaborators

  • uittorio