# jasmine-automock

> A small library used to mock an instance of a class using jasmine, perfect for mocking injected dependencies.

Latest version **1.0.0** (published 2020-12-20) · ISC license · 0 weekly downloads

## Install

```sh
npm install jasmine-automock
pnpm add jasmine-automock
yarn add jasmine-automock
bun add jasmine-automock
```

## 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 | 1.0.0 |
| Published | 2020-12-20 |
| First published | 2020-12-20 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Daniel Cornock |
| Maintainers | danielc7150 |
| Keywords | jasmine, testing, automock |

## Links

- npm: https://www.npmjs.com/package/jasmine-automock
- npm.io page: https://npm.io/package/jasmine-automock

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2020-12-20

## README

# jasmine-automock

This package allows you to automock the methods on a class instance. Mostly useful when using dependency injection, but can come in useful when mocking the return of a factory too. Works perfectly with Angular testing.

## Usage

Import using:

```typescript
import { autoMock } from 'jasmine-automock';
```

Create a mock using:

```typescript
let mockClass: jasmine.spyObj<ClassConstructor> = autoMock(ClassConstructor);
```

This will create a jasmine spy object using the instance methods on the class that you pass in, allowing you to check that those methods have been called, as well as mocking what they should return.

If using TypeScript, the `autoMock` function returns a `jasmine.spyObj<T>` type, with `T` being the class that you passed in. This allows you to access all of the class methods, as well as the mocking methods that appear on a mocked function.

For a more detailed example (using Angular), see below:

## Example

```typescript
import { autoMock } from 'jasmine-automock';

/* Consumer of default angular http client - class that we are testing */
@Injectable()
class TodoHttpService {
  constructor(private httpClient: HttpClient) {}

  public getAllTodos() {
    return this.httpClient.get('api-url/todos');
  }
}

/* Test suite */
describe('TodoHttpService', () => {
  let service: TodoHttpService, mockHttpClient: jasmine.spyObj<HttpClient>;

  beforeEach(() => {
    /* We can mock angular services too! */
    mockHttpClient = autoMock(HttpClient);

    TestBed.configureTestingModule({
      providers: [{ provide: HttpClient, useValue: mockHttpClient }]
    });

    service = TestBed.inject(HttpService);
  });

  describe('getAllTodos', () => {
    let result;

    beforeEach(() => {
      mockHttpClient.get.and.returnValue('todos');
      result = service.getAllTodos();
    });

    it('should fetch the todos', () => {
      expect(mockHttpClient.get).toHaveBeenCalledWith('api-url/todos');
    });

    it('should return the todos', () => {
      expect(result).toBe('todos');
    });
  });
});
```

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