# jest-return-value-hooks

> Return any value from your tests instead of just Promises or undefined.

Latest version **1.0.0** (published 2021-01-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install jest-return-value-hooks
pnpm add jest-return-value-hooks
yarn add jest-return-value-hooks
bun add jest-return-value-hooks
```

## 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.0 |
| Published | 2021-01-23 |
| First published | 2021-01-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 12.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Luca Becker |
| Maintainers | lucabecker |
| Keywords | jest, rxjs |

## Links

- npm: https://www.npmjs.com/package/jest-return-value-hooks
- npm.io page: https://npm.io/package/jest-return-value-hooks

## Dependencies (3)

- [co](https://npm.io/package/co.md) 4.6.0
- [rxjs](https://npm.io/package/rxjs.md) 6.6.3
- [is-generator-fn](https://npm.io/package/is-generator-fn.md) 2.1.0

## Recent versions

- 1.0.0 (latest) — 2021-01-23

## README

# jest-return-value-hooks

This project allows you to hook into the handling of the
return values of your tests, and the lifecycle functions.

Why would you want this? Currently, [Jest](https://jestjs.io/)
only supports `Promise<void>` or `undefined` as return values.
However, you might want to pass different values back such
as Observables from [RxJS](https://github.com/ReactiveX/rxjs).
You can implement the `JestFunctionHook` interface and then pass
it along to the two functions `patchLifeCycleFunctions` and
`patchTestFunctions`. They accept multiple hooks at once to avoid
overhead.

## How to use

Add this to your Jest configuration

```json
{
    "setupFilesAfterEnv": ["jest-return-value-hooks"]
}
```

and your Jest tests will accept Observables as an additional
return value. As an alternative you can also implement the
aforementioned `JestFunctionHook` and call the mentioned methods
like so

```typescript
import { patchTestFunctions, patchLifeCycleFunctions, JestFunctionHook } from 'jest-return-value-hooks';
import { Observable, isObservable } from 'rxjs';

const hook: JestFunctionHook = {
    match: isObservable,
    map: (ob: Observable<unknown>, timeoutAfter: number) => new Promise.resolve(),
};

patchLifeCycleFunctions(hook);
patchTestFunctions(hook);
```

and then point Jest the same way as mentioned above to your file.

## Credit

Parts of this code stem originally from the
[jest-jasmine2](https://github.com/facebook/jest)
project, which is released under the MIT License, however adaptions
have been made to provide hooks to users.

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