Giter Site home page Giter Site logo

jest-ts-auto-mock's Introduction

Jest Ts Auto Mock

Actions Status

This is a library that extend ts-auto-mock to be used with jest

The intention of the library is to automatically assign jest mock to functions giving you type safety

Requirements

typescript@^3.2.2
ts-jest@>=24 <27

Installation

  • A Transformer needs to be provided at compile time. We need to tell ts-jest to use ttypescript that allow us to use a transformer.

IMPORTANT:

  • set "cacheBetweenTests" as false

  • Add the transformer to your ts config

    {
      "compilerOptions": {
        ...
        "plugins": [
          { "transform": "ts-auto-mock/transformer", "cacheBetweenTests": false }
        ]
      }
    }
    • Enable ttypescript into the ts-jest configuration
       ...
       "globals": {
          "ts-jest": {
            "compiler": "ttypescript"
          }
        }
  • provide jest-ts-auto-mock config before your test

"jest": {
    ...
    "setupFiles": [
      "<rootDir>/config.ts"
    ]
   ...
  },
  • config file
import 'jest-ts-auto-mock'

Examples

ts-jest-ttypesctipt

Usage

  1. create an interface
interface Interface {
    methodToMock: () => string
}
  1. create a mock
const mock: Interface = createMock<Interface>();
  1. get the method mock

You can get the method spy in 2 different ways

Through method

import { On, method } from "ts-auto-mock/extension";
const mockMethod: Jest.Mock = On(mock).get(method(mock => mock.methodToMock));

Through string

import { On, method } from "ts-auto-mock/extension";
const mockMethod: Jest.Mock = On(mock).get(method('methodToMock'));
  1. trigger the method
someMethodThatWillTriggerInterfaceA();
expect(mockMethod).toHaveBeenCalled();

Authors

License

This project is licensed under the MIT License

jest-ts-auto-mock's People

Contributors

afonsojramos avatar dependabot[bot] avatar greenkeeper[bot] avatar okuuva avatar qdelettre avatar uittorio avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.