New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

jest-when

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-when - npm Package Compare versions

Comparing version 1.0.3 to 1.1.0

136

dist/when.js

@@ -6,8 +6,6 @@ 'use strict';

});
exports.WhenMock = undefined;
exports.when = exports.WhenMock = undefined;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
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; }; }();
var _jasmine_utils = require('expect/build/jasmine_utils');

@@ -17,91 +15,87 @@

var WhenMock = exports.WhenMock = function () {
function WhenMock(fn) {
var _this = this;
var WhenMock = exports.WhenMock = function WhenMock(fn) {
var _this = this;
_classCallCheck(this, WhenMock);
_classCallCheck(this, WhenMock);
this.fn = fn;
this.callMocks = [];
this.debug = false;
this.log = function () {
var _console;
this.fn = fn;
this.callMocks = [];
this.debug = false;
this.log = function () {
var _console;
return _this.debug && (_console = console).log.apply(_console, arguments);
};
}
return _this.debug && (_console = console).log.apply(_console, arguments);
};
_createClass(WhenMock, [{
key: 'calledWith',
value: function calledWith() {
var _this2 = this;
var mockReturnValue = function mockReturnValue(matchers, assertCall) {
return function (val) {
_this.callMocks.push({ matchers: matchers, val: val, assertCall: assertCall });
for (var _len = arguments.length, matchers = Array(_len), _key = 0; _key < _len; _key++) {
matchers[_key] = arguments[_key];
}
_this.fn.mockImplementation(function () {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
this.log('calledWith', matchers);
_this.log('mocked impl', args);
return {
mockReturnValue: function mockReturnValue(val) {
var assertCall = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var _loop = function _loop(i) {
var _callMocks$i = _this.callMocks[i],
matchers = _callMocks$i.matchers,
val = _callMocks$i.val,
assertCall = _callMocks$i.assertCall;
_this2.callMocks.push({ matchers: matchers, val: val, assertCall: assertCall });
var match = matchers.reduce(function (match, matcher, i) {
_this.log('matcher check, match: ' + match + ', index: ' + i);
_this2.fn.mockImplementation(function () {
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
// Propagate failure to the end
if (!match) {
return false;
}
_this2.log('mocked impl', args);
var arg = args[i];
var _loop = function _loop(i) {
var _callMocks$i = _this2.callMocks[i],
matchers = _callMocks$i.matchers,
val = _callMocks$i.val,
assertCall = _callMocks$i.assertCall;
_this.log(' matcher: ' + matcher);
_this.log(' arg: ' + arg);
var match = matchers.reduce(function (match, matcher, i) {
_this2.log('matcher check, match: ' + match + ', index: ' + i);
// Assert the match for better messaging during a failure
if (assertCall) {
expect(arg).toEqual(matcher);
}
// Propagate failure to the end
if (!match) {
return false;
}
return (0, _jasmine_utils.equals)(arg, matcher);
}, true);
var arg = args[i];
_this2.log(' matcher: ' + matcher);
_this2.log(' arg: ' + arg);
// Assert the match for better messaging during a failure
if (assertCall) {
expect(arg).toEqual(matcher);
}
return (0, _jasmine_utils.equals)(arg, matcher);
}, true);
if (match) {
return {
v: val
};
}
if (match) {
return {
v: val
};
}
};
for (var i = 0; i < _this2.callMocks.length; i++) {
var _ret = _loop(i);
for (var i = 0; i < _this.callMocks.length; i++) {
var _ret = _loop(i);
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
}
});
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
}
};
});
};
};
this.calledWith = function () {
for (var _len2 = arguments.length, matchers = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
matchers[_key2] = arguments[_key2];
}
}]);
return WhenMock;
}();
return { mockReturnValue: mockReturnValue(matchers, false) };
};
this.expectCalledWith = function () {
for (var _len3 = arguments.length, matchers = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
matchers[_key3] = arguments[_key3];
}
var when = function when(fn) {
return { mockReturnValue: mockReturnValue(matchers, true) };
};
};
var when = exports.when = function when(fn) {
if (fn.__whenMock__ instanceof WhenMock) return fn.__whenMock__;

@@ -111,3 +105,1 @@ fn.__whenMock__ = new WhenMock(fn);

};
exports.default = when;
{
"name": "jest-when",
"version": "1.0.3",
"version": "1.1.0",
"description": "A when(fn).calledWith(args).thenReturn(value) lib for jest",

@@ -5,0 +5,0 @@ "main": "dist/when.js",

@@ -10,3 +10,3 @@ # jest-when

```javascript
import when from 'jest-when';
import { when } from 'jest-when';

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

```javascript
import when from 'jest-when';
import { when } from 'jest-when';

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

```javascript
import when from 'jest-when';
import { when } from 'jest-when';

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

```javascript
import when from 'jest-when';
import { when } from 'jest-when';

@@ -66,12 +66,15 @@ const fn = jest.fn();

#### Assert args for debugging:
#### Assert the args:
Just pass `true` as second param to `.mockReturnValue(value, true)`. After that your test will fail if the jest mock function is ever called without those exact `calledWith` params.
Use `expectCalledWith` instead to run an assertion that the `fn` was called with the provided args. Your test will fail if the jest mock function is ever called without those exact `expectCalledWith` params.
Disclaimer: This won't really work very well with compound declarations, because one of them will always fail, and throw an assertion error.
```javascript
import when from 'jest-when';
import { when } from 'jest-when';
const fn = jest.fn();
when(fn).calledWith(1).mockReturnValue('x');
when(fn).expectCalledWith(1).mockReturnValue('x');
fn(2); // Will throw a helpful jest assertion error with diff
```
fn(2); // Will throw a helpful jest assertion error with args diff
```
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