spy4js
Advanced tools
Comparing version 2.1.1 to 2.1.3
@@ -10,19 +10,15 @@ "use strict"; | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var uninitialized = function uninitialized(method) { | ||
return function () { | ||
throw new Error("Method '".concat(method, "' was not initialized on Mock.")); | ||
}; | ||
const uninitialized = method => () => { | ||
throw new Error(`Method '${method}' was not initialized on Mock.`); | ||
}; | ||
var defaultScope = Symbol('__Spy_global__'); | ||
const defaultScope = Symbol('__Spy_global__'); | ||
exports.defaultScope = defaultScope; | ||
var _mocks = _defineProperty({}, defaultScope, []); | ||
const _mocks = { | ||
[defaultScope]: [] | ||
}; | ||
exports._mocks = _mocks; | ||
var scope = defaultScope; | ||
let scope = defaultScope; | ||
var setScope = function setScope(scoping) { | ||
const setScope = scoping => { | ||
if (scoping) { | ||
@@ -36,9 +32,7 @@ _mocks[scoping] = []; | ||
var registerMock = function registerMock(mocked) { | ||
var mock = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
const registerMock = (mocked, mock = {}) => { | ||
_mocks[scope].push({ | ||
mocked: mocked, | ||
mock: mock, | ||
scope: scope | ||
mocked, | ||
mock, | ||
scope | ||
}); | ||
@@ -49,5 +43,5 @@ | ||
var createMock = function createMock(obj, methods) { | ||
var mock = registerMock(obj); | ||
(0, _utils.forEach)(methods, function (_, method) { | ||
const createMock = (obj, methods) => { | ||
const mock = registerMock(obj); | ||
(0, _utils.forEach)(methods, (_, method) => { | ||
mock[method] = uninitialized(method); | ||
@@ -60,15 +54,14 @@ }); | ||
var couldNotInitError = function couldNotInitError(scope, additional) { | ||
return new Error("Could not initialize mock for ".concat(scope === defaultScope ? 'global scope' : "scope \"".concat(scope, "\""), ", because:\n").concat(additional)); | ||
}; | ||
const couldNotInitError = (scope, additional) => new Error(`Could not initialize mock for ${scope === defaultScope ? 'global scope' : `scope "${scope}"`}, because:\n${additional}`); | ||
var initMock = function initMock(_ref, spyOn) { | ||
var mocked = _ref.mocked, | ||
mock = _ref.mock, | ||
scope = _ref.scope; | ||
(0, _utils.forEach)(mock, function (method) { | ||
const initMock = ({ | ||
mocked, | ||
mock, | ||
scope | ||
}, spyOn) => { | ||
(0, _utils.forEach)(mock, method => { | ||
try { | ||
mock[method] = spyOn(mocked, method); | ||
} catch (e) { | ||
throw new couldNotInitError(scope, e.message); | ||
throw couldNotInitError(scope, e.message); | ||
} | ||
@@ -78,9 +71,7 @@ }); | ||
var initMockScope = function initMockScope(scoping, spyOn) { | ||
(0, _utils.forEach)(_mocks[scoping], function (_, mock) { | ||
return initMock(mock, spyOn); | ||
}); | ||
const initMockScope = (scoping, spyOn) => { | ||
(0, _utils.forEach)(_mocks[scoping], (_, mock) => initMock(mock, spyOn)); | ||
}; | ||
var initMocks = function initMocks(spyOn, scoping) { | ||
const initMocks = (spyOn, scoping) => { | ||
initMockScope(defaultScope, spyOn); | ||
@@ -87,0 +78,0 @@ scoping && initMockScope(scoping, spyOn); |
@@ -10,6 +10,8 @@ "use strict"; | ||
var restoreAttributeForEntry = function restoreAttributeForEntry(value) { | ||
var obj = value.obj, | ||
method = value.method, | ||
methodName = value.methodName; | ||
const restoreAttributeForEntry = value => { | ||
const { | ||
obj, | ||
method, | ||
methodName | ||
} = value; | ||
@@ -32,3 +34,3 @@ if (obj) { | ||
SpyRegistry.prototype.restoreAll = function () { | ||
(0, _utils.forEach)(this.register, function (ignored, entry) { | ||
(0, _utils.forEach)(this.register, (ignored, entry) => { | ||
restoreAttributeForEntry(entry); | ||
@@ -40,3 +42,3 @@ }); | ||
SpyRegistry.prototype.restore = function (index) { | ||
var entry = this.register[index]; | ||
const entry = this.register[index]; | ||
@@ -52,5 +54,5 @@ if (entry) { | ||
this.register[this.registerCount] = { | ||
obj: obj, | ||
obj, | ||
method: obj[methodName], | ||
methodName: methodName | ||
methodName | ||
}; | ||
@@ -61,3 +63,3 @@ return this.registerCount; | ||
SpyRegistry.prototype.getOriginalMethod = function (index) { | ||
var entry = this.register[index]; | ||
const entry = this.register[index]; | ||
@@ -70,5 +72,5 @@ if (entry) { | ||
SpyRegistry.prototype.persist = function (index, intoPersReg) { | ||
var fromReg = intoPersReg ? this.register : this.persReg; | ||
var toReg = intoPersReg ? this.persReg : this.register; | ||
var entry = fromReg[index]; | ||
const fromReg = intoPersReg ? this.register : this.persReg; | ||
const toReg = intoPersReg ? this.persReg : this.register; | ||
const entry = fromReg[index]; | ||
@@ -75,0 +77,0 @@ if (entry) { |
@@ -12,6 +12,4 @@ "use strict"; | ||
var serialize = _serializeAsCode.Serializer.create(function (o) { | ||
return o === _utils.IGNORE && '>IGNORED<' || undefined; | ||
}); | ||
const serialize = _serializeAsCode.Serializer.create(o => o === _utils.IGNORE && '>IGNORED<' || undefined); | ||
exports.serialize = serialize; |
329
dist/spy.js
@@ -18,21 +18,7 @@ "use strict"; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } | ||
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); } | ||
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); } | ||
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } } | ||
var registry = new _registry.SpyRegistry(); | ||
var __LOCK__ = true; | ||
var Symbols = { | ||
const registry = new _registry.SpyRegistry(); | ||
let __LOCK__ = true; | ||
const Symbols = { | ||
name: Symbol('__Spy_name__'), | ||
@@ -45,7 +31,7 @@ isSpy: Symbol('__Spy_isSpy__'), | ||
}; | ||
var DefaultSettings = { | ||
const DefaultSettings = { | ||
useOwnEquals: true | ||
}; | ||
var SpyFunctions = { | ||
configure: function configure(config) { | ||
const SpyFunctions = { | ||
configure(config) { | ||
if (config.useOwnEquals !== undefined) { | ||
@@ -57,3 +43,3 @@ this[Symbols.config].useOwnEquals = config.useOwnEquals; | ||
if (!this[Symbols.index]) { | ||
throw new Error("\n\n".concat(this[Symbols.name], " can not") + ' be configured to be persistent!' + ' It does not mock any object.'); | ||
throw new Error(`\n\n${this[Symbols.name]} can not` + ' be configured to be persistent!' + ' It does not mock any object.'); | ||
} | ||
@@ -67,9 +53,6 @@ | ||
}, | ||
calls: function calls() { | ||
for (var _len = arguments.length, funcs = new Array(_len), _key = 0; _key < _len; _key++) { | ||
funcs[_key] = arguments[_key]; | ||
} | ||
calls(...funcs) { | ||
if (funcs.length === 0) { | ||
this[Symbols.func] = function () {}; | ||
this[Symbols.func] = () => {}; | ||
@@ -79,8 +62,8 @@ return this; | ||
var max = funcs.length - 1; | ||
var counter = -1; | ||
const max = funcs.length - 1; | ||
let counter = -1; | ||
this[Symbols.func] = function () { | ||
this[Symbols.func] = (...args) => { | ||
counter++; | ||
return funcs[max < counter ? max : counter].apply(funcs, arguments); | ||
return funcs[max < counter ? max : counter](...args); | ||
}; | ||
@@ -90,40 +73,18 @@ | ||
}, | ||
returns: function returns() { | ||
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
return this.calls.apply(this, _toConsumableArray(args.map(function (arg) { | ||
return function () { | ||
return arg; | ||
}; | ||
}))); | ||
returns(...args) { | ||
return this.calls(...args.map(arg => () => arg)); | ||
}, | ||
resolves: function resolves() { | ||
for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { | ||
args[_key3] = arguments[_key3]; | ||
} | ||
return this.returns.apply(this, _toConsumableArray((args.length ? args : [undefined]).map(function (arg) { | ||
return Promise.resolve(arg); | ||
}))); | ||
resolves(...args) { | ||
return this.returns(...(args.length ? args : [undefined]).map(arg => Promise.resolve(arg))); | ||
}, | ||
rejects: function rejects() { | ||
var _this = this; | ||
for (var _len4 = arguments.length, msgOrErrors = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) { | ||
msgOrErrors[_key4] = arguments[_key4]; | ||
} | ||
return this.calls.apply(this, _toConsumableArray((msgOrErrors.length ? msgOrErrors : [undefined]).map(function (msgOrError) { | ||
return function () { | ||
return Promise.reject((0, _utils.toError)(msgOrError, _this[Symbols.name])); | ||
}; | ||
}))); | ||
rejects(...msgOrErrors) { | ||
return this.calls(...(msgOrErrors.length ? msgOrErrors : [undefined]).map(msgOrError => () => Promise.reject((0, _utils.toError)(msgOrError, this[Symbols.name])))); | ||
}, | ||
throws: function throws(msgOrError) { | ||
var _this2 = this; | ||
this[Symbols.func] = function () { | ||
throw (0, _utils.toError)(msgOrError, _this2[Symbols.name]); | ||
throws(msgOrError) { | ||
this[Symbols.func] = () => { | ||
throw (0, _utils.toError)(msgOrError, this[Symbols.name]); | ||
}; | ||
@@ -133,9 +94,11 @@ | ||
}, | ||
reset: function reset() { | ||
reset() { | ||
this[Symbols.calls] = []; | ||
return this; | ||
}, | ||
restore: function restore() { | ||
restore() { | ||
if (this[Symbols.config].persistent) { | ||
throw new Error("\n\n".concat(this[Symbols.name], " can not be restored!") + ' It was configured to be persistent.'); | ||
throw new Error(`\n\n${this[Symbols.name]} can not be restored!` + ' It was configured to be persistent.'); | ||
} | ||
@@ -146,16 +109,16 @@ | ||
}, | ||
transparent: function transparent() { | ||
transparent() { | ||
return this.transparentAfter(0); | ||
}, | ||
transparentAfter: function transparentAfter(callCount) { | ||
var _this3 = this; | ||
var oldFunc = this[Symbols.func]; | ||
transparentAfter(callCount) { | ||
const oldFunc = this[Symbols.func]; | ||
this[Symbols.func] = function () { | ||
if (_this3[Symbols.calls].length > callCount) { | ||
var originalMethod = registry.getOriginalMethod(_this3[Symbols.index]); | ||
this[Symbols.func] = (...args) => { | ||
if (this[Symbols.calls].length > callCount) { | ||
const originalMethod = registry.getOriginalMethod(this[Symbols.index]); | ||
if (originalMethod) { | ||
return originalMethod.apply(void 0, arguments); | ||
return originalMethod(...args); | ||
} | ||
@@ -166,3 +129,3 @@ | ||
return oldFunc.apply(void 0, arguments); | ||
return oldFunc(...args); | ||
}; | ||
@@ -172,64 +135,53 @@ | ||
}, | ||
wasCalled: function wasCalled(callCount) { | ||
var madeCalls = this[Symbols.calls].length; | ||
wasCalled(callCount) { | ||
const madeCalls = this[Symbols.calls].length; | ||
if (typeof callCount === 'number') { | ||
if (madeCalls !== callCount) { | ||
throw new Error("\n\n".concat(this[Symbols.name], " was called ").concat(madeCalls, " times,") + " but there were expected ".concat(callCount, " calls.\n\n") + 'Actually there were:\n\n' + this.showCallArguments()); | ||
throw new Error(`\n\n${this[Symbols.name]} was called ${madeCalls} times,` + ` but there were expected ${callCount} calls.\n\n` + 'Actually there were:\n\n' + this.showCallArguments()); | ||
} | ||
} else if (madeCalls === 0) { | ||
throw new Error("\n\n".concat(this[Symbols.name], " was never called!\n\n")); | ||
throw new Error(`\n\n${this[Symbols.name]} was never called!\n\n`); | ||
} | ||
}, | ||
hasCallHistory: function hasCallHistory() { | ||
var _this4 = this; | ||
var madeCalls = this[Symbols.calls]; | ||
hasCallHistory(...callHistory) { | ||
const madeCalls = this[Symbols.calls]; | ||
const callCount = callHistory.length; | ||
for (var _len5 = arguments.length, callHistory = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) { | ||
callHistory[_key5] = arguments[_key5]; | ||
} | ||
var callCount = callHistory.length; | ||
if (madeCalls.length !== callCount) { | ||
throw new Error("\n\n".concat(this[Symbols.name], " was called ").concat(madeCalls.length, " times,") + " but the expected call history includes exactly ".concat(callHistory.length, " calls.\n\n") + 'Actually there were:\n\n' + this.showCallArguments()); | ||
throw new Error(`\n\n${this[Symbols.name]} was called ${madeCalls.length} times,` + ` but the expected call history includes exactly ${callHistory.length} calls.\n\n` + 'Actually there were:\n\n' + this.showCallArguments()); | ||
} | ||
var modifiedCallHistory = callHistory.map(function (arg) { | ||
return Array.isArray(arg) ? arg : [arg]; | ||
}); | ||
var hasErrors = false; | ||
var diffInfo = madeCalls.map(function (call, index) { | ||
var diff = (0, _utils.differenceOf)(call.args, modifiedCallHistory[index], _this4[Symbols.config]); | ||
const modifiedCallHistory = callHistory.map(arg => Array.isArray(arg) ? arg : [arg]); | ||
let hasErrors = false; | ||
const diffInfo = madeCalls.map((call, index) => { | ||
const diff = (0, _utils.differenceOf)(call.args, modifiedCallHistory[index], this[Symbols.config]); | ||
if (diff) hasErrors = true; | ||
return diff; | ||
}); | ||
if (hasErrors) throw new Error("\n\n".concat(this[Symbols.name], " was considered") + ' to be called with the following arguments in the given order:\n\n' + "".concat(modifiedCallHistory.map(function (entry, index) { | ||
return "call ".concat(index, ": ").concat((0, _serializer.serialize)(entry)); | ||
}).join('\n'), "\n\n") + 'Actually there were:\n\n' + this.showCallArguments(diffInfo)); | ||
if (hasErrors) throw new Error(`\n\n${this[Symbols.name]} was considered` + ' to be called with the following arguments in the given order:\n\n' + `${modifiedCallHistory.map((entry, index) => `call ${index}: ${(0, _serializer.serialize)(entry)}`).join('\n')}\n\n` + 'Actually there were:\n\n' + this.showCallArguments(diffInfo)); | ||
}, | ||
wasNotCalled: function wasNotCalled() { | ||
var madeCalls = this[Symbols.calls]; | ||
wasNotCalled() { | ||
const madeCalls = this[Symbols.calls]; | ||
if (madeCalls.length !== 0) { | ||
throw new Error("\n\n".concat(this[Symbols.name], " was not") + ' considered to be called.\n\n' + 'Actually there were:\n\n' + this.showCallArguments()); | ||
throw new Error(`\n\n${this[Symbols.name]} was not` + ' considered to be called.\n\n' + 'Actually there were:\n\n' + this.showCallArguments()); | ||
} | ||
}, | ||
wasCalledWith: function wasCalledWith() { | ||
var madeCalls = this[Symbols.calls]; | ||
wasCalledWith(...args) { | ||
const madeCalls = this[Symbols.calls]; | ||
if (madeCalls.length === 0) { | ||
throw new Error("\n\n".concat(this[Symbols.name], " was never called!\n\n")); | ||
throw new Error(`\n\n${this[Symbols.name]} was never called!\n\n`); | ||
} | ||
var diffInfo = []; | ||
const diffInfo = []; | ||
for (var _len6 = arguments.length, args = new Array(_len6), _key6 = 0; _key6 < _len6; _key6++) { | ||
args[_key6] = arguments[_key6]; | ||
} | ||
for (let i = 0; i < madeCalls.length; i++) { | ||
const diff = (0, _utils.differenceOf)(madeCalls[i].args, args, this[Symbols.config]); | ||
for (var i = 0; i < madeCalls.length; i++) { | ||
var diff = (0, _utils.differenceOf)(madeCalls[i].args, args, this[Symbols.config]); | ||
if (!diff) { | ||
@@ -242,13 +194,10 @@ return; | ||
throw new Error("\n\n".concat(this[Symbols.name], " was considered") + ' to be called with the following arguments:\n\n' + " --> ".concat((0, _serializer.serialize)(args), "\n\n") + 'Actually there were:\n\n' + this.showCallArguments(diffInfo)); | ||
throw new Error(`\n\n${this[Symbols.name]} was considered` + ' to be called with the following arguments:\n\n' + ` --> ${(0, _serializer.serialize)(args)}\n\n` + 'Actually there were:\n\n' + this.showCallArguments(diffInfo)); | ||
}, | ||
wasNotCalledWith: function wasNotCalledWith() { | ||
var errorOccurred = false; | ||
for (var _len7 = arguments.length, args = new Array(_len7), _key7 = 0; _key7 < _len7; _key7++) { | ||
args[_key7] = arguments[_key7]; | ||
} | ||
wasNotCalledWith(...args) { | ||
let errorOccurred = false; | ||
try { | ||
this.wasCalledWith.apply(this, args); | ||
this.wasCalledWith(...args); | ||
} catch (e) { | ||
@@ -259,11 +208,11 @@ errorOccurred = true; | ||
if (!errorOccurred) { | ||
throw new Error("\n\n".concat(this[Symbols.name], " was called") + ' unexpectedly with the following arguments:\n\n' + " --> ".concat((0, _serializer.serialize)(args), "\n\n")); | ||
throw new Error(`\n\n${this[Symbols.name]} was called` + ' unexpectedly with the following arguments:\n\n' + ` --> ${(0, _serializer.serialize)(args)}\n\n`); | ||
} | ||
}, | ||
getCallArguments: function getCallArguments() { | ||
var callNr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; | ||
var madeCalls = this[Symbols.calls]; | ||
getCallArguments(callNr = 0) { | ||
const madeCalls = this[Symbols.calls]; | ||
if (callNr % 1 !== 0 || callNr >= madeCalls.length) { | ||
throw new Error("\n\nThe provided callNr \"".concat(callNr, "\" was not valid.\n\n") + "Made calls for ".concat(this[Symbols.name], ":\n\n") + this.showCallArguments()); | ||
throw new Error(`\n\nThe provided callNr "${callNr}" was not valid.\n\n` + `Made calls for ${this[Symbols.name]}:\n\n` + this.showCallArguments()); | ||
} | ||
@@ -273,27 +222,26 @@ | ||
}, | ||
getCallArgument: function getCallArgument() { | ||
var callNr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; | ||
var argNr = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; | ||
getCallArgument(callNr = 0, argNr = 0) { | ||
return this.getCallArguments(callNr)[argNr]; | ||
}, | ||
getCallCount: function getCallCount() { | ||
getCallCount() { | ||
return this[Symbols.calls].length; | ||
}, | ||
showCallArguments: function showCallArguments() { | ||
var additionalInformation = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | ||
var madeCalls = this[Symbols.calls]; | ||
showCallArguments(additionalInformation = []) { | ||
const madeCalls = this[Symbols.calls]; | ||
if (madeCalls.length === 0) { | ||
return "".concat(this[Symbols.name], " was never called!\n"); | ||
return `${this[Symbols.name]} was never called!\n`; | ||
} | ||
var response = ''; | ||
let response = ''; | ||
for (var i = 0; i < madeCalls.length; i++) { | ||
var _args = (0, _serializer.serialize)(madeCalls[i].args); | ||
for (let i = 0; i < madeCalls.length; i++) { | ||
const args = (0, _serializer.serialize)(madeCalls[i].args); | ||
response += `call ${i}: ${args}\n`; | ||
response += "call ".concat(i, ": ").concat(_args, "\n"); | ||
if (additionalInformation[i]) { | ||
response += " ".concat(additionalInformation[i], "\n"); | ||
response += ` ${additionalInformation[i]}\n`; | ||
} | ||
@@ -304,21 +252,12 @@ } | ||
} | ||
}; | ||
var Spy = function () { | ||
function Spy() { | ||
var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'the spy'; | ||
var __mock = arguments.length > 1 ? arguments[1] : undefined; | ||
_classCallCheck(this, Spy); | ||
var spy = function spy() { | ||
for (var _len8 = arguments.length, args = new Array(_len8), _key8 = 0; _key8 < _len8; _key8++) { | ||
args[_key8] = arguments[_key8]; | ||
} | ||
class Spy { | ||
constructor(name = 'the spy', __mock) { | ||
const spy = function (...args) { | ||
spy[Symbols.calls].push({ | ||
args: args | ||
args | ||
}); | ||
return spy[Symbols.func].apply(spy, args); | ||
return spy[Symbols.func](...args); | ||
}; | ||
@@ -335,3 +274,3 @@ | ||
spy[Symbols.func] = function () {}; | ||
spy[Symbols.func] = () => {}; | ||
@@ -342,3 +281,3 @@ spy[Symbols.calls] = []; | ||
}; | ||
(0, _utils.forEach)(SpyFunctions, function (key, value) { | ||
(0, _utils.forEach)(SpyFunctions, (key, value) => { | ||
spy[key] = value; | ||
@@ -349,60 +288,48 @@ }); | ||
_createClass(Spy, null, [{ | ||
key: "configure", | ||
value: function configure(config) { | ||
if (config.useOwnEquals !== undefined) { | ||
DefaultSettings.useOwnEquals = config.useOwnEquals; | ||
} | ||
(0, _testSuite.configureTestSuite)({ | ||
afterEach: config.afterEach, | ||
beforeEach: config.beforeEach | ||
}); | ||
static configure(config) { | ||
if (config.useOwnEquals !== undefined) { | ||
DefaultSettings.useOwnEquals = config.useOwnEquals; | ||
} | ||
}, { | ||
key: "on", | ||
value: function on(obj, methodName) { | ||
var method = obj[methodName]; | ||
if (!(method instanceof Function)) { | ||
throw new Error("The object attribute '".concat(methodName, "' ") + "was: ".concat((0, _serializer.serialize)(method), "\n\n") + 'You should only spy on functions!'); | ||
} | ||
(0, _testSuite.configureTestSuite)({ | ||
afterEach: config.afterEach, | ||
beforeEach: config.beforeEach | ||
}); | ||
} | ||
if (method[Symbols.isSpy]) { | ||
throw new Error("The objects attribute '".concat(methodName, "'") + ' was already spied. Please make sure to spy' + ' only once at a time at any attribute.'); | ||
} | ||
static on(obj, methodName) { | ||
const method = obj[methodName]; | ||
__LOCK__ = false; | ||
var spy = new Spy("the spy on '".concat(methodName, "'"), { | ||
obj: obj, | ||
methodName: methodName | ||
}); | ||
__LOCK__ = true; | ||
obj[methodName] = spy; | ||
return spy; | ||
if (!(method instanceof Function)) { | ||
throw new Error(`The object attribute '${methodName}' ` + `was: ${(0, _serializer.serialize)(method)}\n\n` + 'You should only spy on functions!'); | ||
} | ||
}, { | ||
key: "mock", | ||
value: function mock(obj) { | ||
for (var _len9 = arguments.length, methodNames = new Array(_len9 > 1 ? _len9 - 1 : 0), _key9 = 1; _key9 < _len9; _key9++) { | ||
methodNames[_key9 - 1] = arguments[_key9]; | ||
} | ||
return (0, _mock.createMock)(obj, methodNames); | ||
if (method[Symbols.isSpy]) { | ||
throw new Error(`The objects attribute '${methodName}'` + ' was already spied. Please make sure to spy' + ' only once at a time at any attribute.'); | ||
} | ||
}, { | ||
key: "initMocks", | ||
value: function initMocks(scope) { | ||
(0, _mock.initMocks)(Spy.on, scope); | ||
} | ||
}, { | ||
key: "restoreAll", | ||
value: function restoreAll() { | ||
registry.restoreAll(); | ||
} | ||
}]); | ||
return Spy; | ||
}(); | ||
__LOCK__ = false; | ||
const spy = new Spy(`the spy on '${methodName}'`, { | ||
obj, | ||
methodName | ||
}); | ||
__LOCK__ = true; | ||
obj[methodName] = spy; | ||
return spy; | ||
} | ||
static mock(obj, ...methodNames) { | ||
return (0, _mock.createMock)(obj, methodNames); | ||
} | ||
static initMocks(scope) { | ||
(0, _mock.initMocks)(Spy.on, scope); | ||
} | ||
static restoreAll() { | ||
registry.restoreAll(); | ||
} | ||
} | ||
exports.Spy = Spy; | ||
@@ -409,0 +336,0 @@ |
@@ -10,15 +10,15 @@ "use strict"; | ||
var runner = {}; | ||
var oldDescribe = describe; | ||
const runner = {}; | ||
const oldDescribe = describe; | ||
describe = function describe(name, suite) { | ||
oldDescribe(name, function () { | ||
describe = (name, suite) => { | ||
oldDescribe(name, () => { | ||
(0, _mock.setScope)(name); | ||
beforeEach(function () { | ||
beforeEach(() => { | ||
runner.beforeEach && runner.beforeEach(name); | ||
}); | ||
afterEach(function () { | ||
afterEach(() => { | ||
runner.afterEach && runner.afterEach(name); | ||
}); | ||
var rv = suite(); | ||
const rv = suite(); | ||
(0, _mock.setScope)(undefined); | ||
@@ -29,3 +29,3 @@ return rv; | ||
var configureTestSuite = function configureTestSuite(other) { | ||
const configureTestSuite = other => { | ||
if (other.afterEach) runner.afterEach = other.afterEach; | ||
@@ -32,0 +32,0 @@ if (other.beforeEach) runner.beforeEach = other.beforeEach; |
@@ -8,22 +8,8 @@ "use strict"; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } | ||
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); } | ||
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); } | ||
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } } | ||
var forEach = function forEach(arrOrObj, handler) { | ||
for (var _key in arrOrObj) { | ||
if (arrOrObj.hasOwnProperty(_key)) { | ||
handler(_key, arrOrObj[_key]); | ||
const forEach = (arrOrObj, handler) => { | ||
for (let key in arrOrObj) { | ||
if (arrOrObj.hasOwnProperty(key)) { | ||
handler(key, arrOrObj[key]); | ||
} | ||
@@ -35,7 +21,5 @@ } | ||
var objectKeys = function objectKeys(arrOrObj) { | ||
var keys = []; | ||
forEach(arrOrObj, function (key) { | ||
return keys.push(key); | ||
}); | ||
const objectKeys = arrOrObj => { | ||
const keys = []; | ||
forEach(arrOrObj, key => keys.push(key)); | ||
return keys; | ||
@@ -46,6 +30,5 @@ }; | ||
var mergeArrays = function mergeArrays(arr1, arr2) { | ||
var result = _toConsumableArray(arr1); | ||
forEach(arr2, function (key, val) { | ||
const mergeArrays = (arr1, arr2) => { | ||
const result = [...arr1]; | ||
forEach(arr2, (key, val) => { | ||
if (arr1.indexOf(val) === -1) { | ||
@@ -58,9 +41,7 @@ result.push(val); | ||
var IGNORE = Symbol.for('__Spy_IGNORE__'); | ||
const IGNORE = Symbol.for('__Spy_IGNORE__'); | ||
exports.IGNORE = IGNORE; | ||
var SpyComparator = function () { | ||
function SpyComparator(comparator) { | ||
_classCallCheck(this, SpyComparator); | ||
class SpyComparator { | ||
constructor(comparator) { | ||
_defineProperty(this, "_func", void 0); | ||
@@ -71,20 +52,13 @@ | ||
_createClass(SpyComparator, [{ | ||
key: "compare", | ||
value: function compare(arg) { | ||
if (!this._func(arg)) return 'custom comparison failed'; | ||
} | ||
}]); | ||
compare(arg) { | ||
if (!this._func(arg)) return 'custom comparison failed'; | ||
} | ||
return SpyComparator; | ||
}(); | ||
} | ||
var COMPARE = function COMPARE(comparator) { | ||
return new SpyComparator(comparator); | ||
}; | ||
const COMPARE = comparator => new SpyComparator(comparator); | ||
exports.COMPARE = COMPARE; | ||
var __diff = function __diff(a, b, initial, useOwnEquals) { | ||
var alreadyComparedArray = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : []; | ||
const __diff = (a, b, initial, useOwnEquals, alreadyComparedArray = []) => { | ||
if (a === IGNORE || b === IGNORE) return; | ||
@@ -96,5 +70,5 @@ if (a instanceof SpyComparator) return a.compare(b); | ||
if (a === null || b === null) return 'one was null'; | ||
var aClass = Object.prototype.toString.call(a); | ||
var bClass = Object.prototype.toString.call(b); | ||
if (aClass !== bClass) return "different object types: ".concat(aClass, " <-> ").concat(bClass); | ||
const aClass = Object.prototype.toString.call(a); | ||
const bClass = Object.prototype.toString.call(b); | ||
if (aClass !== bClass) return `different object types: ${aClass} <-> ${bClass}`; | ||
@@ -161,13 +135,12 @@ switch (aClass) { | ||
var compared = _toConsumableArray(alreadyComparedArray).concat([a]); | ||
const compared = [...alreadyComparedArray, a]; | ||
const keys = mergeArrays(objectKeys(a), objectKeys(b)); | ||
var keys = mergeArrays(objectKeys(a), objectKeys(b)); | ||
for (let i = 0; i < keys.length; i++) { | ||
const key = keys[i]; | ||
for (var i = 0; i < keys.length; i++) { | ||
var _key2 = keys[i]; | ||
const diffStr = __diff(a[key], b[key], false, useOwnEquals, compared); | ||
var diffStr = __diff(a[_key2], b[_key2], false, useOwnEquals, compared); | ||
if (diffStr !== undefined) { | ||
return "".concat(initial ? "--> ".concat(_key2) : "".concat(_key2), " / ").concat(diffStr); | ||
return `${initial ? `--> ${key}` : `${key}`} / ${diffStr}`; | ||
} | ||
@@ -177,6 +150,5 @@ } | ||
var differenceOf = function differenceOf(a, b) { | ||
var config = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { | ||
useOwnEquals: true | ||
}; | ||
const differenceOf = (a, b, config = { | ||
useOwnEquals: true | ||
}) => { | ||
return __diff(a, b, true, config.useOwnEquals); | ||
@@ -187,6 +159,4 @@ }; | ||
var toError = function toError(msgOrError, spyName) { | ||
return msgOrError instanceof Error ? msgOrError : new Error(msgOrError || "".concat(spyName, " was requested to throw")); | ||
}; | ||
const toError = (msgOrError, spyName) => msgOrError instanceof Error ? msgOrError : new Error(msgOrError || `${spyName} was requested to throw`); | ||
exports.toError = toError; |
{ | ||
"name": "spy4js", | ||
"version": "2.1.1", | ||
"version": "2.1.3", | ||
"description": "Smart, compact and powerful spy test framework", | ||
@@ -55,7 +55,4 @@ "jest": { | ||
"@babel/plugin-proposal-class-properties": "^7.1.0", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.0.0", | ||
"@babel/plugin-transform-runtime": "^7.1.0", | ||
"@babel/preset-env": "^7.1.5", | ||
"@babel/preset-flow": "^7.0.0", | ||
"@babel/runtime": "^7.1.5", | ||
"babel-core": "7.0.0-bridge.0", | ||
@@ -77,22 +74,16 @@ "babel-eslint": "^10.0.1", | ||
"presets": [ | ||
"@babel/preset-env", | ||
"@babel/preset-flow" | ||
], | ||
"plugins": [ | ||
"@babel/plugin-proposal-class-properties", | ||
"@babel/preset-flow", | ||
[ | ||
"@babel/plugin-transform-runtime", | ||
"@babel/preset-env", | ||
{ | ||
"helpers": false, | ||
"regenerator": true | ||
"targets": { | ||
"node": "8" | ||
} | ||
} | ||
], | ||
[ | ||
"@babel/plugin-proposal-object-rest-spread", | ||
{ | ||
"useBuiltIns": true | ||
} | ||
] | ||
], | ||
"plugins": [ | ||
"@babel/plugin-proposal-class-properties" | ||
] | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
17
83059
514