# jest-ts-auto-mock

> This is a library that extend [ts-auto-mock](https://github.com/Typescript-TDD/ts-auto-mock) to be used with jasmine

Latest version **2.1.0** (published 2022-03-06) · ISC license · 0 weekly downloads

## Install

```sh
npm install jest-ts-auto-mock
pnpm add jest-ts-auto-mock
yarn add jest-ts-auto-mock
bun add jest-ts-auto-mock
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2022-03-06 |
| First published | 2019-03-10 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 3.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 169 |
| Maintainers | uittorio |

## Links

- npm: https://www.npmjs.com/package/jest-ts-auto-mock
- Repository: https://github.com/Typescript-TDD/jest-ts-auto-mock
- Homepage: https://github.com/Typescript-TDD/jest-ts-auto-mock.git#readme
- Issues: https://github.com/Typescript-TDD/jest-ts-auto-mock.git/issues
- npm.io page: https://npm.io/package/jest-ts-auto-mock

## Recent versions

- 2.1.0 (latest) — 2022-03-06
- 2.0.0 — 2021-02-06
- 1.0.12 — 2020-12-12
- 1.0.11 — 2020-02-08
- 1.0.10 — 2020-01-01
- 1.0.9 — 2019-12-27
- 1.0.8 — 2019-12-22
- 1.0.7 — 2019-11-30
- 1.0.6 — 2019-11-26
- 1.0.5 — 2019-11-23
- 1.0.4 — 2019-11-23
- 1.0.3 — 2019-10-29
- 1.0.2 — 2019-08-18
- 1.0.1 — 2019-07-07
- 1.0.0 — 2019-04-27
- … 12 more at https://npm.io/package/jest-ts-auto-mock/versions

## README

# Jest Ts Auto Mock

[![Actions Status](https://github.com/Typescript-TDD/jest-ts-auto-mock/workflows/Master/badge.svg)](https://github.com/Typescript-TDD/jest-ts-auto-mock/actions) 



This is a library that extend [ts-auto-mock](https://github.com/Typescript-TDD/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<br>
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 
    ```ts   
    {
      "compilerOptions": {
        ...
        "plugins": [
          { "transform": "ts-auto-mock/transformer", "cacheBetweenTests": false }
        ]
      }
    }
    ```
    
    - Enable ttypescript into the ts-jest configuration
    ```ts              
       ...
       "globals": {
          "ts-jest": {
            "compiler": "ttypescript"
          }
        }
    ```

- provide jest-ts-auto-mock config before your test
```ts
"jest": {
    ...
    "setupFiles": [
      "<rootDir>/config.ts"
    ]
   ...
  },
```

- config file
```ts
import 'jest-ts-auto-mock'

```

## Examples
[ts-jest-ttypesctipt](examples/ts-jest-ttypescript)

## Usage
1) create an interface
```ts
interface Interface {
    methodToMock: () => string
}
```
2) create a mock
```ts
const mock: Interface = createMock<Interface>();
```
3) get the method mock 

You can get the method spy in 2 different ways

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

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

## Authors

* [**Vittorio Guerriero**](https://github.com/uittorio)
* [**Giulio Caprino**](https://github.com/pmyl)

## License

This project is licensed under the MIT License

---
_Source: https://npm.io/package/jest-ts-auto-mock · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
