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.12 to 2.0.0

50

dist/jest-mock-promise.d.ts

@@ -20,25 +20,33 @@ /**

*/
import { AnyFunction } from './jest-mock-promise-types';
import { PromiseState, AnyFunction } from './jest-mock-promise-types';
declare type QueueItem<T> = {
/** handler registered with `this` */
onFulfilled?: AnyFunction<any, T>;
/** handler registered with `catch` or `this` */
onRejected?: AnyFunction<any, T>;
/** handler registered with `finally` */
onFinally?: AnyFunction<any, T>;
/** chained promise */
nextPromise: JestMockPromise;
/** value returned by `onFulfilled` */
nextValue?: T;
/** value set by `reject` method */
err?: any;
};
declare type Queue<T> = Array<QueueItem<T>>;
declare class JestMockPromise<T = any> {
private handlers;
private handlerIx;
private data;
protected queue: Queue<T>;
/** value with wich the promise was resolved */
private value;
/** error with wich the promise was rejected */
private err;
private state;
/** current state of the promise */
protected state: PromiseState;
constructor(callbackFn?: (x?: any, y?: any) => any);
/**
* Resolves the given promise
* @param value data which should be passed to `then` handler functions
* Resolves promises at a given level
* @param currLevel list of promises which need to be resolved at this level
*/
private resolveFn;
private static processLevel;
/**
* Rejects the given promise
* @param err error object which is to be passed as a param to `catch` function
*/
private rejectFn;
/**
* Calls `finally` handlers
*/
private callFinally;
/**
* Appends fulfillment and rejection handlers to the promise,

@@ -61,3 +69,3 @@ * and returns a new promise resolving to the return value of

*/
catch(onRejected: AnyFunction): this;
catch(onRejected: AnyFunction): JestMockPromise<any>;
/**

@@ -67,3 +75,3 @@ * Appends a finally handler callback to the promise

*/
finally(onFinally: AnyFunction): this;
finally(onFinally: AnyFunction): JestMockPromise<any>;
/**

@@ -74,5 +82,5 @@ * Resolves the promise with the given promise data.

* handlers have been registered.
* @param {*} data
* @param {*} value
*/
resolve(data?: T): void;
resolve(value?: T): void;
/**

@@ -79,0 +87,0 @@ * Rejects the promise with the given promise with the given error object.

/*! 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;var r=e;this.state=t.PromiseState.resolved,this.err=void 0;for(var n=this.handlers.length;this.handlerIx<n;this.handlerIx++){var s=this.handlers[this.handlerIx];if(s.catch||s.finally)return void this.callFinally();try{r=s.then(r)}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(r,n){switch("function"!=typeof r&&(r=function(e){return e}),this.state){case t.PromiseState.rejected:n&&n(this.err);break;case t.PromiseState.resolved:var s=new e;return s.resolve(r(this.data)),s;default:this.handlers.push({then:r}),n&&this.handlers.push({catch:n})}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})()}));
!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 o=t[n];if(void 0!==o)return o.exports;var s=t[n]={exports:{}};return e[n](s,s.exports,r),s.exports}var n={};return(()=>{var e=n;Object.defineProperty(e,"__esModule",{value:!0});var t=r(803),o=function(){function e(e){this.queue=[],this.state=t.PromiseState.pending,e&&e(this.resolve.bind(this),this.reject.bind(this))}return e.processLevel=function(r){if(0!==r.length){var n=[];r.forEach((function(e){var r=e.instance,o=e.value,s=e.err,i=e.newState,c=r.queue;r.state=i,r.value=o,r.err=s,c.forEach((function(e,r){var c=e.nextPromise,a=e.onFulfilled,u=e.onRejected,f=e.onFinally,l={instance:c,value:o,err:s,newState:i};switch(i){case t.PromiseState.resolved:if(a)try{l.value=a(o)}catch(e){l={instance:c,err:e,newState:t.PromiseState.rejected}}break;case t.PromiseState.rejected:if(u)try{u(s),l.newState=t.PromiseState.resolved}catch(e){l.err=e}else setTimeout((function(){console.error("Uncaught (in promise) Error: ".concat(s))}))}if(f)try{f()}catch(e){l={instance:c,err:e,newState:t.PromiseState.rejected}}n.push(l)}))})),e.processLevel(n)}},e.prototype.then=function(r,n){var o;switch("function"!=typeof r&&(r=function(e){return e}),this.state){case t.PromiseState.rejected:n&&n(this.err),(o=new e).reject(this.err);break;case t.PromiseState.resolved:(o=new e).resolve(r(this.value));break;default:o=new e}return this.queue.push({onFulfilled:r,onRejected:n,nextPromise:o}),o},e.prototype.catch=function(r){var n;return this.state===t.PromiseState.rejected?(r(this.err),(n=new e).resolve()):n=new e,this.queue.push({onRejected:r,nextPromise:n}),n},e.prototype.finally=function(r){var n;return this.state!==t.PromiseState.pending?(r(),(n=new e).resolve(this.value)):n=new e,this.queue.push({onFinally:r,nextPromise:n}),n},e.prototype.resolve=function(r){e.processLevel([{instance:this,value:r,newState:t.PromiseState.resolved}])},e.prototype.reject=function(r){e.processLevel([{instance:this,err:r,newState:t.PromiseState.rejected}])},e.resolve=function(t){return new e((function(e,r){setTimeout((function(){return e(t)}),100)}))},e.reject=function(t){return new e((function(e,r){setTimeout((function(){return r(t)}),0)}))},e}();e.default=o})(),n})()}));
//# sourceMappingURL=jest-mock-promise.js.map
{
"name": "jest-mock-promise",
"version": "1.1.12",
"version": "2.0.0",
"description": "Synchronous Promise Mock for testing with Jest",

@@ -5,0 +5,0 @@ "main": "dist/jest-mock-promise.js",

import JestMockPromise from "../lib/jest-mock-promise";
test('it should support pre-resolved promise', async () => {
const handler = jest.fn()
const promise = JestMockPromise
.resolve("abcd")
.then(handler);
await promise;
expect(handler.mock.calls.length).toEqual(1);
expect(handler).toHaveBeenCalledWith("abcd");
});
test('it should support pre-rejected promise', async () => {
const handler = jest.fn()
const promise = JestMockPromise
.reject("some error")
.catch(handler);
await promise;
expect(handler.mock.calls.length).toEqual(1);
expect(handler).toHaveBeenCalledWith("some error");
});
test('`finally` must be called with no arguments after if a promise is resolved', () => {
const promise = new JestMockPromise<[number, number]>();

@@ -9,6 +34,9 @@ const finallyHandler = jest.fn();

promise.then(thenHandler);
promise.catch(() => {});
promise.finally(finallyHandler);
const promise = new JestMockPromise<[number, number]>();
promise
.then(thenHandler)
.catch(() => {})
.finally(finallyHandler);
promise.resolve([1, 2]);

@@ -29,5 +57,6 @@

promise.then(() => {});
promise.catch(catchHandler);
promise.finally(finallyHandler);
promise.then(() => {})
.catch(catchHandler)
.finally(finallyHandler)
promise.reject('some error data');

@@ -42,3 +71,3 @@

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

@@ -49,4 +78,5 @@

promise.finally(finallyHandler);
promise.then(thenHandler);
promise
.finally(finallyHandler)
.then(thenHandler);

@@ -59,6 +89,6 @@ promise.resolve('some data');

expect(thenHandler.mock.calls.length).toEqual(1);
expect(thenHandler.mock.calls).toEqual([[void 0]]);
expect(thenHandler.mock.calls).toEqual([['some data']]);
});
test('if promise is REJECTED `then` directly following after `finally` should also be called', () => {
test('if promise is REJECTED then "then" directly following after finally should NOT be called', () => {
const promise = new JestMockPromise();

@@ -69,4 +99,5 @@

promise.finally(finallyHandler);
promise.then(thenHandler);
promise
.finally(finallyHandler)
.then(thenHandler);

@@ -78,4 +109,3 @@ promise.reject('error data');

expect(thenHandler.mock.calls.length).toEqual(1);
expect(thenHandler.mock.calls).toEqual([[void 0]]);
expect(thenHandler.mock.calls.length).toEqual(0);
});

@@ -87,13 +117,15 @@

const finallyHandler = jest.fn();
const catchHandler = jest.fn();
const catchHandler1 = jest.fn();
const catchHandler2 = jest.fn();
const mockError = new Error('mock error');
promise.finally(() => {
finallyHandler();
throw mockError;
});
finallyHandler();
throw mockError;
})
.then(() => {})
.catch(catchHandler1)
.catch(catchHandler2);
promise.then(() => {});
promise.catch(catchHandler);
promise.resolve('some data');

@@ -104,8 +136,9 @@

expect(catchHandler.mock.calls.length).toEqual(1);
expect(catchHandler.mock.calls).toEqual([[mockError]]);
expect(catchHandler1.mock.calls.length).toEqual(1);
expect(catchHandler1.mock.calls).toEqual([[mockError]]);
expect(catchHandler2).not.toBeCalled();
});
test('if an error is thrown inside `finally` all the `finally` which follow should also be called', () => {
const promise = new JestMockPromise();

@@ -115,9 +148,10 @@ const finallyHandler1= jest.fn();

const promise = new JestMockPromise();
promise.finally(() => {
finallyHandler1();
throw new Error('mock error');
});
finallyHandler1();
throw new Error('mock error');
})
.finally(finallyHandler2)
promise.finally(finallyHandler2);
promise.resolve('some data');

@@ -141,9 +175,8 @@

promise.finally(() => {
thenHandler();
throw mockError;
});
thenHandler();
throw mockError;
})
.then(() => {})
.catch(catchHandler);
promise.then(() => {});
promise.catch(catchHandler);
promise.resolve('some data');

@@ -157,2 +190,3 @@

});
test('if an error is thrown inside `this` the closest `catch` should be called', () => {

@@ -165,8 +199,7 @@ const promise = new JestMockPromise<string>();

promise.then(() => {
throw mockError;
});
throw mockError;
})
.then(() => {})
.catch(catchHandler);
promise.then(() => {});
promise.catch(catchHandler);
promise.resolve('some data');

@@ -185,8 +218,7 @@

promise.catch(() => {
throw mockError;
});
throw mockError;
})
.then(() => {})
.catch(catchHandler2);
promise.then(() => {});
promise.catch(catchHandler2);
promise.reject('error data');

@@ -198,3 +230,3 @@

test('if promise is pre-resolved `then` and `finally` must be called as soon as they are registered', () => {
test('if promise is pre-resolved then and finally must be called as soon as they are registered', () => {
const promise = new JestMockPromise();

@@ -207,7 +239,9 @@

promise.finally(finallyHandler);
promise
.finally(finallyHandler)
.then(thenHandler);
expect(finallyHandler.mock.calls.length).toEqual(1);
expect(finallyHandler.mock.calls).toEqual([[]]);
promise.then(thenHandler);
expect(thenHandler.mock.calls.length).toEqual(1);

@@ -239,4 +273,5 @@ expect(thenHandler.mock.calls).toEqual([['mock data']]);

promise.then(() => 'returned by first handler');
promise.then(thenHandler2);
promise
.then(() => 'returned by first handler')
.then(thenHandler2);

@@ -249,3 +284,3 @@ promise.resolve('initial data');

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

@@ -255,4 +290,5 @@

promise.catch(() => 'returned by catch handler');
promise.then(thenHandler2);
promise
.catch(() => 'returned by catch handler')
.then(thenHandler2);

@@ -262,3 +298,3 @@ promise.reject('error data');

expect(thenHandler2.mock.calls.length).toEqual(1);
expect(thenHandler2.mock.calls).toEqual([['returned by catch handler']]);
expect(thenHandler2.mock.calls).toEqual([[undefined]]);
});

@@ -337,2 +373,41 @@

expect(thirdHandler.mock.calls).toEqual([['2nd return value']]);
});
});
test('if multiple handlers have been attached to same promise, all should be resolved', () => {
const promise = new JestMockPromise<string>();
const handlerA1 = jest.fn<any,[string]>().mockReturnValue("A1 return value");
const handlerA2 = jest.fn<any,[string]>().mockReturnValue("A2 return value");
const handlerB1 = jest.fn().mockReturnValue("B1 return value");
const handlerB2 = jest.fn().mockReturnValue("B2 return value");
const handlerC = jest.fn();
promise.then(handlerA1)
.then(handlerA2);
promise
.then(handlerB1)
.then(handlerB2);
promise.then(handlerC);
promise.resolve('original data');
expect(handlerA1.mock.calls.length).toEqual(1);
expect(handlerA1.mock.calls).toEqual([['original data']]);
expect(handlerA2.mock.calls.length).toEqual(1);
expect(handlerA2.mock.calls).toEqual([['A1 return value']]);
expect(handlerB1.mock.calls.length).toEqual(1);
expect(handlerB1.mock.calls).toEqual([['original data']]);
expect(handlerB2.mock.calls.length).toEqual(1);
expect(handlerB2.mock.calls).toEqual([['B1 return value']]);
expect(handlerC.mock.calls.length).toEqual(1);
expect(handlerC.mock.calls).toEqual([['original data']]);
});

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