jest-mock-promise
Advanced tools
Comparing version 1.0.15 to 1.0.17
{ | ||
"name": "jest-mock-promise", | ||
"version": "1.0.15", | ||
"version": "1.0.17", | ||
"description": "Synchronous Promise Mock for testing with Jest", | ||
@@ -5,0 +5,0 @@ "main": "dist/jest-mock-promise.js", |
@@ -13,4 +13,4 @@ # What's this? | ||
* [How to use it?](#how-to-use-it) | ||
* [Testing in the async way](#testing-in-the-async-way) | ||
* [Applying the synchronous Promise](#applying-the-synchronous-promise) | ||
* [First example - Traditional async test](#first-example---traditional-async-test) | ||
* [Second example - Applying the synchronous Promise](#second-example---applying-the-synchronous-promise ) | ||
* [API](#api) | ||
@@ -65,5 +65,5 @@ | ||
Synchronous Promise is usefull for testing an async component. | ||
Synchronous Promise wac created to simplify unit testing an async component. So to illustrate how it can be used, in the following example we are going to test a component, which multiplies two numbers provided as a payload of a promise. The result is returned a call to a callback function. | ||
We'll look at an example of testing a method, which multiplies two numbers provided as a promise payload, after which it passes the result by calling a callback function. Here's the code of the component we want to test: | ||
First let's have a look a the component we want to test: | ||
```javascript | ||
@@ -81,4 +81,6 @@ // ./src/component.js | ||
``` | ||
## Testing in the async way | ||
To test this method in a regular Jest test, we would need to write something along these lines: | ||
Now let's write a Jest tests. In our first example we'll create a test in a traditional async way ... just to show how unreadable it is. Then, in the second example, we'll simplify things by using `jest-mock-axios`. | ||
## First example - Traditional async test | ||
```javascript | ||
@@ -113,3 +115,3 @@ // ./src/__test__/component.spec.js | ||
## Applying the synchronous Promise | ||
## Second example - Applying the synchronous Promise | ||
The first thing we need to do is install this component: `npm i --save-dev jest-mock-promise` | ||
@@ -120,3 +122,3 @@ | ||
// ./__mocks__/es6-promise.js | ||
import JestMockPromise from './jest-mock-promise'; | ||
import JestMockPromise from 'jest-mock-promise'; | ||
@@ -151,2 +153,6 @@ // mocking the es6-promise, which is used by Axios | ||
## Third example - Mocking Axios | ||
As the final example we can have a look source code of [`jest-mock-axios`](https://github.com/knee-cola/jest-mock-axios), which is based on `jest-mock-promise`. | ||
# API | ||
@@ -153,0 +159,0 @@ |
33758
170