jest-return-value-hooks
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

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 only supports Promise<void> or undefined as return values. However, you might want to pass different values back such as Observables from 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

{
    "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

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 project, which is released under the MIT License, however adaptions have been made to provide hooks to users.

Readme

Keywords

Package Sidebar

Install

npm i jest-return-value-hooks

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

12.6 kB

Total Files

23

Last publish

Collaborators

  • lucabecker