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.10 to 1.1.11

dist/jest-mock-promise.js.LICENSE.txt

6

dist/jest-mock-promise-types.d.ts

@@ -6,5 +6,5 @@ declare enum PromiseState {

}
declare type AnyFunction = (...args: any[]) => any;
declare type HandlerType = {
then?: AnyFunction;
declare type AnyFunction<T = any, Y = any> = (...args: Y[]) => T;
declare type HandlerType<T> = {
then?: AnyFunction<T>;
catch?: AnyFunction;

@@ -11,0 +11,0 @@ finally?: AnyFunction;

@@ -13,3 +13,3 @@ /**

*
* let promise = ExternalComponent.doAyncWork();
* let promise = ExternalComponent.doAsyncWork();
* promise.resolve({ label: 'this is some mock data' });

@@ -22,3 +22,3 @@ *

import { AnyFunction } from './jest-mock-promise-types';
declare class JestMockPromise {
declare class JestMockPromise<T = any> {
private handlers;

@@ -53,3 +53,3 @@ private handlerIx;

*/
then(onFulfilled: AnyFunction, onRejected?: AnyFunction): JestMockPromise;
then(onFulfilled: AnyFunction<any, T>, onRejected?: AnyFunction): JestMockPromise<T>;
/**

@@ -76,3 +76,3 @@ * Appends a rejection handler callback to the promise,

*/
resolve(data?: any): void;
resolve(data?: T): void;
/**

@@ -90,3 +90,3 @@ * Rejects the promise with the given promise with the given error object.

*/
static resolve(data?: any): JestMockPromise;
static resolve<T = any>(data?: any): JestMockPromise<T>;
/**

@@ -96,4 +96,4 @@ * Creates a rejected promise with the given data

*/
static reject(err?: any): JestMockPromise;
static reject<T = any>(err?: any): JestMockPromise<T>;
}
export default JestMockPromise;

@@ -1,21 +0,3 @@

!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["jest-mock-promise"]=t():e["jest-mock-promise"]=t()}("undefined"!=typeof self?self:this,function(){return function(e){function t(r){if(n[r])return n[r].exports;var i=n[r]={i:r,l:!1,exports:{}};return e[r].call(i.exports,i,i.exports,t),i.l=!0,i.exports}var n={};return t.m=e,t.c=n,t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:r})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t,n){"use strict";/**
* Synchronous Promise, which gets settled (resolved or settled) in a synchronous manner.
*
* `JestMockPromise` was written to simplify unit testing mocking (i.e. in [Jest](https://facebook.github.io/jest/) )
*
* In order to simplify synchronious promise settling two additional methods
* were added to the promise instance:
* - `resolve` = forces the given promise to be resolved right away
* - `reject` = forces the given promise to be rejected right away
*
* By using these methods, we can write something like (provided that the Promise is mocked):
*
* let promise = ExternalComponent.doAyncWork();
* promise.resolve({ label: 'this is some mock data' });
*
* @author knee-cola<nikola.derezic@gmail.com>
* @license @license MIT License, http://www.opensource.org/licenses/MIT
*
*/
Object.defineProperty(t,"__esModule",{value:!0});var r=n(1),i=function(){function e(e){this.handlers=[],this.handlerIx=0,this.state=r.PromiseState.pending,e&&e(this.resolveFn.bind(this),this.rejectFn.bind(this))}return e.prototype.resolveFn=function(e){this.data=e,this.state=r.PromiseState.resolved,this.err=void 0;for(var t=this.handlers.length;this.handlerIx<t;this.handlerIx++){var n=this.handlers[this.handlerIx];if(n.catch||n.finally)return void this.callFinally();try{e=n.then(e)}catch(e){this.handlerIx++,this.rejectFn(e)}}},e.prototype.rejectFn=function(e){this.state=r.PromiseState.rejected,this.err=e;for(var t=this.handlers.length;this.handlerIx<t;this.handlerIx++){var n,i=this.handlers[this.handlerIx];if(i.catch)try{n=i.catch(e),this.handlerIx++,this.resolveFn(n);break}catch(e){this.handlerIx++,this.rejectFn(e);break}else i.finally&&this.callFinally()}},e.prototype.callFinally=function(){for(var e=!1,t=this.handlers.length;this.handlerIx<t;this.handlerIx++){var n=this.handlers[this.handlerIx];try{if(n.finally)n.finally(),e=!0;else{if(n.then&&e){this.resolveFn();break}if(n.catch){e=!1;continue}}}catch(e){this.handlerIx++,this.rejectFn(e);break}}},e.prototype.then=function(e,t){switch("function"!=typeof e&&(e=function(e){return e}),this.state){case r.PromiseState.rejected:t&&t(this.err);break;case r.PromiseState.resolved:e(this.data);break;default:this.handlers.push({then:e}),t&&this.handlers.push({catch:t})}return this},e.prototype.catch=function(e){return this.state===r.PromiseState.rejected?e(this.err):this.handlers.push({catch:e}),this},e.prototype.finally=function(e){return this.state!==r.PromiseState.pending?e():this.handlers.push({finally:e}),this},e.prototype.resolve=function(e){this.resolveFn(e)},e.prototype.reject=function(e){this.rejectFn(e)},e.resolve=function(t){return console.warn("a promise created via `JestMockPromise.resolve` will be executed async ... for sync execution call `resolve` method on an instance of `Promise`"),new e(function(e,n){setTimeout(e(t),0)})},e.reject=function(t){return console.warn("a promise created via `JestMockPromise.reject` will be executed async ... for sync execution call `reject` method on an instance of `Promise`"),new e(function(e,n){setTimeout(n(t),0)})},e}();t.default=i},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r;!function(e){e[e.pending=0]="pending",e[e.resolved=1]="resolved",e[e.rejected=2]="rejected"}(r||(r={})),t.PromiseState=r}])});
/*! For license information please see jest-mock-promise.js.LICENSE.txt */
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["jest-mock-promise"]=t():e["jest-mock-promise"]=t()}(self,(function(){return(()=>{"use strict";var e={803:(e,t)=>{var r;Object.defineProperty(t,"__esModule",{value:!0}),t.PromiseState=void 0,function(e){e[e.pending=0]="pending",e[e.resolved=1]="resolved",e[e.rejected=2]="rejected"}(r||(r={})),t.PromiseState=r}},t={};function r(n){var s=t[n];if(void 0!==s)return s.exports;var i=t[n]={exports:{}};return e[n](i,i.exports,r),i.exports}var n={};return(()=>{var e=n;Object.defineProperty(e,"__esModule",{value:!0});var t=r(803),s=function(){function e(e){this.handlers=[],this.handlerIx=0,this.state=t.PromiseState.pending,e&&e(this.resolveFn.bind(this),this.rejectFn.bind(this))}return e.prototype.resolveFn=function(e){this.data=e,this.state=t.PromiseState.resolved,this.err=void 0;for(var r=this.handlers.length;this.handlerIx<r;this.handlerIx++){var n=this.handlers[this.handlerIx];if(n.catch||n.finally)return void this.callFinally();try{e=n.then(e)}catch(e){this.handlerIx++,this.rejectFn(e)}}},e.prototype.rejectFn=function(e){this.state=t.PromiseState.rejected,this.err=e;for(var r=this.handlers.length;this.handlerIx<r;this.handlerIx++){var n,s=this.handlers[this.handlerIx];if(s.catch)try{n=s.catch(e),this.handlerIx++,this.resolveFn(n);break}catch(e){this.handlerIx++,this.rejectFn(e);break}else s.finally&&this.callFinally()}},e.prototype.callFinally=function(){for(var e=!1,t=this.handlers.length;this.handlerIx<t;this.handlerIx++){var r=this.handlers[this.handlerIx];try{if(r.finally)r.finally(),e=!0;else{if(r.then&&e){this.resolveFn();break}if(r.catch){e=!1;continue}}}catch(e){this.handlerIx++,this.rejectFn(e);break}}},e.prototype.then=function(e,r){switch("function"!=typeof e&&(e=function(e){return e}),this.state){case t.PromiseState.rejected:r&&r(this.err);break;case t.PromiseState.resolved:e(this.data);break;default:this.handlers.push({then:e}),r&&this.handlers.push({catch:r})}return this},e.prototype.catch=function(e){return this.state===t.PromiseState.rejected?e(this.err):this.handlers.push({catch:e}),this},e.prototype.finally=function(e){return this.state!==t.PromiseState.pending?e():this.handlers.push({finally:e}),this},e.prototype.resolve=function(e){this.resolveFn(e)},e.prototype.reject=function(e){this.rejectFn(e)},e.resolve=function(t){return console.warn("a promise created via `JestMockPromise.resolve` will be executed async ... for sync execution call `resolve` method on an instance of `Promise`"),new e((function(e,r){setTimeout(e(t),0)}))},e.reject=function(t){return console.warn("a promise created via `JestMockPromise.reject` will be executed async ... for sync execution call `reject` method on an instance of `Promise`"),new e((function(e,r){setTimeout(r(t),0)}))},e}();e.default=s})(),n})()}));
//# sourceMappingURL=jest-mock-promise.js.map
{
"name": "jest-mock-promise",
"version": "1.1.10",
"version": "1.1.11",
"description": "Synchronous Promise Mock for testing with Jest",

@@ -8,3 +8,3 @@ "main": "dist/jest-mock-promise.js",

"scripts": {
"build": "webpack -p",
"build": "webpack",
"test": "jest --watchAll"

@@ -15,8 +15,9 @@ },

"devDependencies": {
"@types/jest": "^24.0.20",
"jest": "^24.9.0",
"ts-jest": "^24.1.0",
"ts-loader": "^3.1.1",
"typescript": "^2.9.2",
"webpack": "^3.12.0"
"@types/jest": "^27.0.3",
"jest": "^27.4.5",
"ts-jest": "^27.1.2",
"ts-loader": "^9.2.6",
"typescript": "^4.5.4",
"webpack": "^5.65.0",
"webpack-cli": "^4.9.1"
},

@@ -23,0 +24,0 @@ "repository": {

@@ -32,10 +32,10 @@ # What's this?

```javascript
new Promise((resolve, reject) => { resolve(1,2); });
new Promise<string>((resolve, reject) => { resolve("some string value"); });
```
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();
let promise = new Promise<string>();
// resolving a promise
promise.resolve(1, 2);
promise.resolve("some string value");
```

@@ -80,3 +80,3 @@

// creating a new promis
// creating a new promise
let promise = new Promise((resolve, reject) => {

@@ -123,3 +123,3 @@ // assigning the resolve function a variable from outter scope

const onPromiseMultiply = (promise, callback) => {
promise.then((a,b) => {
promise.then(([a,b]) => {
callback(a*b);

@@ -139,3 +139,3 @@ })

The next snippet contains a test written in traditional async way:
```javascript
```typescript
// ./src/__test__/component.spec.js

@@ -149,7 +149,7 @@ import {onPromiseMultiply} from '../component.js';

let callbackFn = jest.fn();
let promise = new Promise((resolve, reject) {
let promise = new Promise<[number,number]>((resolve, reject) {
// providing two numbers which need to be multiplied
// as we know, although we have resolved the promise right away,
// `then` handlers will be called asnyc at later time
resolve(1,2);
resolve([1,2]);
});

@@ -161,3 +161,3 @@

// Altought promise is already resolved, `then` handlers will
// be called asnyc at later time. That's why we need to put
// be called async at later time. That's why we need to put
// our expectation inside a `then` handler

@@ -195,3 +195,3 @@ // + we need to return a promise to the Jest, so it knows

let callbackFn = jest.fn();
let promise = new Promise();
let promise = new Promise<[number,number]>();

@@ -202,3 +202,3 @@ // calling the function we want to test

// resolving our promise
promise.resolve(1,2);
promise.resolve([1,2]);

@@ -205,0 +205,0 @@ // testing to see if our function is working correctly

import JestMockPromise from "../lib/jest-mock-promise";
test('`finally` must be called with no arguments after if a promise is resolved', () => {
const promise = new JestMockPromise();
const promise = new JestMockPromise<[number, number]>();
const finallyHandler = jest.fn();
const thenHandler = jest.fn();
const thenHandler = jest.fn<void, [[number, number]]>();

@@ -13,6 +13,6 @@ promise.then(thenHandler);

promise.resolve('some data');
promise.resolve([1, 2]);
expect(thenHandler.mock.calls.length).toEqual(1);
expect(thenHandler.mock.calls).toEqual([["some data"]]);
expect(thenHandler.mock.calls).toEqual([[[1, 2]]]);

@@ -42,3 +42,3 @@ expect(finallyHandler.mock.calls.length).toEqual(1);

test('if promise is RESOLVED `then` directly following after `finally` should also be called', () => {
const promise = new JestMockPromise();
const promise = new JestMockPromise<string>();

@@ -126,3 +126,3 @@ const finallyHandler = jest.fn();

test('if an error is thrown inside `this` the closest `catch` should be called', () => {
const promise = new JestMockPromise();
const promise = new JestMockPromise<string>();

@@ -150,3 +150,3 @@ const thenHandler = jest.fn();

test('if an error is thrown inside `this` the closest `catch` should be called', () => {
const promise = new JestMockPromise();
const promise = new JestMockPromise<string>();

@@ -223,3 +223,3 @@ const catchHandler = jest.fn();

test('return value from one `then` should be passed to the next one in chain', () => {
const promise = new JestMockPromise();
const promise = new JestMockPromise<string>();

@@ -252,3 +252,3 @@ const thenHandler2 = jest.fn();

test('if promise is pre-resolved and `then` is called with a non-function as onFulfilled callback, onFulfilled should be replaced by identity function', () => {
const promise = new JestMockPromise();
const promise = new JestMockPromise<string>();

@@ -265,3 +265,3 @@ promise.resolve('mock data');

test('if `then` is called with a non-function as onFulfilled callback, onFulfilled should be replaced by identity function', () => {
const promise = new JestMockPromise();
const promise = new JestMockPromise<string>();

@@ -268,0 +268,0 @@ const thenHandler = jest.fn();

@@ -6,3 +6,3 @@ {

"declaration": true,
"declarationDir": "./"
"declarationDir": "./dist"
},

@@ -9,0 +9,0 @@ "exclude": [

const path = require('path');
const srcDir = './lib/';
const destDir = 'dist';
const srcDir = './lib';
const destDir = './dist';
module.exports = {
entry: {
"jest-mock-promise": srcDir+"jest-mock-promise.ts",
"jest-mock-promise": `${srcDir}/jest-mock-promise.ts`,
},
output: {
path: path.resolve(__dirname + '/' + destDir),
path: path.resolve(`${__dirname}/${destDir}`),
filename: '[name].js',

@@ -13,0 +13,0 @@ library: 'jest-mock-promise',

Sorry, the diff of this file is not supported yet

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