2.1.0 • Published 2 years ago

jest-ts-auto-mock v2.1.0

Weekly downloads
13,101
License
ISC
Repository
github
Last release
2 years ago

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
}

2) create a mock

const mock: Interface = createMock<Interface>();

3) 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'));

4) trigger the method

someMethodThatWillTriggerInterfaceA();
expect(mockMethod).toHaveBeenCalled();

Authors

License

This project is licensed under the MIT License

2.1.0

2 years ago

2.0.0

3 years ago

1.0.12

3 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago