UNPKG

@sprucelabs/test-utils

Version:

Helpful utilities to make asserting more complicated conditions quick and easy! ⚡️

33 lines (32 loc) 1.16 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const assert_1 = __importDefault(require("../../assert/assert")); 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_1.default.isEqualDeep(this.lastArgs, args, `${String(this.method)} was not called with the expected argument`); } reset() { this.object[this.method] = this.originalMethod; } } exports.default = Spy;