# jest-auto-stub

> Automatically create type-safe fully stubbed instances of classes or interfaces.

Latest version **1.0.8** (published 2020-11-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install jest-auto-stub
pnpm add jest-auto-stub
yarn add jest-auto-stub
bun add jest-auto-stub
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.8 |
| Published | 2020-11-23 |
| First published | 2019-02-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 10 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Greg Bacchus |
| Maintainers | gregbacchus |
| Keywords | jest, typescript, stub, mock, auto |

## Links

- npm: https://www.npmjs.com/package/jest-auto-stub
- Repository: https://github.com/gregbacchus/jest-auto-stub
- Homepage: https://github.com/gregbacchus/jest-auto-stub#readme
- Issues: https://github.com/gregbacchus/jest-auto-stub/issues
- npm.io page: https://npm.io/package/jest-auto-stub

## Dependencies (5)

- [jest](https://npm.io/package/jest.md) ^26.6.3
- [tslint](https://npm.io/package/tslint.md) ^6.1.3
- [ts-jest](https://npm.io/package/ts-jest.md) ^26.4.4
- [typescript](https://npm.io/package/typescript.md) ^4.1.2
- [@types/jest](https://npm.io/package/@types/jest.md) ^26.0.15

## Alternatives

- [pagerjs](https://npm.io/package/pagerjs.md) — 60 weekly downloads
- [whistle.savefor-mock](https://npm.io/package/whistle.savefor-mock.md) — 4 weekly downloads
- [sinon-typed-stub](https://npm.io/package/sinon-typed-stub.md) — 0 weekly downloads
- [named-patch](https://npm.io/package/named-patch.md) — 0 weekly downloads
- [@anil-labs/factory](https://npm.io/package/@anil-labs/factory.md) — 0 weekly downloads

## Recent versions

- 1.0.8 (latest) — 2020-11-23
- 1.0.7 — 2020-04-06
- 1.0.6 — 2020-03-09
- 1.0.5 — 2020-03-09
- 1.0.4 — 2019-07-07
- 1.0.3 — 2019-02-17
- 1.0.2 — 2019-02-13
- 1.0.1 — 2019-02-13

## README

# jest-auto-stub

Automatically create type-safe fully stubbed instances of classes or interfaces.

## Installation

```
npm install jest-auto-stub
```

## Usage

```typescript
import { reveal, stub } from 'jest-auto-stub';

interface MyInterface {
  foo(): string;
}

class MyClass {
  bar(): string {
    return 'foo';
  }
}

describe('stub', () => {
  describe('interface', () => {
    it('will return an object', () => {
      const s = stub<MyInterface>();
      expect(s).toBeDefined();
      expect(s).toMatchObject({});
    });

    it('will track calls to stubbed methods', () => {
      const s = stub<MyInterface>();
      s.foo();
      expect(s.foo).toHaveBeenCalled();
    });

    it('will be able to provide mock results', () => {
      const s = stub<MyInterface>();

      reveal(s).foo.mockReturnValueOnce('bar');

      const result = s.foo();

      expect(s.foo).toHaveBeenCalled();
      expect(result).toBe('bar');
    });
  });

  describe('class', () => {
    it('will return an object', () => {
      const s = stub<MyClass>();
      expect(s).toBeDefined();
      expect(s).toMatchObject({});
    });

    it('will track calls to stubbed methods', () => {
      const s = stub<MyClass>();
      s.bar();
      expect(s.bar).toHaveBeenCalled();
    });

    it('will be able to provide mock results', () => {
      const s = stub<MyClass>();

      {
        const mock = reveal(s);
        mock.bar.mockReturnValueOnce('hello');
        mock.bar.mockReturnValueOnce('world');
      }

      const result = [s.bar(), s.bar()];

      expect(s.bar).toHaveBeenCalled();
      expect(result[0]).toBe('hello');
      expect(result[1]).toBe('world');
    });
  });
});
```

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