Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jest-mock-promise

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-mock-promise - npm Package Compare versions

Comparing version 1.1.9 to 1.1.10

2

package.json
{
"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.

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc