Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/vdsabev/sneer

Type-safe mocking utilities for reliable unit tests
https://github.com/vdsabev/sneer

mocking testing typescript unit-testing

Last synced: about 1 month ago
JSON representation

Type-safe mocking utilities for reliable unit tests

Awesome Lists containing this project

README

        

[![downloads][downloads-badge]][npmcharts]
[![version][version-badge]][package]
[![MIT License][license-badge]][license]


Sneer 😏


Type-safe mocking utilities

## Why?
You want to write test mocks in TypeScript while preserving type safety and getting meaningful type errors.

## Installation
```
npm install sneer -D
```

## Usage
```ts
import {
mockPartial,
mockPartialArray,
mockRecursivePartial,
mockRecursivePartialArray,
} from 'sneer';
```

### `mockPartial`
### `mockPartialArray`
Casts an object (or array of objects) to a type `T` while only requiring a partial implementation of the type to be passed. This allows us to easily mock an object while writing tests without having to define all of the properties of an object.

Example:
```ts
const store = mockPartial>({
select: jest.fn((selector: (state: State) => any) => Observable.of(selector(state))),
});
```

### `mockRecursivePartial`
### `mockRecursivePartialArray`
Same as `mockPartial` and `mockPartialArray`, but recursively partial 🙃

Example:
```ts
const svg = mockRecursivePartial({
x: {
baseVal: { value: 1 }
}
});
```

[version-badge]: https://img.shields.io/npm/v/sneer.svg?style=flat-square
[package]: https://www.npmjs.com/package/sneer
[downloads-badge]: https://img.shields.io/npm/dm/sneer.svg?style=flat-square
[npmcharts]: https://npmcharts.com/compare/sneer
[license-badge]: https://img.shields.io/npm/l/sneer.svg?style=flat-square
[license]: https://github.com/vdsabev/sneer/blob/master/LICENSE.md