@sprucelabs/test-utils
Version:
Helpful utilities to make asserting more complicated conditions quick and easy! ⚡️
27 lines (26 loc) • 909 B
JavaScript
import assert from '../../assert/assert.js';
export default class Spy {
constructor(object, method) {
this.hitCount = 0;
this.originalMethod = object[method].bind(object);
this.method = method;
this.object = object;
//@ts-ignore
this.object[method] = (...args) => {
this.hitCount++;
this.lastArgs = args;
return this.originalMethod(...args);
};
}
assertCalledTotalTimes(expected) {
if (this.hitCount !== expected) {
throw new Error(`${String(this.method)} was not called ${expected} time(s)! It was called ${this.hitCount} time(s).`);
}
}
assertLastCalledWith(args) {
assert.isEqualDeep(this.lastArgs, args, `${String(this.method)} was not called with the expected argument`);
}
reset() {
this.object[this.method] = this.originalMethod;
}
}