@xpbytes/moxie
Advanced tools
Comparing version 1.1.1 to 1.1.2
@@ -7,2 +7,8 @@ declare type Predicate = (...args: any[]) => boolean; | ||
}; | ||
/** | ||
* @property {{ [P: string]: MockedCall[] }} expected_calls expected calls | ||
* @property {{ [P: string]: MockedCall[] }} actual_calls actual calls | ||
* | ||
* @class Mock | ||
*/ | ||
declare class Mock { | ||
@@ -16,10 +22,69 @@ expected_calls: { | ||
constructor(); | ||
/** | ||
* Helper to stringify the input args | ||
* | ||
* @param {any} args | ||
* @returns {string} | ||
* @memberof Mock | ||
*/ | ||
__print(args: any): string; | ||
/** | ||
* Mock a call to a method | ||
* | ||
* @param {string} name the method name | ||
* @param {any} retval the desired return value | ||
* @param {any[]} [args=[]] the expected arguments, or an empty array if predicate is given | ||
* @param {Predicate|undefined} [predicate=undefined] function to call with the arguments to test a match | ||
* @memberof Mock | ||
*/ | ||
expect(name: string, retval: any, args?: any[], predicate?: Predicate): void; | ||
/** | ||
* Verifies that all expected calls have actually been called | ||
* | ||
* @memberof Mock | ||
* @throws {Error} if an expected call has not been registered | ||
* @returns {true} returns if verified, throws otherwise | ||
*/ | ||
verify(): true; | ||
/** | ||
* Alias for {reset} | ||
*/ | ||
clear(): void; | ||
/** | ||
* Resets all the expected and actual calls | ||
* @memberof Mock | ||
*/ | ||
reset(): void; | ||
/** | ||
* Helper to print out an expected call | ||
* | ||
* @param {string} name | ||
* @param {any} data | ||
* @returns | ||
* @private | ||
* @memberof Mock | ||
*/ | ||
__print_call(name: string, data: any): string; | ||
/** | ||
* Compare two arguments for equality | ||
* | ||
* @param {[any, any]} args | ||
* @returns {boolean} | ||
* @memberof Mock | ||
*/ | ||
__compare([left, right]: [any, any]): boolean; | ||
/** | ||
* No-op in case this was wrapped in a Promise and a caller is checking if it's | ||
* thenable. In this case return self. | ||
* | ||
* @returns {Mock} | ||
* @memberof Mock | ||
*/ | ||
then(): this; | ||
/** | ||
* Called when the mock is called as a function | ||
* | ||
* @param {string} name the original function name | ||
* @param {...any} actual_args the original arguments | ||
*/ | ||
__call(name: string, ...actual_args: any[]): any; | ||
@@ -26,0 +91,0 @@ } |
@@ -0,1 +1,2 @@ | ||
// @ts-check | ||
var ArgumentError = /*@__PURE__*/(function (Error) { | ||
@@ -26,3 +27,10 @@ function ArgumentError(message) { | ||
}(Error)); | ||
/** | ||
* @property {{ [P: string]: MockedCall[] }} expected_calls expected calls | ||
* @property {{ [P: string]: MockedCall[] }} actual_calls actual calls | ||
* | ||
* @class Mock | ||
*/ | ||
var Mock = function Mock() { | ||
@@ -32,7 +40,25 @@ this.expected_calls = {}; | ||
}; | ||
/** | ||
* Helper to stringify the input args | ||
* | ||
* @param {any} args | ||
* @returns {string} | ||
* @memberof Mock | ||
*/ | ||
Mock.prototype.__print = function __print (args) { | ||
return JSON.stringify(args); | ||
}; | ||
/** | ||
* Mock a call to a method | ||
* | ||
* @param {string} name the method name | ||
* @param {any} retval the desired return value | ||
* @param {any[]} [args=[]] the expected arguments, or an empty array if predicate is given | ||
* @param {Predicate|undefined} [predicate=undefined] function to call with the arguments to test a match | ||
* @memberof Mock | ||
*/ | ||
Mock.prototype.expect = function expect (name, retval, args, predicate) { | ||
@@ -64,3 +90,11 @@ if ( args === void 0 ) args = []; | ||
}; | ||
/** | ||
* Verifies that all expected calls have actually been called | ||
* | ||
* @memberof Mock | ||
* @throws {Error} if an expected call has not been registered | ||
* @returns {true} returns if verified, throws otherwise | ||
*/ | ||
Mock.prototype.verify = function verify () { | ||
@@ -83,7 +117,16 @@ var this$1 = this; | ||
}; | ||
/** | ||
* Alias for {reset} | ||
*/ | ||
Mock.prototype.clear = function clear () { | ||
this.reset(); | ||
}; | ||
/** | ||
* Resets all the expected and actual calls | ||
* @memberof Mock | ||
*/ | ||
Mock.prototype.reset = function reset () { | ||
@@ -93,3 +136,13 @@ this.expected_calls = {}; | ||
}; | ||
/** | ||
* Helper to print out an expected call | ||
* | ||
* @param {string} name | ||
* @param {any} data | ||
* @returns | ||
* @private | ||
* @memberof Mock | ||
*/ | ||
Mock.prototype.__print_call = function __print_call (name, data) { | ||
@@ -99,3 +152,3 @@ var this$1 = this; | ||
if (Array.isArray(data)) { | ||
return data.map(function (d) { return this$1.__call(name, d); }).join(', '); | ||
return data.map(function (d) { return this$1.__print_call(name, d); }).join(', '); | ||
} | ||
@@ -105,3 +158,11 @@ | ||
}; | ||
/** | ||
* Compare two arguments for equality | ||
* | ||
* @param {[any, any]} args | ||
* @returns {boolean} | ||
* @memberof Mock | ||
*/ | ||
Mock.prototype.__compare = function __compare (ref) { | ||
@@ -111,9 +172,25 @@ var left = ref[0]; | ||
// TODO: implement case equality | ||
return left === right; | ||
}; | ||
/** | ||
* No-op in case this was wrapped in a Promise and a caller is checking if it's | ||
* thenable. In this case return self. | ||
* | ||
* @returns {Mock} | ||
* @memberof Mock | ||
*/ | ||
Mock.prototype.then = function then () { | ||
return this; | ||
}; | ||
/** | ||
* Called when the mock is called as a function | ||
* | ||
* @param {string} name the original function name | ||
* @param{...any} actual_args the original arguments | ||
*/ | ||
Mock.prototype.__call = function __call (name) { | ||
@@ -151,3 +228,5 @@ var actual_args = [], len = arguments.length - 1; | ||
var zipped_args = expected_args.map(function (arg, i) { return [arg, actual_args[i]]; }); | ||
var zipped_args = expected_args.map(function (arg, i) { return [arg, actual_args[i]]; }); // Intentional == to coerce | ||
// TODO: allow for === case equailty style matching later | ||
var fully_matched = zipped_args.every(this.__compare); | ||
@@ -166,4 +245,12 @@ | ||
var KNOWN = [Symbol('util.inspect.custom').toString(), Symbol.toStringTag.toString(), 'inspect', 'valueOf', '$$typeof'].concat(Object.getOwnPropertyNames(Object.prototype)).concat(Object.getOwnPropertyNames(Mock.prototype)); | ||
var KNOWN = [// The following are called by runtimes whenever they want to inspect the mock | ||
// itself. Whenever that happens, just pass-through. | ||
Symbol('util.inspect.custom').toString(), Symbol.toStringTag.toString(), 'inspect', 'valueOf', '$$typeof'].concat(Object.getOwnPropertyNames(Object.prototype)).concat(Object.getOwnPropertyNames(Mock.prototype)); | ||
var __handler = { | ||
/** | ||
* Called right before a property (function or otherwise) is retrieved | ||
* | ||
* @param {Mock} mock | ||
* @param {string} prop | ||
*/ | ||
get: function (mock, prop) { | ||
@@ -170,0 +257,0 @@ if (mock.hasOwnProperty(prop) || mock[prop]) { |
@@ -6,2 +6,3 @@ (function (global, factory) { | ||
}(this, (function () { | ||
// @ts-check | ||
var ArgumentError = /*@__PURE__*/(function (Error) { | ||
@@ -32,3 +33,10 @@ function ArgumentError(message) { | ||
}(Error)); | ||
/** | ||
* @property {{ [P: string]: MockedCall[] }} expected_calls expected calls | ||
* @property {{ [P: string]: MockedCall[] }} actual_calls actual calls | ||
* | ||
* @class Mock | ||
*/ | ||
var Mock = function Mock() { | ||
@@ -38,7 +46,25 @@ this.expected_calls = {}; | ||
}; | ||
/** | ||
* Helper to stringify the input args | ||
* | ||
* @param {any} args | ||
* @returns {string} | ||
* @memberof Mock | ||
*/ | ||
Mock.prototype.__print = function __print (args) { | ||
return JSON.stringify(args); | ||
}; | ||
/** | ||
* Mock a call to a method | ||
* | ||
* @param {string} name the method name | ||
* @param {any} retval the desired return value | ||
* @param {any[]} [args=[]] the expected arguments, or an empty array if predicate is given | ||
* @param {Predicate|undefined} [predicate=undefined] function to call with the arguments to test a match | ||
* @memberof Mock | ||
*/ | ||
Mock.prototype.expect = function expect (name, retval, args, predicate) { | ||
@@ -70,3 +96,11 @@ if ( args === void 0 ) args = []; | ||
}; | ||
/** | ||
* Verifies that all expected calls have actually been called | ||
* | ||
* @memberof Mock | ||
* @throws {Error} if an expected call has not been registered | ||
* @returns {true} returns if verified, throws otherwise | ||
*/ | ||
Mock.prototype.verify = function verify () { | ||
@@ -89,7 +123,16 @@ var this$1 = this; | ||
}; | ||
/** | ||
* Alias for {reset} | ||
*/ | ||
Mock.prototype.clear = function clear () { | ||
this.reset(); | ||
}; | ||
/** | ||
* Resets all the expected and actual calls | ||
* @memberof Mock | ||
*/ | ||
Mock.prototype.reset = function reset () { | ||
@@ -99,3 +142,13 @@ this.expected_calls = {}; | ||
}; | ||
/** | ||
* Helper to print out an expected call | ||
* | ||
* @param {string} name | ||
* @param {any} data | ||
* @returns | ||
* @private | ||
* @memberof Mock | ||
*/ | ||
Mock.prototype.__print_call = function __print_call (name, data) { | ||
@@ -105,3 +158,3 @@ var this$1 = this; | ||
if (Array.isArray(data)) { | ||
return data.map(function (d) { return this$1.__call(name, d); }).join(', '); | ||
return data.map(function (d) { return this$1.__print_call(name, d); }).join(', '); | ||
} | ||
@@ -111,3 +164,11 @@ | ||
}; | ||
/** | ||
* Compare two arguments for equality | ||
* | ||
* @param {[any, any]} args | ||
* @returns {boolean} | ||
* @memberof Mock | ||
*/ | ||
Mock.prototype.__compare = function __compare (ref) { | ||
@@ -117,9 +178,25 @@ var left = ref[0]; | ||
// TODO: implement case equality | ||
return left === right; | ||
}; | ||
/** | ||
* No-op in case this was wrapped in a Promise and a caller is checking if it's | ||
* thenable. In this case return self. | ||
* | ||
* @returns {Mock} | ||
* @memberof Mock | ||
*/ | ||
Mock.prototype.then = function then () { | ||
return this; | ||
}; | ||
/** | ||
* Called when the mock is called as a function | ||
* | ||
* @param {string} name the original function name | ||
* @param{...any} actual_args the original arguments | ||
*/ | ||
Mock.prototype.__call = function __call (name) { | ||
@@ -157,3 +234,5 @@ var actual_args = [], len = arguments.length - 1; | ||
var zipped_args = expected_args.map(function (arg, i) { return [arg, actual_args[i]]; }); | ||
var zipped_args = expected_args.map(function (arg, i) { return [arg, actual_args[i]]; }); // Intentional == to coerce | ||
// TODO: allow for === case equailty style matching later | ||
var fully_matched = zipped_args.every(this.__compare); | ||
@@ -172,4 +251,12 @@ | ||
var KNOWN = [Symbol('util.inspect.custom').toString(), Symbol.toStringTag.toString(), 'inspect', 'valueOf', '$$typeof'].concat(Object.getOwnPropertyNames(Object.prototype)).concat(Object.getOwnPropertyNames(Mock.prototype)); | ||
var KNOWN = [// The following are called by runtimes whenever they want to inspect the mock | ||
// itself. Whenever that happens, just pass-through. | ||
Symbol('util.inspect.custom').toString(), Symbol.toStringTag.toString(), 'inspect', 'valueOf', '$$typeof'].concat(Object.getOwnPropertyNames(Object.prototype)).concat(Object.getOwnPropertyNames(Mock.prototype)); | ||
var __handler = { | ||
/** | ||
* Called right before a property (function or otherwise) is retrieved | ||
* | ||
* @param {Mock} mock | ||
* @param {string} prop | ||
*/ | ||
get: function (mock, prop) { | ||
@@ -176,0 +263,0 @@ if (mock.hasOwnProperty(prop) || mock[prop]) { |
@@ -127,3 +127,3 @@ // @ts-check | ||
if (Array.isArray(data)) { | ||
return data.map((d) => this.__call(name, d)).join(', ') | ||
return data.map((d) => this.__print_call(name, d)).join(', ') | ||
} | ||
@@ -130,0 +130,0 @@ |
{ | ||
"name": "@xpbytes/moxie", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Proxy based mock for node and v8", | ||
@@ -5,0 +5,0 @@ "main": "dist/moxie.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
74004
981
0