@xpbytes/moxie
Advanced tools
Comparing version 1.1.2 to 1.2.0
@@ -0,10 +1,15 @@ | ||
import ArgumentError from './ArgumentError'; | ||
import MockVerificationError from './MockVerificationError'; | ||
declare type Predicate = (...args: any[]) => boolean; | ||
declare type MockedCall = { | ||
interface MockedCall { | ||
retval: any; | ||
args?: any[]; | ||
predicate?: Predicate; | ||
}; | ||
} | ||
interface MockedCallMap { | ||
[P: string]: MockedCall[]; | ||
} | ||
/** | ||
* @property {{ [P: string]: MockedCall[] }} expected_calls expected calls | ||
* @property {{ [P: string]: MockedCall[] }} actual_calls actual calls | ||
* @property {MockedCallMap} expectedCalls expected calls | ||
* @property {MockedCallMap} actualCalls actual calls | ||
* | ||
@@ -14,8 +19,5 @@ * @class Mock | ||
declare class Mock { | ||
expected_calls: { | ||
[P: string]: MockedCall[]; | ||
}; | ||
actual_calls: { | ||
[P: string]: MockedCall[]; | ||
}; | ||
[name: string]: any; | ||
expectedCalls: MockedCallMap; | ||
actualCalls: MockedCallMap; | ||
constructor(); | ||
@@ -87,7 +89,11 @@ /** | ||
* @param {string} name the original function name | ||
* @param {...any} actual_args the original arguments | ||
* @param {...any} actualArgs the original arguments | ||
*/ | ||
__call(name: string, ...actual_args: any[]): any; | ||
__call(name: string, ...actualArgs: any[]): any; | ||
} | ||
export default function createMock(): Mock; | ||
export {}; | ||
declare const _default: { | ||
(): Mock; | ||
ArgumentError: typeof ArgumentError; | ||
MockVerificationError: typeof MockVerificationError; | ||
}; | ||
export default _default; |
@@ -1,2 +0,1 @@ | ||
// @ts-check | ||
var ArgumentError = /*@__PURE__*/(function (Error) { | ||
@@ -27,5 +26,7 @@ function ArgumentError(message) { | ||
}(Error)); | ||
// @ts-check | ||
/** | ||
* @property {{ [P: string]: MockedCall[] }} expected_calls expected calls | ||
* @property {{ [P: string]: MockedCall[] }} actual_calls actual calls | ||
* @property {MockedCallMap} expectedCalls expected calls | ||
* @property {MockedCallMap} actualCalls actual calls | ||
* | ||
@@ -35,6 +36,5 @@ * @class Mock | ||
var Mock = function Mock() { | ||
this.expected_calls = {}; | ||
this.actual_calls = {}; | ||
this.expectedCalls = {}; | ||
this.actualCalls = {}; | ||
}; | ||
@@ -72,4 +72,4 @@ /** | ||
this.expected_calls[name] = this.expected_calls[name] || []; | ||
this.expected_calls[name].push({ | ||
this.expectedCalls[name] = this.expectedCalls[name] || []; | ||
this.expectedCalls[name].push({ | ||
retval: retval, | ||
@@ -82,7 +82,7 @@ predicate: predicate | ||
if (!Array.isArray(args)) { | ||
throw new ArgumentError("args must be an array"); | ||
throw new ArgumentError('args must be an array'); | ||
} | ||
this.expected_calls[name] = this.expected_calls[name] || []; | ||
this.expected_calls[name].push({ | ||
this.expectedCalls[name] = this.expectedCalls[name] || []; | ||
this.expectedCalls[name].push({ | ||
retval: retval, | ||
@@ -104,5 +104,5 @@ args: args | ||
Object.keys(this.expected_calls).forEach(function (name) { | ||
var expected = this$1.expected_calls[name]; | ||
var actual = this$1.actual_calls[name]; | ||
Object.keys(this.expectedCalls).forEach(function (name) { | ||
var expected = this$1.expectedCalls[name]; | ||
var actual = this$1.actualCalls[name]; | ||
@@ -134,4 +134,4 @@ if (!actual) { | ||
Mock.prototype.reset = function reset () { | ||
this.expected_calls = {}; | ||
this.actual_calls = {}; | ||
this.expectedCalls = {}; | ||
this.actualCalls = {}; | ||
}; | ||
@@ -190,3 +190,3 @@ /** | ||
* @param {string} name the original function name | ||
* @param{...any} actual_args the original arguments | ||
* @param{...any} actualArgs the original arguments | ||
*/ | ||
@@ -196,22 +196,22 @@ | ||
Mock.prototype.__call = function __call (name) { | ||
var actual_args = [], len = arguments.length - 1; | ||
while ( len-- > 0 ) actual_args[ len ] = arguments[ len + 1 ]; | ||
var actualArgs = [], len = arguments.length - 1; | ||
while ( len-- > 0 ) actualArgs[ len ] = arguments[ len + 1 ]; | ||
var actual_calls = this.actual_calls[name] = this.actual_calls[name] || []; | ||
var index = actual_calls.length; | ||
var expected_call = (this.expected_calls[name] || [])[index]; | ||
var actualCalls = this.actualCalls[name] = this.actualCalls[name] || []; | ||
var index = actualCalls.length; | ||
var expectedCall = (this.expectedCalls[name] || [])[index]; | ||
if (!expected_call) { | ||
throw new MockVerificationError(("No more (>= " + index + ") expects available for " + name + ": " + (this.__print(actual_args)) + " (" + (this.__print(this)) + ")")); | ||
if (!expectedCall) { | ||
throw new MockVerificationError(("No more (>= " + index + ") expects available for " + name + ": " + (this.__print(actualArgs)) + " (" + (this.__print(this)) + ")")); | ||
} | ||
var maybe_expected_args = expected_call.args; | ||
var retval = expected_call.retval; | ||
var predicate = expected_call.predicate; | ||
var maybeExpectedArgs = expectedCall.args; | ||
var retval = expectedCall.retval; | ||
var predicate = expectedCall.predicate; | ||
if (predicate) { | ||
actual_calls.push(expected_call); | ||
actualCalls.push(expectedCall); | ||
if (!predicate.apply(void 0, actual_args)) { | ||
throw new MockVerificationError(("mocked method " + name + " failed predicate w/ " + (this.__print(actual_args)))); | ||
if (!predicate.apply(void 0, actualArgs)) { | ||
throw new MockVerificationError(("mocked method " + name + " failed predicate w/ " + (this.__print(actualArgs)))); | ||
} | ||
@@ -222,20 +222,20 @@ | ||
var expected_args = maybe_expected_args; | ||
var expectedArgs = maybeExpectedArgs; | ||
if (expected_args.length !== actual_args.length) { | ||
throw new MockVerificationError(("mocked method " + name + " expects " + (expected_args.length) + ", got " + (actual_args.length))); | ||
if (expectedArgs.length !== actualArgs.length) { | ||
throw new MockVerificationError(("mocked method " + name + " expects " + (expectedArgs.length) + ", got " + (actualArgs.length))); | ||
} | ||
var zipped_args = expected_args.map(function (arg, i) { return [arg, actual_args[i]]; }); // Intentional == to coerce | ||
var zippedArgs = expectedArgs.map(function (arg, i) { return [arg, actualArgs[i]]; }); // Intentional == to coerce | ||
// TODO: allow for === case equailty style matching later | ||
var fully_matched = zipped_args.every(this.__compare); | ||
var fullyMatched = zippedArgs.every(this.__compare); | ||
if (!fully_matched) { | ||
throw new MockVerificationError(("mocked method " + name + " called with unexpected arguments " + (this.__print(actual_args)) + ", expected " + (this.__print(expected_args)))); | ||
if (!fullyMatched) { | ||
throw new MockVerificationError(("mocked method " + name + " called with unexpected arguments " + (this.__print(actualArgs)) + ", expected " + (this.__print(expectedArgs)))); | ||
} | ||
actual_calls.push({ | ||
actualCalls.push({ | ||
retval: retval, | ||
args: actual_args | ||
args: actualArgs | ||
}); | ||
@@ -248,3 +248,3 @@ return retval; | ||
Symbol('util.inspect.custom').toString(), Symbol.toStringTag.toString(), 'inspect', 'valueOf', '$$typeof'].concat(Object.getOwnPropertyNames(Object.prototype)).concat(Object.getOwnPropertyNames(Mock.prototype)); | ||
var __handler = { | ||
var handler = { | ||
/** | ||
@@ -256,3 +256,3 @@ * Called right before a property (function or otherwise) is retrieved | ||
*/ | ||
get: function (mock, prop) { | ||
get: function get(mock, prop) { | ||
if (mock.hasOwnProperty(prop) || mock[prop]) { | ||
@@ -262,3 +262,3 @@ return mock[prop]; | ||
if (mock.expected_calls[prop]) { | ||
if (mock.expectedCalls[prop]) { | ||
return function () { | ||
@@ -278,11 +278,20 @@ var args = [], len = arguments.length; | ||
var expected_calls = Object.keys(mock.expected_calls) || ['<nothing>']; | ||
throw new ArgumentError(("unmocked method " + name + ", expected one of " + (mock.__print(expected_calls)))); | ||
var expectedCalls = Object.keys(mock.expectedCalls) || ['<nothing>']; | ||
throw new ArgumentError(("unmocked method " + name + ", expected one of " + (mock.__print(expectedCalls)))); | ||
} | ||
}; | ||
/** | ||
* @property {new () => ArgumentError} ArgumentError | ||
* @property {new () => MockVerificationError} MockVerificationError | ||
*/ | ||
function createMock() { | ||
return new Proxy(new Mock(), __handler); | ||
return new Proxy(new Mock(), handler); | ||
} | ||
createMock.ArgumentError = ArgumentError; | ||
createMock.MockVerificationError = MockVerificationError; | ||
module.exports = createMock; | ||
//# sourceMappingURL=moxie.js.map |
@@ -6,3 +6,2 @@ (function (global, factory) { | ||
}(this, (function () { | ||
// @ts-check | ||
var ArgumentError = /*@__PURE__*/(function (Error) { | ||
@@ -33,5 +32,7 @@ function ArgumentError(message) { | ||
}(Error)); | ||
// @ts-check | ||
/** | ||
* @property {{ [P: string]: MockedCall[] }} expected_calls expected calls | ||
* @property {{ [P: string]: MockedCall[] }} actual_calls actual calls | ||
* @property {MockedCallMap} expectedCalls expected calls | ||
* @property {MockedCallMap} actualCalls actual calls | ||
* | ||
@@ -41,6 +42,5 @@ * @class Mock | ||
var Mock = function Mock() { | ||
this.expected_calls = {}; | ||
this.actual_calls = {}; | ||
this.expectedCalls = {}; | ||
this.actualCalls = {}; | ||
}; | ||
@@ -78,4 +78,4 @@ /** | ||
this.expected_calls[name] = this.expected_calls[name] || []; | ||
this.expected_calls[name].push({ | ||
this.expectedCalls[name] = this.expectedCalls[name] || []; | ||
this.expectedCalls[name].push({ | ||
retval: retval, | ||
@@ -88,7 +88,7 @@ predicate: predicate | ||
if (!Array.isArray(args)) { | ||
throw new ArgumentError("args must be an array"); | ||
throw new ArgumentError('args must be an array'); | ||
} | ||
this.expected_calls[name] = this.expected_calls[name] || []; | ||
this.expected_calls[name].push({ | ||
this.expectedCalls[name] = this.expectedCalls[name] || []; | ||
this.expectedCalls[name].push({ | ||
retval: retval, | ||
@@ -110,5 +110,5 @@ args: args | ||
Object.keys(this.expected_calls).forEach(function (name) { | ||
var expected = this$1.expected_calls[name]; | ||
var actual = this$1.actual_calls[name]; | ||
Object.keys(this.expectedCalls).forEach(function (name) { | ||
var expected = this$1.expectedCalls[name]; | ||
var actual = this$1.actualCalls[name]; | ||
@@ -140,4 +140,4 @@ if (!actual) { | ||
Mock.prototype.reset = function reset () { | ||
this.expected_calls = {}; | ||
this.actual_calls = {}; | ||
this.expectedCalls = {}; | ||
this.actualCalls = {}; | ||
}; | ||
@@ -196,3 +196,3 @@ /** | ||
* @param {string} name the original function name | ||
* @param{...any} actual_args the original arguments | ||
* @param{...any} actualArgs the original arguments | ||
*/ | ||
@@ -202,22 +202,22 @@ | ||
Mock.prototype.__call = function __call (name) { | ||
var actual_args = [], len = arguments.length - 1; | ||
while ( len-- > 0 ) actual_args[ len ] = arguments[ len + 1 ]; | ||
var actualArgs = [], len = arguments.length - 1; | ||
while ( len-- > 0 ) actualArgs[ len ] = arguments[ len + 1 ]; | ||
var actual_calls = this.actual_calls[name] = this.actual_calls[name] || []; | ||
var index = actual_calls.length; | ||
var expected_call = (this.expected_calls[name] || [])[index]; | ||
var actualCalls = this.actualCalls[name] = this.actualCalls[name] || []; | ||
var index = actualCalls.length; | ||
var expectedCall = (this.expectedCalls[name] || [])[index]; | ||
if (!expected_call) { | ||
throw new MockVerificationError(("No more (>= " + index + ") expects available for " + name + ": " + (this.__print(actual_args)) + " (" + (this.__print(this)) + ")")); | ||
if (!expectedCall) { | ||
throw new MockVerificationError(("No more (>= " + index + ") expects available for " + name + ": " + (this.__print(actualArgs)) + " (" + (this.__print(this)) + ")")); | ||
} | ||
var maybe_expected_args = expected_call.args; | ||
var retval = expected_call.retval; | ||
var predicate = expected_call.predicate; | ||
var maybeExpectedArgs = expectedCall.args; | ||
var retval = expectedCall.retval; | ||
var predicate = expectedCall.predicate; | ||
if (predicate) { | ||
actual_calls.push(expected_call); | ||
actualCalls.push(expectedCall); | ||
if (!predicate.apply(void 0, actual_args)) { | ||
throw new MockVerificationError(("mocked method " + name + " failed predicate w/ " + (this.__print(actual_args)))); | ||
if (!predicate.apply(void 0, actualArgs)) { | ||
throw new MockVerificationError(("mocked method " + name + " failed predicate w/ " + (this.__print(actualArgs)))); | ||
} | ||
@@ -228,20 +228,20 @@ | ||
var expected_args = maybe_expected_args; | ||
var expectedArgs = maybeExpectedArgs; | ||
if (expected_args.length !== actual_args.length) { | ||
throw new MockVerificationError(("mocked method " + name + " expects " + (expected_args.length) + ", got " + (actual_args.length))); | ||
if (expectedArgs.length !== actualArgs.length) { | ||
throw new MockVerificationError(("mocked method " + name + " expects " + (expectedArgs.length) + ", got " + (actualArgs.length))); | ||
} | ||
var zipped_args = expected_args.map(function (arg, i) { return [arg, actual_args[i]]; }); // Intentional == to coerce | ||
var zippedArgs = expectedArgs.map(function (arg, i) { return [arg, actualArgs[i]]; }); // Intentional == to coerce | ||
// TODO: allow for === case equailty style matching later | ||
var fully_matched = zipped_args.every(this.__compare); | ||
var fullyMatched = zippedArgs.every(this.__compare); | ||
if (!fully_matched) { | ||
throw new MockVerificationError(("mocked method " + name + " called with unexpected arguments " + (this.__print(actual_args)) + ", expected " + (this.__print(expected_args)))); | ||
if (!fullyMatched) { | ||
throw new MockVerificationError(("mocked method " + name + " called with unexpected arguments " + (this.__print(actualArgs)) + ", expected " + (this.__print(expectedArgs)))); | ||
} | ||
actual_calls.push({ | ||
actualCalls.push({ | ||
retval: retval, | ||
args: actual_args | ||
args: actualArgs | ||
}); | ||
@@ -254,3 +254,3 @@ return retval; | ||
Symbol('util.inspect.custom').toString(), Symbol.toStringTag.toString(), 'inspect', 'valueOf', '$$typeof'].concat(Object.getOwnPropertyNames(Object.prototype)).concat(Object.getOwnPropertyNames(Mock.prototype)); | ||
var __handler = { | ||
var handler = { | ||
/** | ||
@@ -262,3 +262,3 @@ * Called right before a property (function or otherwise) is retrieved | ||
*/ | ||
get: function (mock, prop) { | ||
get: function get(mock, prop) { | ||
if (mock.hasOwnProperty(prop) || mock[prop]) { | ||
@@ -268,3 +268,3 @@ return mock[prop]; | ||
if (mock.expected_calls[prop]) { | ||
if (mock.expectedCalls[prop]) { | ||
return function () { | ||
@@ -284,10 +284,19 @@ var args = [], len = arguments.length; | ||
var expected_calls = Object.keys(mock.expected_calls) || ['<nothing>']; | ||
throw new ArgumentError(("unmocked method " + name + ", expected one of " + (mock.__print(expected_calls)))); | ||
var expectedCalls = Object.keys(mock.expectedCalls) || ['<nothing>']; | ||
throw new ArgumentError(("unmocked method " + name + ", expected one of " + (mock.__print(expectedCalls)))); | ||
} | ||
}; | ||
/** | ||
* @property {new () => ArgumentError} ArgumentError | ||
* @property {new () => MockVerificationError} MockVerificationError | ||
*/ | ||
function createMock() { | ||
return new Proxy(new Mock(), __handler); | ||
return new Proxy(new Mock(), handler); | ||
} | ||
createMock.ArgumentError = ArgumentError; | ||
createMock.MockVerificationError = MockVerificationError; | ||
return createMock; | ||
@@ -294,0 +303,0 @@ |
183
moxie.ts
// @ts-check | ||
import ArgumentError from './ArgumentError' | ||
import MockVerificationError from './MockVerificationError' | ||
class ArgumentError extends Error { | ||
constructor(message: string) { | ||
super(message) | ||
Error.captureStackTrace(this, this.constructor) | ||
} | ||
type Predicate = (...args: any[]) => boolean | ||
interface MockedCall { | ||
retval: any | ||
args?: any[] | ||
predicate?: Predicate | ||
} | ||
class MockVerificationError extends Error { | ||
constructor(message: string) { | ||
super(message) | ||
Error.captureStackTrace(this, this.constructor) | ||
} | ||
interface MockedCallMap { | ||
[P: string]: MockedCall[] | ||
} | ||
type Predicate = (...args: any[]) => boolean | ||
type MockedCall = { retval: any, args?: any[], predicate?: Predicate } | ||
/** | ||
* @property {{ [P: string]: MockedCall[] }} expected_calls expected calls | ||
* @property {{ [P: string]: MockedCall[] }} actual_calls actual calls | ||
* @property {MockedCallMap} expectedCalls expected calls | ||
* @property {MockedCallMap} actualCalls actual calls | ||
* | ||
@@ -29,8 +22,10 @@ * @class Mock | ||
class Mock { | ||
expected_calls: { [P: string]: MockedCall[] } | ||
actual_calls: { [P: string]: MockedCall[] } | ||
[name: string]: any | ||
public expectedCalls: MockedCallMap | ||
public actualCalls: MockedCallMap | ||
constructor() { | ||
this.expected_calls = {} | ||
this.actual_calls = {} | ||
this.expectedCalls = {} | ||
this.actualCalls = {} | ||
} | ||
@@ -45,3 +40,3 @@ | ||
*/ | ||
__print(args: any): string { | ||
public __print(args: any): string { | ||
return JSON.stringify(args) | ||
@@ -59,9 +54,16 @@ } | ||
*/ | ||
expect(name: string, retval: any, args: any[] = [], predicate?: Predicate) { | ||
public expect( | ||
name: string, | ||
retval: any, | ||
args: any[] = [], | ||
predicate?: Predicate | ||
) { | ||
if (predicate instanceof Function) { | ||
if (args && (!Array.isArray(args) || args.length > 0)) { | ||
throw new ArgumentError(`args ignored when predicate is given (args: ${this.__print(args)})`) | ||
throw new ArgumentError( | ||
`args ignored when predicate is given (args: ${this.__print(args)})` | ||
) | ||
} | ||
this.expected_calls[name] = this.expected_calls[name] || [] | ||
this.expected_calls[name].push({ retval, predicate }) | ||
this.expectedCalls[name] = this.expectedCalls[name] || [] | ||
this.expectedCalls[name].push({ retval, predicate }) | ||
return | ||
@@ -71,6 +73,6 @@ } | ||
if (!Array.isArray(args)) { | ||
throw new ArgumentError("args must be an array") | ||
throw new ArgumentError('args must be an array') | ||
} | ||
this.expected_calls[name] = this.expected_calls[name] || [] | ||
this.expected_calls[name].push({ retval, args }) | ||
this.expectedCalls[name] = this.expectedCalls[name] || [] | ||
this.expectedCalls[name].push({ retval, args }) | ||
} | ||
@@ -85,6 +87,6 @@ | ||
*/ | ||
verify(): true { | ||
Object.keys(this.expected_calls).forEach((name) => { | ||
const expected = this.expected_calls[name] | ||
const actual = this.actual_calls[name] | ||
public verify(): true { | ||
Object.keys(this.expectedCalls).forEach(name => { | ||
const expected = this.expectedCalls[name] | ||
const actual = this.actualCalls[name] | ||
if (!actual) { | ||
@@ -97,3 +99,6 @@ throw new MockVerificationError( | ||
throw new MockVerificationError( | ||
`expected ${this.__print_call(name, expected[actual.length])}, got [${this.__print_call(name, actual)}]` | ||
`expected ${this.__print_call( | ||
name, | ||
expected[actual.length] | ||
)}, got [${this.__print_call(name, actual)}]` | ||
) | ||
@@ -109,3 +114,3 @@ } | ||
*/ | ||
clear() { | ||
public clear() { | ||
this.reset() | ||
@@ -118,5 +123,5 @@ } | ||
*/ | ||
reset() { | ||
this.expected_calls = {} | ||
this.actual_calls = {} | ||
public reset() { | ||
this.expectedCalls = {} | ||
this.actualCalls = {} | ||
} | ||
@@ -133,8 +138,10 @@ | ||
*/ | ||
__print_call(name: string, data: any): string { | ||
public __print_call(name: string, data: any): string { | ||
if (Array.isArray(data)) { | ||
return data.map((d) => this.__print_call(name, d)).join(', ') | ||
return data.map(d => this.__print_call(name, d)).join(', ') | ||
} | ||
return `${name}(${(data.args || []).join(', ')}) => ${typeof data.retval} (${data.retval})` | ||
return `${name}(${(data.args || []).join( | ||
', ' | ||
)}) => ${typeof data.retval} (${data.retval})` | ||
} | ||
@@ -149,3 +156,3 @@ | ||
*/ | ||
__compare([left, right]: [any, any]): boolean { | ||
public __compare([left, right]: [any, any]): boolean { | ||
// TODO: implement case equality | ||
@@ -162,3 +169,3 @@ return left === right | ||
*/ | ||
then(): this { | ||
public then(): this { | ||
return this | ||
@@ -171,22 +178,26 @@ } | ||
* @param {string} name the original function name | ||
* @param {...any} actual_args the original arguments | ||
* @param {...any} actualArgs the original arguments | ||
*/ | ||
__call(name: string, ...actual_args: any[]) { | ||
const actual_calls = this.actual_calls[name] = this.actual_calls[name] || [] | ||
const index = actual_calls.length | ||
const expected_call = (this.expected_calls[name] || [])[index] | ||
public __call(name: string, ...actualArgs: any[]) { | ||
const actualCalls = (this.actualCalls[name] = this.actualCalls[name] || []) | ||
const index = actualCalls.length | ||
const expectedCall = (this.expectedCalls[name] || [])[index] | ||
if (!expected_call) { | ||
if (!expectedCall) { | ||
throw new MockVerificationError( | ||
`No more (>= ${index}) expects available for ${name}: ${this.__print(actual_args)} (${this.__print(this)})` | ||
) | ||
`No more (>= ${index}) expects available for ${name}: ${this.__print( | ||
actualArgs | ||
)} (${this.__print(this)})` | ||
) | ||
} | ||
const { args: maybe_expected_args, retval, predicate } = expected_call | ||
const { args: maybeExpectedArgs, retval, predicate } = expectedCall | ||
if (predicate) { | ||
actual_calls.push(expected_call) | ||
if (!predicate(...actual_args)) { | ||
actualCalls.push(expectedCall) | ||
if (!predicate(...actualArgs)) { | ||
throw new MockVerificationError( | ||
`mocked method ${name} failed predicate w/ ${this.__print(actual_args)}` | ||
`mocked method ${name} failed predicate w/ ${this.__print( | ||
actualArgs | ||
)}` | ||
) | ||
@@ -198,24 +209,31 @@ } | ||
const expected_args = maybe_expected_args!! | ||
const expectedArgs = maybeExpectedArgs!! | ||
if (expected_args.length !== actual_args.length) { | ||
if (expectedArgs.length !== actualArgs.length) { | ||
throw new MockVerificationError( | ||
`mocked method ${name} expects ${expected_args.length}, got ${actual_args.length}` | ||
`mocked method ${name} expects ${expectedArgs.length}, got ${ | ||
actualArgs.length | ||
}` | ||
) | ||
} | ||
const zipped_args = expected_args.map((arg, i) => [arg, actual_args[i]]) as [any, any][] | ||
const zippedArgs = expectedArgs.map((arg, i) => [ | ||
arg, | ||
actualArgs[i] | ||
]) as Array<[any, any]> | ||
// Intentional == to coerce | ||
// TODO: allow for === case equailty style matching later | ||
const fully_matched = zipped_args.every(this.__compare) | ||
const fullyMatched = zippedArgs.every(this.__compare) | ||
if (!fully_matched) { | ||
if (!fullyMatched) { | ||
throw new MockVerificationError( | ||
`mocked method ${name} called with unexpected arguments ${this.__print(actual_args)}, expected ${this.__print(expected_args)}` | ||
`mocked method ${name} called with unexpected arguments ${this.__print( | ||
actualArgs | ||
)}, expected ${this.__print(expectedArgs)}` | ||
) | ||
} | ||
actual_calls.push({ | ||
actualCalls.push({ | ||
retval, | ||
args: actual_args | ||
args: actualArgs | ||
}) | ||
@@ -234,8 +252,8 @@ | ||
'valueOf', | ||
'$$typeof', | ||
'$$typeof' | ||
] | ||
.concat(Object.getOwnPropertyNames(Object.prototype)) | ||
.concat(Object.getOwnPropertyNames(Mock.prototype)) | ||
.concat(Object.getOwnPropertyNames(Object.prototype)) | ||
.concat(Object.getOwnPropertyNames(Mock.prototype)) | ||
const __handler = { | ||
const handler = { | ||
/** | ||
@@ -247,3 +265,3 @@ * Called right before a property (function or otherwise) is retrieved | ||
*/ | ||
get: function(mock: Mock & { [P: string]: any }, prop: string) { | ||
get(mock: Mock & { [P: string]: any }, prop: string) { | ||
if (mock.hasOwnProperty(prop) || mock[prop]) { | ||
@@ -253,3 +271,3 @@ return mock[prop] | ||
if (mock.expected_calls[prop]) { | ||
if (mock.expectedCalls[prop]) { | ||
return (...args: any[]) => mock.__call(prop, ...args) | ||
@@ -263,11 +281,24 @@ } | ||
const expected_calls = Object.keys(mock.expected_calls) || ['<nothing>'] | ||
const expectedCalls = Object.keys(mock.expectedCalls) || ['<nothing>'] | ||
throw new ArgumentError( | ||
`unmocked method ${name}, expected one of ${mock.__print(expected_calls)}` | ||
) | ||
`unmocked method ${name}, expected one of ${mock.__print(expectedCalls)}` | ||
) | ||
} | ||
} | ||
export default function createMock(): Mock { | ||
return new Proxy(new Mock(), __handler) | ||
/** | ||
* @property {new () => ArgumentError} ArgumentError | ||
* @property {new () => MockVerificationError} MockVerificationError | ||
*/ | ||
function createMock(): Mock { | ||
return new Proxy(new Mock(), handler) | ||
} | ||
createMock.ArgumentError = ArgumentError | ||
createMock.MockVerificationError = MockVerificationError | ||
export default createMock as { | ||
(): Mock | ||
ArgumentError: typeof ArgumentError | ||
MockVerificationError: typeof MockVerificationError | ||
} |
{ | ||
"name": "@xpbytes/moxie", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "Proxy based mock for node and v8", | ||
@@ -23,10 +23,20 @@ "main": "dist/moxie.js", | ||
"test": "ava", | ||
"prepublish": "rimraf dist && yarn test" | ||
"test:coverage": "yarn nyc --reporter=lcov --reporter=html --reporter=text yarn test", | ||
"prepublish": "rimraf dist && yarn format && yarn test && yarn lint", | ||
"format:base": "yarn prettier --arrow-parens=avoid --parser=typescript --no-semi --single-quote --trailing-comma=none", | ||
"format": "yarn format:base --write **/*.ts", | ||
"format:check": "yarn format:base -c **/*.ts", | ||
"lint": "yarn tslint *.ts test/*.ts" | ||
}, | ||
"devDependencies": { | ||
"ava": "^1.2.1", | ||
"esm": "^3.2.4", | ||
"ava": "^1.3.1", | ||
"esm": "^3.2.16", | ||
"microbundle": "^0.9.0", | ||
"nyc": "^13.3.0", | ||
"prettier": "1.16.4", | ||
"rimraf": "^2.6.3", | ||
"terser": "^3.16.1" | ||
"terser": "^3.17.0", | ||
"tslint": "^5.14.0", | ||
"tslint-config-prettier": "^1.18.0", | ||
"typescript": "^3.3.3333" | ||
}, | ||
@@ -33,0 +43,0 @@ "ava": { |
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
78835
14
1043
10