jest-mock-promise
Advanced tools
Comparing version 1.1.9 to 1.1.10
{ | ||
"name": "jest-mock-promise", | ||
"version": "1.1.9", | ||
"version": "1.1.10", | ||
"description": "Synchronous Promise Mock for testing with Jest", | ||
@@ -5,0 +5,0 @@ "main": "dist/jest-mock-promise.js", |
# What's this? | ||
This is yet another _synchronous_ implementation od JavaScript Promise. *JestMockPromise* implements the same API as native JavaScript Promise, which added benefit that it can be resolved *"from outside"* (see the examples listed below). | ||
This a _synchronous_ implementation od JavaScript Promise. *JestMockPromise* implements the same API as native JavaScript Promise, with two added benefits: | ||
* it works synchronously - it calls the handler functions (`then`, `catch`, `finally`) right away (examples below clarify this) | ||
* it exposes the `resolve` and `reject` functions as instance methods allowig the promise to be resolved outside of _executor function_ | ||
Although its originally created to make writing unit tests easier, it can also be used outside the unit testing context. In fact it doesn't even contain any unit-testing-specific features and apart from it's name it has nothing to do with _Jest_. | ||
Although its originally created to make writing unit tests easier, it can also be used outside the unit testing context. | ||
# What's in this document? | ||
* [How does it work?](#how-does-it-work) | ||
* [How to use it?](#how-to-use-it) | ||
* [API](#api) | ||
* [JestMockPromise vs native Promise](#jestmockpromise-vs-native-promise) | ||
* [An unit testing example?](#an-unit-testing-example) | ||
* [First example - Traditional async test](#first-example---traditional-async-test) | ||
* [Second example - Applying the synchronous Promise](#second-example---applying-the-synchronous-promise ) | ||
* [Third example - Mocking `fetch`](#third-example---mocking-fetch) | ||
* [API](#api) | ||
@@ -32,14 +34,13 @@ # TL; DR | ||
``` | ||
Having them attached to the instance enables us to call them outside the callback function, which makes our code much more readable: | ||
Having `resolve` and `reject` attached as instance methods enables us to call them outside the callback function, which makes our code much more readable: | ||
```javascript | ||
let promise = new Promise(); | ||
promise.resolve(1,2); | ||
// resolving a promise | ||
promise.resolve(1, 2); | ||
``` | ||
# How does it work - Examples | ||
# JestMockPromise vs native Promise | ||
It works the same way a normal Promise would, with the exception that does it right away (synchronously) and not at later time (async). | ||
Let's jump right in and see how to use this: | ||
Let's jump right in and see an example: | ||
```javascript | ||
@@ -107,3 +108,3 @@ import JestMockPromise from "../lib/jest-mock-promise"; | ||
# How to use it? | ||
# An unit testing example | ||
@@ -110,0 +111,0 @@ Synchronous Promise was created to simplify unit testing an async component. So in the next two examples we'll have a look at how we can do just that - simplify a unit test. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58103
209