@sprucelabs/test-utils 5.1.553 → 5.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,3 +7,5 @@ export * from './assert/assert';
7
7
  export { default as test } from './decorators';
8
8
  export { default as assertUtil } from './assert/assert.utility';
9
9
  export { default as StackCleaner } from './StackCleaner';
10
+ export { default as Spier } from './utilities/spies/Spier';
11
+ export { default as Spy } from './utilities/spies/Spy';
@@ -7,3 +7,5 @@ export * from './assert/assert.js';
7
7
  export { default as test } from './decorators.js';
8
8
  export { default as assertUtil } from './assert/assert.utility.js';
9
9
  export { default as StackCleaner } from './StackCleaner.js';
10
+ export { default as Spier } from './utilities/spies/Spier.js';
11
+ export { default as Spy } from './utilities/spies/Spy.js';
@@ -0,0 +1,6 @@
1
+ import Spy from './Spy';
2
+ export default class Spier {
3
+ private static spyInstances;
4
+ static Spier(): Spier;
5
+ spy<T extends Record<string, any>>(object: T, method: keyof T): Spy<T, keyof T>;
6
+ }
@@ -0,0 +1,20 @@
1
+ import Spy from './Spy.js';
2
+ class Spier {
3
+ static Spier() {
4
+ for (const spy of Spier.spyInstances) {
5
+ spy.reset();
6
+ }
7
+ Spier.spyInstances = [];
8
+ return new Spier();
9
+ }
10
+ spy(object, method) {
11
+ if (!object) {
12
+ throw new Error('You must pass a function to spy on');
13
+ }
14
+ const spy = new Spy(object, method);
15
+ Spier.spyInstances.push(spy);
16
+ return spy;
17
+ }
18
+ }
19
+ Spier.spyInstances = [];
20
+ export default Spier;
@@ -0,0 +1,11 @@
1
+ export default class Spy<T extends Record<string, any>, M extends keyof T> {
2
+ private method;
3
+ private hitCount;
4
+ private originalMethod;
5
+ private object;
6
+ private lastArgs?;
7
+ constructor(object: T, method: M);
8
+ assertCalledTotalTimes(expected: number): void;
9
+ assertLastCalledWith(args: string[]): void;
10
+ reset(): void;
11
+ }
@@ -0,0 +1,26 @@
1
+ import assert from '../../assert/assert.js';
2
+ export default class Spy {
3
+ constructor(object, method) {
4
+ this.hitCount = 0;
5
+ this.originalMethod = object[method].bind(object);
6
+ this.method = method;
7
+ this.object = object;
8
+ //@ts-ignore
9
+ this.object[method] = (...args) => {
10
+ this.hitCount++;
11
+ this.lastArgs = args;
12
+ return this.originalMethod(...args);
13
+ };
14
+ }
15
+ assertCalledTotalTimes(expected) {
16
+ if (this.hitCount !== expected) {
17
+ throw new Error(`${String(this.method)} was not called ${expected} time(s)! It was called ${this.hitCount} time(s).`);
18
+ }
19
+ }
20
+ assertLastCalledWith(args) {
21
+ assert.isEqualDeep(this.lastArgs, args, `${String(this.method)} was not called with the expected argument`);
22
+ }
23
+ reset() {
24
+ this.object[this.method] = this.originalMethod;
25
+ }
26
+ }
package/build/index.d.ts CHANGED
@@ -7,3 +7,5 @@ export * from './assert/assert';
7
7
  export { default as test } from './decorators';
8
8
  export { default as assertUtil } from './assert/assert.utility';
9
9
  export { default as StackCleaner } from './StackCleaner';
10
+ export { default as Spier } from './utilities/spies/Spier';
11
+ export { default as Spy } from './utilities/spies/Spy';
package/build/index.js CHANGED
@@ -17,7 +17,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
17
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
18
18
  };
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.StackCleaner = exports.assertUtil = exports.test = exports.assert = exports.default = exports.generateId = exports.errorAssertUtil = exports.errorAssert = void 0;
20
+ exports.Spy = exports.Spier = exports.StackCleaner = exports.assertUtil = exports.test = exports.assert = exports.default = exports.generateId = exports.errorAssertUtil = exports.errorAssert = void 0;
21
21
  var errorAssert_1 = require("./utilities/errorAssert");
22
22
  Object.defineProperty(exports, "errorAssert", { enumerable: true, get: function () { return __importDefault(errorAssert_1).default; } });
23
23
  var errorAssert_utility_1 = require("./utilities/errorAssert.utility");
@@ -35,3 +35,7 @@ var assert_utility_1 = require("./assert/assert.utility");
35
35
  Object.defineProperty(exports, "assertUtil", { enumerable: true, get: function () { return __importDefault(assert_utility_1).default; } });
36
36
  var StackCleaner_1 = require("./StackCleaner");
37
37
  Object.defineProperty(exports, "StackCleaner", { enumerable: true, get: function () { return __importDefault(StackCleaner_1).default; } });
38
+ var Spier_1 = require("./utilities/spies/Spier");
39
+ Object.defineProperty(exports, "Spier", { enumerable: true, get: function () { return __importDefault(Spier_1).default; } });
40
+ var Spy_1 = require("./utilities/spies/Spy");
41
+ Object.defineProperty(exports, "Spy", { enumerable: true, get: function () { return __importDefault(Spy_1).default; } });
@@ -0,0 +1,6 @@
1
+ import Spy from './Spy';
2
+ export default class Spier {
3
+ private static spyInstances;
4
+ static Spier(): Spier;
5
+ spy<T extends Record<string, any>>(object: T, method: keyof T): Spy<T, keyof T>;
6
+ }
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const Spy_1 = __importDefault(require("./Spy"));
7
+ class Spier {
8
+ static Spier() {
9
+ for (const spy of Spier.spyInstances) {
10
+ spy.reset();
11
+ }
12
+ Spier.spyInstances = [];
13
+ return new Spier();
14
+ }
15
+ spy(object, method) {
16
+ if (!object) {
17
+ throw new Error('You must pass a function to spy on');
18
+ }
19
+ const spy = new Spy_1.default(object, method);
20
+ Spier.spyInstances.push(spy);
21
+ return spy;
22
+ }
23
+ }
24
+ Spier.spyInstances = [];
25
+ exports.default = Spier;
@@ -0,0 +1,11 @@
1
+ export default class Spy<T extends Record<string, any>, M extends keyof T> {
2
+ private method;
3
+ private hitCount;
4
+ private originalMethod;
5
+ private object;
6
+ private lastArgs?;
7
+ constructor(object: T, method: M);
8
+ assertCalledTotalTimes(expected: number): void;
9
+ assertLastCalledWith(args: string[]): void;
10
+ reset(): void;
11
+ }
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const assert_1 = __importDefault(require("../../assert/assert"));
7
+ class Spy {
8
+ constructor(object, method) {
9
+ this.hitCount = 0;
10
+ this.originalMethod = object[method].bind(object);
11
+ this.method = method;
12
+ this.object = object;
13
+ //@ts-ignore
14
+ this.object[method] = (...args) => {
15
+ this.hitCount++;
16
+ this.lastArgs = args;
17
+ return this.originalMethod(...args);
18
+ };
19
+ }
20
+ assertCalledTotalTimes(expected) {
21
+ if (this.hitCount !== expected) {
22
+ throw new Error(`${String(this.method)} was not called ${expected} time(s)! It was called ${this.hitCount} time(s).`);
23
+ }
24
+ }
25
+ assertLastCalledWith(args) {
26
+ assert_1.default.isEqualDeep(this.lastArgs, args, `${String(this.method)} was not called with the expected argument`);
27
+ }
28
+ reset() {
29
+ this.object[this.method] = this.originalMethod;
30
+ }
31
+ }
32
+ exports.default = Spy;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "5.1.553",
6
+ "version": "5.2.1",
7
7
  "files": [
8
8
  "build"
9
9
  ],
@@ -76,7 +76,7 @@
76
76
  "@types/deep-equal": "^1.0.4",
77
77
  "@types/jest": "^29.5.14",
78
78
  "@types/lodash": "^4.17.15",
79
- "@types/node": "^22.13.0",
79
+ "@types/node": "^22.13.1",
80
80
  "chokidar-cli": "^3.0.0",
81
81
  "eslint": "^9.19.0",
82
82
  "eslint-config-spruce": "^11.2.26",