strong-mock 9.1.0 → 9.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.
- package/README.md +16 -0
- package/dist/index.cjs +44 -28
- package/dist/index.d.cts +13 -1
- package/dist/index.d.ts +13 -1
- package/dist/index.js +44 -28
- package/package.json +11 -13
package/README.md
CHANGED
|
@@ -51,6 +51,7 @@ console.log(foo.bar(23)); // 'I am strong!'
|
|
|
51
51
|
- [Reset](#reset)
|
|
52
52
|
- [Resetting expectations](#resetting-expectations)
|
|
53
53
|
- [Mock options](#mock-options)
|
|
54
|
+
- [Name](#name)
|
|
54
55
|
- [Unexpected property return value](#unexpected-property-return-value)
|
|
55
56
|
- [Exact params](#exact-params)
|
|
56
57
|
- [Concrete matcher](#concrete-matcher)
|
|
@@ -465,6 +466,21 @@ const superStrictMock = mock<() => void>();
|
|
|
465
466
|
const strictMock = mock<() => void>({ exactParams: false });
|
|
466
467
|
```
|
|
467
468
|
|
|
469
|
+
### Name
|
|
470
|
+
|
|
471
|
+
The name of a mock appears in error messages e.g., when an unexpected call happens. By default, all mocks are simply named `mock`, but you can set a custom name to make the error messages more descriptive.
|
|
472
|
+
|
|
473
|
+
```typescript
|
|
474
|
+
import { mock, when } from 'strong-mock';
|
|
475
|
+
|
|
476
|
+
interface Service { /* ... */}
|
|
477
|
+
const service = mock<Service>({ name: 'ServiceMock' });
|
|
478
|
+
|
|
479
|
+
when(() => service.foo(1)).thenReturn(2);
|
|
480
|
+
|
|
481
|
+
service.foo(3); // throws "Didn't expect ServiceMock.foo(3) to be called"
|
|
482
|
+
```
|
|
483
|
+
|
|
468
484
|
### Unexpected property return value
|
|
469
485
|
|
|
470
486
|
You can control what happens whenever an unexpected property is accessed, or an unexpected call is made.
|
package/dist/index.cjs
CHANGED
|
@@ -124,13 +124,13 @@ var deepPrint = (value) => (0, import_cloneDeepWith.default)(value, (value2) =>
|
|
|
124
124
|
return void 0;
|
|
125
125
|
});
|
|
126
126
|
var printArgs = (args) => args.map((arg) => printValue(arg)).join(", ");
|
|
127
|
-
var printCall = (property, args) => {
|
|
127
|
+
var printCall = (mockName, property, args) => {
|
|
128
128
|
const prettyProperty = printProperty(property);
|
|
129
129
|
if (args) {
|
|
130
130
|
const prettyArgs = printArgs(args);
|
|
131
|
-
return
|
|
131
|
+
return `${mockName}${(0, import_jest_matcher_utils.RECEIVED_COLOR)(`${prettyProperty}(${prettyArgs})`)}`;
|
|
132
132
|
}
|
|
133
|
-
return
|
|
133
|
+
return `${mockName}${(0, import_jest_matcher_utils.RECEIVED_COLOR)(`${prettyProperty}`)}`;
|
|
134
134
|
};
|
|
135
135
|
var printReturns = ({ isError, isPromise, value }, min, max) => {
|
|
136
136
|
let thenPrefix = "";
|
|
@@ -149,24 +149,24 @@ var printReturns = ({ isError, isPromise, value }, min, max) => {
|
|
|
149
149
|
printValue(value)
|
|
150
150
|
)}).between(${min}, ${max})`;
|
|
151
151
|
};
|
|
152
|
-
var printWhen = (property, args) => {
|
|
152
|
+
var printWhen = (mockName, property, args) => {
|
|
153
153
|
const prettyProperty = printProperty(property);
|
|
154
154
|
if (args) {
|
|
155
|
-
return `when(() =>
|
|
155
|
+
return `when(() => ${mockName}${(0, import_jest_matcher_utils.EXPECTED_COLOR)(
|
|
156
156
|
`${prettyProperty}(${printArgs(args)})`
|
|
157
157
|
)})`;
|
|
158
158
|
}
|
|
159
|
-
return `when(() =>
|
|
159
|
+
return `when(() => ${mockName}${(0, import_jest_matcher_utils.EXPECTED_COLOR)(`${printProperty(property)}`)})`;
|
|
160
160
|
};
|
|
161
|
-
var printExpectation = (property, args, returnValue, min, max) => `${printWhen(property, args)}${printReturns(returnValue, min, max)}`;
|
|
161
|
+
var printExpectation = (mockName, property, args, returnValue, min, max) => `${printWhen(mockName, property, args)}${printReturns(returnValue, min, max)}`;
|
|
162
162
|
var printRemainingExpectations = (expectations) => expectations.length ? `Remaining unmet expectations:
|
|
163
163
|
- ${expectations.map((e) => e.toString()).join("\n - ")}` : "There are no remaining unmet expectations.";
|
|
164
164
|
|
|
165
165
|
// src/errors/unexpected-access.ts
|
|
166
166
|
var UnexpectedAccess = class extends Error {
|
|
167
|
-
constructor(property, expectations) {
|
|
167
|
+
constructor(mockName, property, expectations) {
|
|
168
168
|
super(
|
|
169
|
-
(0, import_jest_matcher_utils2.DIM_COLOR)(`Didn't expect ${printCall(property)} to be accessed.
|
|
169
|
+
(0, import_jest_matcher_utils2.DIM_COLOR)(`Didn't expect ${printCall(mockName, property)} to be accessed.
|
|
170
170
|
|
|
171
171
|
If you expect this property to be accessed then you should
|
|
172
172
|
set an expectation for it with when().
|
|
@@ -242,9 +242,9 @@ ${diff}`;
|
|
|
242
242
|
|
|
243
243
|
// src/errors/unexpected-call.ts
|
|
244
244
|
var UnexpectedCall = class extends Error {
|
|
245
|
-
constructor(property, args, expectations) {
|
|
245
|
+
constructor(mockName, property, args, expectations) {
|
|
246
246
|
var _a;
|
|
247
|
-
const header = `Didn't expect ${printCall(property, args)} to be called.`;
|
|
247
|
+
const header = `Didn't expect ${printCall(mockName, property, args)} to be called.`;
|
|
248
248
|
const propertyExpectations = expectations.filter(
|
|
249
249
|
(e) => e.property === property
|
|
250
250
|
);
|
|
@@ -310,7 +310,8 @@ var unboxReturnValue = ({
|
|
|
310
310
|
|
|
311
311
|
// src/expectation/repository/flexible-repository.ts
|
|
312
312
|
var FlexibleRepository = class {
|
|
313
|
-
constructor(unexpectedProperty = 0 /* THROW */) {
|
|
313
|
+
constructor(mockName, unexpectedProperty = 0 /* THROW */) {
|
|
314
|
+
this.mockName = mockName;
|
|
314
315
|
this.unexpectedProperty = unexpectedProperty;
|
|
315
316
|
this.expectations = /* @__PURE__ */ new Map();
|
|
316
317
|
this.expectedCallStats = /* @__PURE__ */ new Map();
|
|
@@ -342,11 +343,11 @@ var FlexibleRepository = class {
|
|
|
342
343
|
this.handlePropertyWithNoExpectations = (property) => {
|
|
343
344
|
switch (property) {
|
|
344
345
|
case "toString":
|
|
345
|
-
return () =>
|
|
346
|
+
return () => this.mockName;
|
|
346
347
|
case "@@toStringTag":
|
|
347
348
|
case Symbol.toStringTag:
|
|
348
349
|
case "name":
|
|
349
|
-
return
|
|
350
|
+
return this.mockName;
|
|
350
351
|
// Promise.resolve() tries to see if it's a "thenable".
|
|
351
352
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#thenables
|
|
352
353
|
case "then":
|
|
@@ -434,23 +435,24 @@ var FlexibleRepository = class {
|
|
|
434
435
|
}
|
|
435
436
|
getValueForUnexpectedCall(property, args) {
|
|
436
437
|
this.recordUnexpected(property, args);
|
|
437
|
-
throw new UnexpectedCall(property, args, this.getUnmet());
|
|
438
|
+
throw new UnexpectedCall(this.mockName, property, args, this.getUnmet());
|
|
438
439
|
}
|
|
439
440
|
getValueForUnexpectedAccess(property) {
|
|
440
441
|
if (this.unexpectedProperty === 0 /* THROW */) {
|
|
441
442
|
this.recordUnexpected(property, void 0);
|
|
442
|
-
throw new UnexpectedAccess(property, this.getUnmet());
|
|
443
|
+
throw new UnexpectedAccess(this.mockName, property, this.getUnmet());
|
|
443
444
|
}
|
|
444
445
|
return (...args) => {
|
|
445
446
|
this.recordUnexpected(property, args);
|
|
446
|
-
throw new UnexpectedCall(property, args, this.getUnmet());
|
|
447
|
+
throw new UnexpectedCall(this.mockName, property, args, this.getUnmet());
|
|
447
448
|
};
|
|
448
449
|
}
|
|
449
450
|
};
|
|
450
451
|
|
|
451
452
|
// src/expectation/strong-expectation.ts
|
|
452
453
|
var StrongExpectation = class {
|
|
453
|
-
constructor(property, args, returnValue, exactParams = false) {
|
|
454
|
+
constructor(mockName, property, args, returnValue, exactParams = false) {
|
|
455
|
+
this.mockName = mockName;
|
|
454
456
|
this.property = property;
|
|
455
457
|
this.args = args;
|
|
456
458
|
this.returnValue = returnValue;
|
|
@@ -489,6 +491,7 @@ var StrongExpectation = class {
|
|
|
489
491
|
}
|
|
490
492
|
toString() {
|
|
491
493
|
return printExpectation(
|
|
494
|
+
this.mockName,
|
|
492
495
|
this.property,
|
|
493
496
|
this.args,
|
|
494
497
|
this.returnValue,
|
|
@@ -500,10 +503,10 @@ var StrongExpectation = class {
|
|
|
500
503
|
|
|
501
504
|
// src/errors/api.ts
|
|
502
505
|
var UnfinishedExpectation = class extends Error {
|
|
503
|
-
constructor(property, args) {
|
|
506
|
+
constructor(mockName, property, args) {
|
|
504
507
|
super(`There is an unfinished pending expectation:
|
|
505
508
|
|
|
506
|
-
${printWhen(property, args)}
|
|
509
|
+
${printWhen(mockName, property, args)}
|
|
507
510
|
|
|
508
511
|
You should finish it by setting a return value with e.g. thenReturns(),
|
|
509
512
|
even if that value is undefined.`);
|
|
@@ -544,14 +547,15 @@ ${snippet}`
|
|
|
544
547
|
|
|
545
548
|
// src/when/expectation-builder.ts
|
|
546
549
|
var ExpectationBuilderWithFactory = class {
|
|
547
|
-
constructor(createExpectation, concreteMatcher, exactParams) {
|
|
550
|
+
constructor(createExpectation, concreteMatcher, mockName, exactParams) {
|
|
548
551
|
this.createExpectation = createExpectation;
|
|
549
552
|
this.concreteMatcher = concreteMatcher;
|
|
553
|
+
this.mockName = mockName;
|
|
550
554
|
this.exactParams = exactParams;
|
|
551
555
|
}
|
|
552
556
|
setProperty(value) {
|
|
553
557
|
if (this.property) {
|
|
554
|
-
throw new UnfinishedExpectation(this.property, this.args);
|
|
558
|
+
throw new UnfinishedExpectation(this.mockName, this.property, this.args);
|
|
555
559
|
}
|
|
556
560
|
this.property = value;
|
|
557
561
|
}
|
|
@@ -563,6 +567,7 @@ var ExpectationBuilderWithFactory = class {
|
|
|
563
567
|
throw new MissingWhen();
|
|
564
568
|
}
|
|
565
569
|
const expectation = this.createExpectation(
|
|
570
|
+
this.mockName,
|
|
566
571
|
this.property,
|
|
567
572
|
this.args,
|
|
568
573
|
returnValue,
|
|
@@ -644,6 +649,7 @@ var deepEquals = (expected, {
|
|
|
644
649
|
|
|
645
650
|
// src/mock/defaults.ts
|
|
646
651
|
var defaults = {
|
|
652
|
+
name: "mock",
|
|
647
653
|
concreteMatcher: deepEquals,
|
|
648
654
|
unexpectedProperty: 1 /* CALL_THROW */,
|
|
649
655
|
exactParams: false
|
|
@@ -756,27 +762,33 @@ var createStub = (repo, builder, getCurrentMode) => {
|
|
|
756
762
|
};
|
|
757
763
|
|
|
758
764
|
// src/mock/mock.ts
|
|
759
|
-
var strongExpectationFactory = (property, args, returnValue, concreteMatcher, exactParams) => new StrongExpectation(
|
|
765
|
+
var strongExpectationFactory = (name, property, args, returnValue, concreteMatcher, exactParams) => new StrongExpectation(
|
|
766
|
+
name,
|
|
760
767
|
property,
|
|
761
|
-
// Wrap every non-matcher in the default matcher.
|
|
762
768
|
args == null ? void 0 : args.map((arg) => isMatcher(arg) ? arg : concreteMatcher(arg)),
|
|
763
769
|
returnValue,
|
|
764
770
|
exactParams
|
|
765
771
|
);
|
|
766
772
|
var mock = ({
|
|
773
|
+
name,
|
|
767
774
|
unexpectedProperty,
|
|
768
775
|
concreteMatcher,
|
|
769
776
|
exactParams
|
|
770
777
|
} = {}) => {
|
|
771
778
|
const options = {
|
|
779
|
+
name: name != null ? name : currentDefaults.name,
|
|
772
780
|
unexpectedProperty: unexpectedProperty != null ? unexpectedProperty : currentDefaults.unexpectedProperty,
|
|
773
781
|
concreteMatcher: concreteMatcher != null ? concreteMatcher : currentDefaults.concreteMatcher,
|
|
774
782
|
exactParams: exactParams != null ? exactParams : currentDefaults.exactParams
|
|
775
783
|
};
|
|
776
|
-
const repository = new FlexibleRepository(
|
|
784
|
+
const repository = new FlexibleRepository(
|
|
785
|
+
options.name,
|
|
786
|
+
options.unexpectedProperty
|
|
787
|
+
);
|
|
777
788
|
const builder = new ExpectationBuilderWithFactory(
|
|
778
789
|
strongExpectationFactory,
|
|
779
790
|
options.concreteMatcher,
|
|
791
|
+
options.name,
|
|
780
792
|
options.exactParams
|
|
781
793
|
);
|
|
782
794
|
const stub = createStub(repository, builder, getMode);
|
|
@@ -907,9 +919,9 @@ var mergeCalls = (callMap) => new Map(
|
|
|
907
919
|
})
|
|
908
920
|
);
|
|
909
921
|
var UnexpectedCalls = class extends Error {
|
|
910
|
-
constructor(unexpectedCalls, expectations) {
|
|
922
|
+
constructor(mockName, unexpectedCalls, expectations) {
|
|
911
923
|
const printedCalls = Array.from(mergeCalls(unexpectedCalls).entries()).map(
|
|
912
|
-
([property, calls]) => calls.map((call) => printCall(property, call.arguments)).join("\n - ")
|
|
924
|
+
([property, calls]) => calls.map((call) => printCall(mockName, property, call.arguments)).join("\n - ")
|
|
913
925
|
).join("\n - ");
|
|
914
926
|
super(
|
|
915
927
|
(0, import_jest_matcher_utils5.DIM_COLOR)(`The following calls were unexpected:
|
|
@@ -929,7 +941,11 @@ var verifyRepo = (repository) => {
|
|
|
929
941
|
}
|
|
930
942
|
const callStats = repository.getCallStats();
|
|
931
943
|
if (callStats.unexpected.size) {
|
|
932
|
-
throw new UnexpectedCalls(
|
|
944
|
+
throw new UnexpectedCalls(
|
|
945
|
+
repository.mockName,
|
|
946
|
+
callStats.unexpected,
|
|
947
|
+
unmetExpectations
|
|
948
|
+
);
|
|
933
949
|
}
|
|
934
950
|
};
|
|
935
951
|
var verify = (mock2) => {
|
package/dist/index.d.cts
CHANGED
|
@@ -122,6 +122,17 @@ declare enum UnexpectedProperty {
|
|
|
122
122
|
CALL_THROW = 1
|
|
123
123
|
}
|
|
124
124
|
interface MockOptions {
|
|
125
|
+
/**
|
|
126
|
+
* The name of the mock that will be used in error messages. Defaults to `mock`.
|
|
127
|
+
*
|
|
128
|
+
* This can be useful when you want to easily identify the mock from the test
|
|
129
|
+
* output, especially if you have multiple mocks in the same test.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* const service = mock<Service>({ name: 'Service' });
|
|
133
|
+
* service.foo() // "Didn't expect Service.foo() to be called"
|
|
134
|
+
*/
|
|
135
|
+
name?: string;
|
|
125
136
|
/**
|
|
126
137
|
* Controls what should be returned for a property with no expectations.
|
|
127
138
|
*
|
|
@@ -191,6 +202,7 @@ type Mock<T> = T;
|
|
|
191
202
|
*
|
|
192
203
|
* @param options Configure the options for this specific mock, overriding any
|
|
193
204
|
* defaults that were set with {@link setDefaults}.
|
|
205
|
+
* @param options.name The name of the mock that appears in error messages.
|
|
194
206
|
* @param options.unexpectedProperty Controls what happens when an unexpected
|
|
195
207
|
* property is accessed.
|
|
196
208
|
* @param options.concreteMatcher The matcher that will be used when one isn't
|
|
@@ -205,7 +217,7 @@ type Mock<T> = T;
|
|
|
205
217
|
*
|
|
206
218
|
* fn() === 23;
|
|
207
219
|
*/
|
|
208
|
-
declare const mock: <T>({ unexpectedProperty, concreteMatcher, exactParams, }?: MockOptions) => Mock<T>;
|
|
220
|
+
declare const mock: <T>({ name, unexpectedProperty, concreteMatcher, exactParams, }?: MockOptions) => Mock<T>;
|
|
209
221
|
|
|
210
222
|
type Property = string | symbol;
|
|
211
223
|
|
package/dist/index.d.ts
CHANGED
|
@@ -122,6 +122,17 @@ declare enum UnexpectedProperty {
|
|
|
122
122
|
CALL_THROW = 1
|
|
123
123
|
}
|
|
124
124
|
interface MockOptions {
|
|
125
|
+
/**
|
|
126
|
+
* The name of the mock that will be used in error messages. Defaults to `mock`.
|
|
127
|
+
*
|
|
128
|
+
* This can be useful when you want to easily identify the mock from the test
|
|
129
|
+
* output, especially if you have multiple mocks in the same test.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* const service = mock<Service>({ name: 'Service' });
|
|
133
|
+
* service.foo() // "Didn't expect Service.foo() to be called"
|
|
134
|
+
*/
|
|
135
|
+
name?: string;
|
|
125
136
|
/**
|
|
126
137
|
* Controls what should be returned for a property with no expectations.
|
|
127
138
|
*
|
|
@@ -191,6 +202,7 @@ type Mock<T> = T;
|
|
|
191
202
|
*
|
|
192
203
|
* @param options Configure the options for this specific mock, overriding any
|
|
193
204
|
* defaults that were set with {@link setDefaults}.
|
|
205
|
+
* @param options.name The name of the mock that appears in error messages.
|
|
194
206
|
* @param options.unexpectedProperty Controls what happens when an unexpected
|
|
195
207
|
* property is accessed.
|
|
196
208
|
* @param options.concreteMatcher The matcher that will be used when one isn't
|
|
@@ -205,7 +217,7 @@ type Mock<T> = T;
|
|
|
205
217
|
*
|
|
206
218
|
* fn() === 23;
|
|
207
219
|
*/
|
|
208
|
-
declare const mock: <T>({ unexpectedProperty, concreteMatcher, exactParams, }?: MockOptions) => Mock<T>;
|
|
220
|
+
declare const mock: <T>({ name, unexpectedProperty, concreteMatcher, exactParams, }?: MockOptions) => Mock<T>;
|
|
209
221
|
|
|
210
222
|
type Property = string | symbol;
|
|
211
223
|
|
package/dist/index.js
CHANGED
|
@@ -87,13 +87,13 @@ var deepPrint = (value) => cloneDeepWith(value, (value2) => {
|
|
|
87
87
|
return void 0;
|
|
88
88
|
});
|
|
89
89
|
var printArgs = (args) => args.map((arg) => printValue(arg)).join(", ");
|
|
90
|
-
var printCall = (property, args) => {
|
|
90
|
+
var printCall = (mockName, property, args) => {
|
|
91
91
|
const prettyProperty = printProperty(property);
|
|
92
92
|
if (args) {
|
|
93
93
|
const prettyArgs = printArgs(args);
|
|
94
|
-
return
|
|
94
|
+
return `${mockName}${RECEIVED_COLOR(`${prettyProperty}(${prettyArgs})`)}`;
|
|
95
95
|
}
|
|
96
|
-
return
|
|
96
|
+
return `${mockName}${RECEIVED_COLOR(`${prettyProperty}`)}`;
|
|
97
97
|
};
|
|
98
98
|
var printReturns = ({ isError, isPromise, value }, min, max) => {
|
|
99
99
|
let thenPrefix = "";
|
|
@@ -112,24 +112,24 @@ var printReturns = ({ isError, isPromise, value }, min, max) => {
|
|
|
112
112
|
printValue(value)
|
|
113
113
|
)}).between(${min}, ${max})`;
|
|
114
114
|
};
|
|
115
|
-
var printWhen = (property, args) => {
|
|
115
|
+
var printWhen = (mockName, property, args) => {
|
|
116
116
|
const prettyProperty = printProperty(property);
|
|
117
117
|
if (args) {
|
|
118
|
-
return `when(() =>
|
|
118
|
+
return `when(() => ${mockName}${EXPECTED_COLOR(
|
|
119
119
|
`${prettyProperty}(${printArgs(args)})`
|
|
120
120
|
)})`;
|
|
121
121
|
}
|
|
122
|
-
return `when(() =>
|
|
122
|
+
return `when(() => ${mockName}${EXPECTED_COLOR(`${printProperty(property)}`)})`;
|
|
123
123
|
};
|
|
124
|
-
var printExpectation = (property, args, returnValue, min, max) => `${printWhen(property, args)}${printReturns(returnValue, min, max)}`;
|
|
124
|
+
var printExpectation = (mockName, property, args, returnValue, min, max) => `${printWhen(mockName, property, args)}${printReturns(returnValue, min, max)}`;
|
|
125
125
|
var printRemainingExpectations = (expectations) => expectations.length ? `Remaining unmet expectations:
|
|
126
126
|
- ${expectations.map((e) => e.toString()).join("\n - ")}` : "There are no remaining unmet expectations.";
|
|
127
127
|
|
|
128
128
|
// src/errors/unexpected-access.ts
|
|
129
129
|
var UnexpectedAccess = class extends Error {
|
|
130
|
-
constructor(property, expectations) {
|
|
130
|
+
constructor(mockName, property, expectations) {
|
|
131
131
|
super(
|
|
132
|
-
DIM_COLOR(`Didn't expect ${printCall(property)} to be accessed.
|
|
132
|
+
DIM_COLOR(`Didn't expect ${printCall(mockName, property)} to be accessed.
|
|
133
133
|
|
|
134
134
|
If you expect this property to be accessed then you should
|
|
135
135
|
set an expectation for it with when().
|
|
@@ -205,9 +205,9 @@ ${diff}`;
|
|
|
205
205
|
|
|
206
206
|
// src/errors/unexpected-call.ts
|
|
207
207
|
var UnexpectedCall = class extends Error {
|
|
208
|
-
constructor(property, args, expectations) {
|
|
208
|
+
constructor(mockName, property, args, expectations) {
|
|
209
209
|
var _a;
|
|
210
|
-
const header = `Didn't expect ${printCall(property, args)} to be called.`;
|
|
210
|
+
const header = `Didn't expect ${printCall(mockName, property, args)} to be called.`;
|
|
211
211
|
const propertyExpectations = expectations.filter(
|
|
212
212
|
(e) => e.property === property
|
|
213
213
|
);
|
|
@@ -273,7 +273,8 @@ var unboxReturnValue = ({
|
|
|
273
273
|
|
|
274
274
|
// src/expectation/repository/flexible-repository.ts
|
|
275
275
|
var FlexibleRepository = class {
|
|
276
|
-
constructor(unexpectedProperty = 0 /* THROW */) {
|
|
276
|
+
constructor(mockName, unexpectedProperty = 0 /* THROW */) {
|
|
277
|
+
this.mockName = mockName;
|
|
277
278
|
this.unexpectedProperty = unexpectedProperty;
|
|
278
279
|
this.expectations = /* @__PURE__ */ new Map();
|
|
279
280
|
this.expectedCallStats = /* @__PURE__ */ new Map();
|
|
@@ -305,11 +306,11 @@ var FlexibleRepository = class {
|
|
|
305
306
|
this.handlePropertyWithNoExpectations = (property) => {
|
|
306
307
|
switch (property) {
|
|
307
308
|
case "toString":
|
|
308
|
-
return () =>
|
|
309
|
+
return () => this.mockName;
|
|
309
310
|
case "@@toStringTag":
|
|
310
311
|
case Symbol.toStringTag:
|
|
311
312
|
case "name":
|
|
312
|
-
return
|
|
313
|
+
return this.mockName;
|
|
313
314
|
// Promise.resolve() tries to see if it's a "thenable".
|
|
314
315
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#thenables
|
|
315
316
|
case "then":
|
|
@@ -397,23 +398,24 @@ var FlexibleRepository = class {
|
|
|
397
398
|
}
|
|
398
399
|
getValueForUnexpectedCall(property, args) {
|
|
399
400
|
this.recordUnexpected(property, args);
|
|
400
|
-
throw new UnexpectedCall(property, args, this.getUnmet());
|
|
401
|
+
throw new UnexpectedCall(this.mockName, property, args, this.getUnmet());
|
|
401
402
|
}
|
|
402
403
|
getValueForUnexpectedAccess(property) {
|
|
403
404
|
if (this.unexpectedProperty === 0 /* THROW */) {
|
|
404
405
|
this.recordUnexpected(property, void 0);
|
|
405
|
-
throw new UnexpectedAccess(property, this.getUnmet());
|
|
406
|
+
throw new UnexpectedAccess(this.mockName, property, this.getUnmet());
|
|
406
407
|
}
|
|
407
408
|
return (...args) => {
|
|
408
409
|
this.recordUnexpected(property, args);
|
|
409
|
-
throw new UnexpectedCall(property, args, this.getUnmet());
|
|
410
|
+
throw new UnexpectedCall(this.mockName, property, args, this.getUnmet());
|
|
410
411
|
};
|
|
411
412
|
}
|
|
412
413
|
};
|
|
413
414
|
|
|
414
415
|
// src/expectation/strong-expectation.ts
|
|
415
416
|
var StrongExpectation = class {
|
|
416
|
-
constructor(property, args, returnValue, exactParams = false) {
|
|
417
|
+
constructor(mockName, property, args, returnValue, exactParams = false) {
|
|
418
|
+
this.mockName = mockName;
|
|
417
419
|
this.property = property;
|
|
418
420
|
this.args = args;
|
|
419
421
|
this.returnValue = returnValue;
|
|
@@ -452,6 +454,7 @@ var StrongExpectation = class {
|
|
|
452
454
|
}
|
|
453
455
|
toString() {
|
|
454
456
|
return printExpectation(
|
|
457
|
+
this.mockName,
|
|
455
458
|
this.property,
|
|
456
459
|
this.args,
|
|
457
460
|
this.returnValue,
|
|
@@ -463,10 +466,10 @@ var StrongExpectation = class {
|
|
|
463
466
|
|
|
464
467
|
// src/errors/api.ts
|
|
465
468
|
var UnfinishedExpectation = class extends Error {
|
|
466
|
-
constructor(property, args) {
|
|
469
|
+
constructor(mockName, property, args) {
|
|
467
470
|
super(`There is an unfinished pending expectation:
|
|
468
471
|
|
|
469
|
-
${printWhen(property, args)}
|
|
472
|
+
${printWhen(mockName, property, args)}
|
|
470
473
|
|
|
471
474
|
You should finish it by setting a return value with e.g. thenReturns(),
|
|
472
475
|
even if that value is undefined.`);
|
|
@@ -507,14 +510,15 @@ ${snippet}`
|
|
|
507
510
|
|
|
508
511
|
// src/when/expectation-builder.ts
|
|
509
512
|
var ExpectationBuilderWithFactory = class {
|
|
510
|
-
constructor(createExpectation, concreteMatcher, exactParams) {
|
|
513
|
+
constructor(createExpectation, concreteMatcher, mockName, exactParams) {
|
|
511
514
|
this.createExpectation = createExpectation;
|
|
512
515
|
this.concreteMatcher = concreteMatcher;
|
|
516
|
+
this.mockName = mockName;
|
|
513
517
|
this.exactParams = exactParams;
|
|
514
518
|
}
|
|
515
519
|
setProperty(value) {
|
|
516
520
|
if (this.property) {
|
|
517
|
-
throw new UnfinishedExpectation(this.property, this.args);
|
|
521
|
+
throw new UnfinishedExpectation(this.mockName, this.property, this.args);
|
|
518
522
|
}
|
|
519
523
|
this.property = value;
|
|
520
524
|
}
|
|
@@ -526,6 +530,7 @@ var ExpectationBuilderWithFactory = class {
|
|
|
526
530
|
throw new MissingWhen();
|
|
527
531
|
}
|
|
528
532
|
const expectation = this.createExpectation(
|
|
533
|
+
this.mockName,
|
|
529
534
|
this.property,
|
|
530
535
|
this.args,
|
|
531
536
|
returnValue,
|
|
@@ -607,6 +612,7 @@ var deepEquals = (expected, {
|
|
|
607
612
|
|
|
608
613
|
// src/mock/defaults.ts
|
|
609
614
|
var defaults = {
|
|
615
|
+
name: "mock",
|
|
610
616
|
concreteMatcher: deepEquals,
|
|
611
617
|
unexpectedProperty: 1 /* CALL_THROW */,
|
|
612
618
|
exactParams: false
|
|
@@ -719,27 +725,33 @@ var createStub = (repo, builder, getCurrentMode) => {
|
|
|
719
725
|
};
|
|
720
726
|
|
|
721
727
|
// src/mock/mock.ts
|
|
722
|
-
var strongExpectationFactory = (property, args, returnValue, concreteMatcher, exactParams) => new StrongExpectation(
|
|
728
|
+
var strongExpectationFactory = (name, property, args, returnValue, concreteMatcher, exactParams) => new StrongExpectation(
|
|
729
|
+
name,
|
|
723
730
|
property,
|
|
724
|
-
// Wrap every non-matcher in the default matcher.
|
|
725
731
|
args == null ? void 0 : args.map((arg) => isMatcher(arg) ? arg : concreteMatcher(arg)),
|
|
726
732
|
returnValue,
|
|
727
733
|
exactParams
|
|
728
734
|
);
|
|
729
735
|
var mock = ({
|
|
736
|
+
name,
|
|
730
737
|
unexpectedProperty,
|
|
731
738
|
concreteMatcher,
|
|
732
739
|
exactParams
|
|
733
740
|
} = {}) => {
|
|
734
741
|
const options = {
|
|
742
|
+
name: name != null ? name : currentDefaults.name,
|
|
735
743
|
unexpectedProperty: unexpectedProperty != null ? unexpectedProperty : currentDefaults.unexpectedProperty,
|
|
736
744
|
concreteMatcher: concreteMatcher != null ? concreteMatcher : currentDefaults.concreteMatcher,
|
|
737
745
|
exactParams: exactParams != null ? exactParams : currentDefaults.exactParams
|
|
738
746
|
};
|
|
739
|
-
const repository = new FlexibleRepository(
|
|
747
|
+
const repository = new FlexibleRepository(
|
|
748
|
+
options.name,
|
|
749
|
+
options.unexpectedProperty
|
|
750
|
+
);
|
|
740
751
|
const builder = new ExpectationBuilderWithFactory(
|
|
741
752
|
strongExpectationFactory,
|
|
742
753
|
options.concreteMatcher,
|
|
754
|
+
options.name,
|
|
743
755
|
options.exactParams
|
|
744
756
|
);
|
|
745
757
|
const stub = createStub(repository, builder, getMode);
|
|
@@ -870,9 +882,9 @@ var mergeCalls = (callMap) => new Map(
|
|
|
870
882
|
})
|
|
871
883
|
);
|
|
872
884
|
var UnexpectedCalls = class extends Error {
|
|
873
|
-
constructor(unexpectedCalls, expectations) {
|
|
885
|
+
constructor(mockName, unexpectedCalls, expectations) {
|
|
874
886
|
const printedCalls = Array.from(mergeCalls(unexpectedCalls).entries()).map(
|
|
875
|
-
([property, calls]) => calls.map((call) => printCall(property, call.arguments)).join("\n - ")
|
|
887
|
+
([property, calls]) => calls.map((call) => printCall(mockName, property, call.arguments)).join("\n - ")
|
|
876
888
|
).join("\n - ");
|
|
877
889
|
super(
|
|
878
890
|
DIM_COLOR3(`The following calls were unexpected:
|
|
@@ -892,7 +904,11 @@ var verifyRepo = (repository) => {
|
|
|
892
904
|
}
|
|
893
905
|
const callStats = repository.getCallStats();
|
|
894
906
|
if (callStats.unexpected.size) {
|
|
895
|
-
throw new UnexpectedCalls(
|
|
907
|
+
throw new UnexpectedCalls(
|
|
908
|
+
repository.mockName,
|
|
909
|
+
callStats.unexpected,
|
|
910
|
+
unmetExpectations
|
|
911
|
+
);
|
|
896
912
|
}
|
|
897
913
|
};
|
|
898
914
|
var verify = (mock2) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "strong-mock",
|
|
3
|
-
"version": "9.1
|
|
3
|
+
"version": "9.2.1",
|
|
4
4
|
"description": "Type safe mocking library for TypeScript",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"tdd",
|
|
@@ -41,27 +41,25 @@
|
|
|
41
41
|
"dist"
|
|
42
42
|
],
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"jest-diff": "~30.
|
|
45
|
-
"jest-matcher-utils": "~30.
|
|
46
|
-
"lodash": "~4.
|
|
44
|
+
"jest-diff": "~30.3.0",
|
|
45
|
+
"jest-matcher-utils": "~30.3.0",
|
|
46
|
+
"lodash": "~4.18.0"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@arethetypeswrong/cli": "~0.18.0",
|
|
50
50
|
"@nighttrax/eslint-config-ts": "~12.1.0",
|
|
51
51
|
"@types/lodash": "~4.17.0",
|
|
52
|
-
"@types/node": "~24.
|
|
53
|
-
"@vitest/coverage-v8": "~4.
|
|
54
|
-
"doctoc": "~2.
|
|
52
|
+
"@types/node": "~24.12.0",
|
|
53
|
+
"@vitest/coverage-v8": "~4.1.0",
|
|
54
|
+
"doctoc": "~2.3.0",
|
|
55
55
|
"eslint": "~9.39.0",
|
|
56
|
-
"prettier": "~3.
|
|
57
|
-
"sm-old": "npm:strong-mock@
|
|
56
|
+
"prettier": "~3.8.0",
|
|
57
|
+
"sm-old": "npm:strong-mock@8.0.1",
|
|
58
58
|
"standard-version": "~9.5.0",
|
|
59
|
-
"strip-ansi": "~7.
|
|
60
|
-
"ts-node": "~10.9.2",
|
|
61
|
-
"tslib": "~2.8.0",
|
|
59
|
+
"strip-ansi": "~7.2.0",
|
|
62
60
|
"tsup": "~8.5.0",
|
|
63
61
|
"typescript": "~5.9.2",
|
|
64
|
-
"vitest": "~4.
|
|
62
|
+
"vitest": "~4.1.0"
|
|
65
63
|
},
|
|
66
64
|
"scripts": {
|
|
67
65
|
"docs": "doctoc --title '**Table of Contents**' README.md",
|