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.0.6 to 1.0.7

305

dist/jest-mock-promise.js

@@ -1,1 +0,304 @@

!function(e){function t(r){if(n[r])return n[r].exports;var s=n[r]={i:r,l:!1,exports:{}};return e[r].call(s.exports,s,s.exports,t),s.l=!0,s.exports}var n={};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";function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var s,i=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}();!function(e){e[e.pending=0]="pending",e[e.resolved=1]="resolved",e[e.rejected=2]="rejected"}(s||(s={}));var a=function(){function e(t){r(this,e),this.handlers=[],this.handlerIx=0,this.state=s.pending,t&&t(this.resolveFn.bind(this),this.rejectFn.bind(this))}return i(e,[{key:"resolveFn",value:function(e){this.data=e,this.state=s.resolved,this.err=void 0;for(var t=this.handlers.length;this.handlerIx<t;this.handlerIx++){var n,r=this.handlers[this.handlerIx];if(r.catch)break;try{n=r.then(this.data)}catch(e){this.handlerIx++,this.rejectFn(e)}void 0!==n&&(this.data=n)}}},{key:"rejectFn",value:function(e){this.state=s.rejected,this.err=e;for(var t=this.handlers.length;this.handlerIx<t;this.handlerIx++){var n,r=this.handlers[this.handlerIx];if(r.catch){n=r.catch(e),this.handlerIx++,this.resolveFn(n);break}}}},{key:"then",value:function(e,t){switch(this.state){case s.rejected:t&&t(this.err);break;case s.resolved:e(this.data);break;default:this.handlers.push({then:e}),t&&this.handlers.push({catch:t})}return this}},{key:"catch",value:function(e){return this.state===s.resolved?e(this.err):this.handlers.push({catch:e}),this}},{key:"resolve",value:function(e){this.resolveFn(e)}},{key:"reject",value:function(e){this.rejectFn(e)}}],[{key:"resolve",value: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)})}},{key:"reject",value: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=a}]);
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["JestMockPromise"] = factory();
else
root["JestMockPromise"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* 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
*
*/
var PromiseState;
(function (PromiseState) {
PromiseState[PromiseState["pending"] = 0] = "pending";
PromiseState[PromiseState["resolved"] = 1] = "resolved";
PromiseState[PromiseState["rejected"] = 2] = "rejected";
})(PromiseState || (PromiseState = {}));
var JestMockPromise = function () {
function JestMockPromise(callbackFn) {
_classCallCheck(this, JestMockPromise);
this.handlers = [];
this.handlerIx = 0;
this.state = PromiseState.pending;
// if given, calling the given function
if (callbackFn) {
callbackFn(this.resolveFn.bind(this), this.rejectFn.bind(this));
}
}
/**
* Resolves the given promise
* @param data data which should be passed to `then` handler functions
*/
_createClass(JestMockPromise, [{
key: "resolveFn",
value: function resolveFn(data) {
this.data = data;
this.state = PromiseState.resolved;
this.err = void 0;
for (var maxIx = this.handlers.length; this.handlerIx < maxIx; this.handlerIx++) {
var el = this.handlers[this.handlerIx];
var returnedValue;
// stop the execution at first `catch` handler you run into
if (el.catch) {
break;
}
try {
// calling a `then` handler
returnedValue = el.then(this.data);
} catch (ex) {
// in case `then` handler throws an error
// > pass it down to a first `catch` handler
this.handlerIx++;
this.rejectFn(ex);
}
if (returnedValue !== void 0) {
// IF handler returned a value
// > use it as the `data` for all the handlers which will be called next
this.data = returnedValue;
}
}
;
}
}, {
key: "rejectFn",
/**
* Rejects the given promise
* @param err error object which is to be passed as a param to `catch` function
*/
value: function rejectFn(err) {
this.state = PromiseState.rejected;
this.err = err;
// find the first `catch` handler and call it
for (var maxIx = this.handlers.length; this.handlerIx < maxIx; this.handlerIx++) {
var el = this.handlers[this.handlerIx],
returnedValue;
if (el.catch) {
returnedValue = el.catch(err);
// try executing `then` handlers which follow
this.handlerIx++;
this.resolveFn(returnedValue);
// stop the execution as soon as you run into a first catch element
break;
}
}
;
}
/**
* Appends fulfillment and rejection handlers to the promise,
* and returns a new promise resolving to the return value of
* the called handler, or to its original settled value if the
* promise was not handled (i.e. if the relevant handler
* onFulfilled or onRejected is not a function).
* @param onFulfilled fulfillment handler function
* @param onRejected rejection handler function
*/
}, {
key: "then",
value: function then(onFulfilled, onRejected) {
// if the promise is already settled (resolved or rejected)
// > call the apropriate handler
switch (this.state) {
case PromiseState.rejected:
if (onRejected) {
onRejected(this.err);
}
break;
case PromiseState.resolved:
onFulfilled(this.data);
break;
default:
this.handlers.push({ then: onFulfilled });
if (onRejected) {
this.handlers.push({ catch: onRejected });
}
}
return this;
}
/**
* Appends a rejection handler callback to the promise,
* and returns a new promise resolving to the return
* value of the callback if it is called, or to its
* original fulfillment value if the promise is instead
* fulfilled.
* @param onRejected rejection handler function
*/
}, {
key: "catch",
value: function _catch(onRejected) {
// if the promise is already rejected
// > call the handler right away
if (this.state === PromiseState.resolved) {
onRejected(this.err);
} else {
this.handlers.push({ catch: onRejected });
}
return this;
}
/**
* Resolves the promise with the given promise data.
* This is a non-standard method, which should be the last
* one to be called, after all the fulfillment and rejection
* handlers have been registered.
* @param {*} data
*/
}, {
key: "resolve",
value: function resolve(data) {
this.resolveFn(data);
}
/**
* Rejects the promise with the given promise with the given error object.
* This is a non-standard method, which should be the last
* one to be called, after all the fulfillment and rejection
* handlers have been registered.
* @param {*} data
*/
}, {
key: "reject",
value: function reject(err) {
this.rejectFn(err);
}
/**
* Creates a resolved promise with the given data
* @param data data which should be passed to `then` handler functions
*/
}], [{
key: "resolve",
value: function resolve(data) {
console.warn('a promise created via `JestMockPromise.resolve` will be executed async ... for sync execution call `resolve` method on an instance of `Promise`');
return new JestMockPromise(function (resolve, reject) {
setTimeout(resolve(data), 0);
});
}
/**
* Creates a rejected promise with the given data
* @param err error object which is to be passed as a param to `catch` function
*/
}, {
key: "reject",
value: function reject(err) {
console.warn('a promise created via `JestMockPromise.reject` will be executed async ... for sync execution call `reject` method on an instance of `Promise`');
return new JestMockPromise(function (resolve, reject) {
setTimeout(reject(err), 0);
});
}
}]);
return JestMockPromise;
}();
exports.default = JestMockPromise;
/***/ })
/******/ ]);
});

2

package.json
{
"name": "jest-mock-promise",
"version": "1.0.6",
"version": "1.0.7",
"description": "Synchronous Promise Mock for testing with Jest",

@@ -5,0 +5,0 @@ "main": "./src/jest-mock-promise.ts",

@@ -13,4 +13,4 @@ const path = require('path');

filename: '[name].js',
// library: 'JestMockPromise',
// libraryTarget: 'umd'
library: 'JestMockPromise',
libraryTarget: 'umd'
},

@@ -17,0 +17,0 @@

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