velocious 1.0.668 → 1.0.669

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.
@@ -1,762 +1,3 @@
1
1
  // @ts-check
2
2
 
3
- import {formatValue, minifiedStringify} from "../utils/format-value.js"
4
- import {anythingDifferent} from "set-state-compare/build/diff-utils.js"
5
- import BaseExpect from "./base-expect.js"
6
- import ExpectToChange from "./expect-to-change.js"
7
- import {
8
- isArrayContaining,
9
- isObjectContaining,
10
- matchArrayContaining,
11
- matchObject
12
- } from "./expect-utils.js"
13
-
14
- export default class Expect extends BaseExpect {
15
- /**
16
- * Runs constructor.
17
- * @param {ReturnType<typeof JSON.parse>} object - Object.
18
- */
19
- constructor(object) {
20
- super()
21
- this._object = object
22
-
23
- /**
24
- * Narrows the runtime value to the documented type.
25
- * @type {Array<Expect | ExpectToChange>} */
26
- this.expectations = []
27
- }
28
-
29
- /**
30
- * Runs and change.
31
- * @param {() => Promise<number>} changeCallback - Change callback.
32
- * @returns {ExpectToChange} - The and change.
33
- */
34
- andChange(changeCallback) {
35
- return this.toChange(changeCallback)
36
- }
37
-
38
- /**
39
- * Returns not.
40
- * @returns {this} - A value.
41
- */
42
- get not() {
43
- this._not = true
44
-
45
- return this
46
- }
47
-
48
- /**
49
- * Runs to be.
50
- * @param {ReturnType<typeof JSON.parse>} result - Result.
51
- * @returns {void} - No return value.
52
- */
53
- toBe(result) {
54
- if (this._not) {
55
- if (this._object === result) {
56
- const objectPrint = formatValue(this._object)
57
- const resultPrint = formatValue(result)
58
-
59
- throw new Error(`${objectPrint} was unexpected not to be ${resultPrint}`)
60
- }
61
- } else {
62
- if (this._object !== result) {
63
- const objectPrint = formatValue(this._object)
64
- const resultPrint = formatValue(result)
65
-
66
- throw new Error(`${objectPrint} wasn't expected be ${resultPrint}`)
67
- }
68
- }
69
- }
70
-
71
- /**
72
- * Runs to be less than.
73
- * @param {number} result - Result.
74
- * @returns {void} - No return value.
75
- */
76
- toBeLessThan(result) {
77
- if (typeof this._object !== "number" || typeof result !== "number") {
78
- throw new Error(`Expected numbers but got ${typeof this._object} and ${typeof result}`)
79
- }
80
-
81
- if (this._not) {
82
- if (this._object < result) {
83
- const objectPrint = formatValue(this._object)
84
- const resultPrint = formatValue(result)
85
-
86
- throw new Error(`${objectPrint} was unexpected to be less than ${resultPrint}`)
87
- }
88
- } else {
89
- if (this._object >= result) {
90
- const objectPrint = formatValue(this._object)
91
- const resultPrint = formatValue(result)
92
-
93
- throw new Error(`${objectPrint} wasn't expected to be greater than or equal to ${resultPrint}`)
94
- }
95
- }
96
- }
97
-
98
- /**
99
- * Runs to be less than or equal.
100
- * @param {number} result - Result.
101
- * @returns {void} - No return value.
102
- */
103
- toBeLessThanOrEqual(result) {
104
- if (typeof this._object !== "number" || typeof result !== "number") {
105
- throw new Error(`Expected numbers but got ${typeof this._object} and ${typeof result}`)
106
- }
107
-
108
- if (this._not) {
109
- if (this._object <= result) {
110
- const objectPrint = formatValue(this._object)
111
- const resultPrint = formatValue(result)
112
-
113
- throw new Error(`${objectPrint} was unexpected to be less than or equal to ${resultPrint}`)
114
- }
115
- } else {
116
- if (this._object > result) {
117
- const objectPrint = formatValue(this._object)
118
- const resultPrint = formatValue(result)
119
-
120
- throw new Error(`${objectPrint} wasn't expected to be greater than ${resultPrint}`)
121
- }
122
- }
123
- }
124
-
125
- /**
126
- * Runs to be greater than.
127
- * @param {number} result - Result.
128
- * @returns {void} - No return value.
129
- */
130
- toBeGreaterThan(result) {
131
- if (typeof this._object !== "number" || typeof result !== "number") {
132
- throw new Error(`Expected numbers but got ${typeof this._object} and ${typeof result}`)
133
- }
134
-
135
- if (this._not) {
136
- if (this._object > result) {
137
- const objectPrint = formatValue(this._object)
138
- const resultPrint = formatValue(result)
139
-
140
- throw new Error(`${objectPrint} was unexpected to be greater than ${resultPrint}`)
141
- }
142
- } else {
143
- if (this._object <= result) {
144
- const objectPrint = formatValue(this._object)
145
- const resultPrint = formatValue(result)
146
-
147
- throw new Error(`${objectPrint} wasn't expected to be less than or equal to ${resultPrint}`)
148
- }
149
- }
150
- }
151
-
152
- /**
153
- * Runs to be greater than or equal.
154
- * @param {number} result - Result.
155
- * @returns {void} - No return value.
156
- */
157
- toBeGreaterThanOrEqual(result) {
158
- if (typeof this._object !== "number" || typeof result !== "number") {
159
- throw new Error(`Expected numbers but got ${typeof this._object} and ${typeof result}`)
160
- }
161
-
162
- if (this._not) {
163
- if (this._object >= result) {
164
- const objectPrint = formatValue(this._object)
165
- const resultPrint = formatValue(result)
166
-
167
- throw new Error(`${objectPrint} was unexpected to be greater than or equal to ${resultPrint}`)
168
- }
169
- } else {
170
- if (this._object < result) {
171
- const objectPrint = formatValue(this._object)
172
- const resultPrint = formatValue(result)
173
-
174
- throw new Error(`${objectPrint} wasn't expected to be less than ${resultPrint}`)
175
- }
176
- }
177
- }
178
-
179
- /**
180
- * Runs to be close to.
181
- * @param {number} result - Result.
182
- * @param {number} [precision] - Decimal precision.
183
- * @returns {void} - No return value.
184
- */
185
- toBeCloseTo(result, precision = 2) {
186
- if (typeof this._object !== "number" || typeof result !== "number") {
187
- throw new Error(`Expected numbers but got ${typeof this._object} and ${typeof result}`)
188
- }
189
-
190
- if (typeof precision !== "number" || !Number.isFinite(precision)) {
191
- throw new Error(`Expected precision to be a number but got ${typeof precision}`)
192
- }
193
-
194
- const tolerance = 0.5 * Math.pow(10, -precision)
195
- const diff = Math.abs(this._object - result)
196
- const isClose = diff <= tolerance
197
-
198
- if (this._not) {
199
- if (isClose) {
200
- const objectPrint = formatValue(this._object)
201
- const resultPrint = formatValue(result)
202
-
203
- throw new Error(`${objectPrint} was unexpected to be close to ${resultPrint}`)
204
- }
205
- } else {
206
- if (!isClose) {
207
- const objectPrint = formatValue(this._object)
208
- const resultPrint = formatValue(result)
209
-
210
- throw new Error(`${objectPrint} wasn't expected to be close to ${resultPrint}`)
211
- }
212
- }
213
- }
214
-
215
- /**
216
- * Runs to have length.
217
- * @param {number} result - Expected length.
218
- * @returns {void} - No return value.
219
- */
220
- toHaveLength(result) {
221
- if (typeof result !== "number") {
222
- throw new Error(`Expected length number but got ${typeof result}`)
223
- }
224
-
225
- if (this._object === null || this._object === undefined || typeof this._object.length !== "number") {
226
- throw new Error(`Expected value with length but got ${typeof this._object}`)
227
- }
228
-
229
- const objectPrint = formatValue(this._object)
230
- const resultPrint = formatValue(result)
231
- const lengthValue = this._object.length
232
-
233
- if (this._not) {
234
- if (lengthValue === result) {
235
- throw new Error(`${objectPrint} was unexpected to have length ${resultPrint}`)
236
- }
237
- } else if (lengthValue !== result) {
238
- throw new Error(`${objectPrint} wasn't expected to have length ${resultPrint}`)
239
- }
240
- }
241
-
242
- /**
243
- * Runs to be defined.
244
- * @returns {void} - No return value.
245
- */
246
- toBeDefined() {
247
- if (this._not) {
248
- if (this._object !== undefined) {
249
- const objectPrint = formatValue(this._object)
250
-
251
- throw new Error(`${objectPrint} wasn´t expected to be defined`)
252
- }
253
- } else {
254
- if (this._object === undefined) {
255
- const objectPrint = formatValue(this._object)
256
-
257
- throw new Error(`${objectPrint} wasn't expected be undefined`)
258
- }
259
- }
260
- }
261
-
262
- /**
263
- * Runs to be instance of.
264
- * @param {new (...args: Array<ReturnType<typeof JSON.parse>>) => ReturnType<typeof JSON.parse>} klass - Class constructor to check against (e.g. a built-in like Error).
265
- * @returns {void} - No return value.
266
- */
267
- toBeInstanceOf(klass) {
268
- if (!(this._object instanceof klass)) {
269
- const objectPrint = formatValue(this._object)
270
-
271
- throw new Error(`Expected ${objectPrint} to be a ${klass.name} but it wasn't`)
272
- }
273
- }
274
-
275
- /**
276
- * Runs to be false.
277
- * @returns {void} - No return value.
278
- */
279
- toBeFalse() {
280
- this.toBe(false)
281
- }
282
-
283
- /**
284
- * Runs to be null.
285
- * @returns {void} - No return value.
286
- */
287
- toBeNull() {
288
- this.toBe(null)
289
- }
290
-
291
- /**
292
- * Runs to be undefined.
293
- * @returns {void} - No return value.
294
- */
295
- toBeUndefined() {
296
- this.toBe(undefined)
297
- }
298
-
299
- /**
300
- * Runs to be true.
301
- * @returns {void} - No return value.
302
- */
303
- toBeTrue() {
304
- this.toBe(true)
305
- }
306
-
307
- /**
308
- * Runs to be truthy.
309
- * @returns {void} - No return value.
310
- */
311
- toBeTruthy() {
312
- const objectPrint = formatValue(this._object)
313
-
314
- if (this._not) {
315
- if (this._object) {
316
- throw new Error(`${objectPrint} was unexpected to be truthy`)
317
- }
318
- } else {
319
- if (!this._object) {
320
- throw new Error(`${objectPrint} wasn't expected to be truthy`)
321
- }
322
- }
323
- }
324
-
325
- /**
326
- * Runs to change.
327
- * @param {() => Promise<number>} changeCallback - Change callback.
328
- * @returns {ExpectToChange} - The change.
329
- */
330
- toChange(changeCallback) {
331
- if (this._not) throw new Error("not stub")
332
-
333
- const expectToChange = new ExpectToChange({changeCallback, expect: this})
334
-
335
- this.expectations.push(expectToChange)
336
-
337
- return expectToChange
338
- }
339
-
340
- /**
341
- * Runs to contain.
342
- * @param {ReturnType<typeof JSON.parse>} valueToContain - Value to contain.
343
- * @returns {void} - No return value.
344
- */
345
- toContain(valueToContain) {
346
- if (typeof this._object == "string") {
347
- const matches = this._object.includes(String(valueToContain))
348
- const objectPrint = minifiedStringify(this._object)
349
- const valuePrint = typeof valueToContain == "string"
350
- ? minifiedStringify(valueToContain)
351
- : formatValue(valueToContain)
352
-
353
- if (this._not) {
354
- if (matches) {
355
- throw new Error(`${objectPrint} was unexpected to contain ${valuePrint}`)
356
- }
357
- } else if (!matches) {
358
- throw new Error(`${objectPrint} doesn't contain ${valuePrint}`)
359
- }
360
-
361
- return
362
- }
363
-
364
- if (!Array.isArray(this._object)) {
365
- throw new Error(`Expected array or string but got ${typeof this._object}`)
366
- }
367
-
368
- const matches = this._object.includes(valueToContain)
369
- const objectPrint = formatValue(this._object)
370
- const valuePrint = typeof valueToContain == "string"
371
- ? minifiedStringify(valueToContain)
372
- : formatValue(valueToContain)
373
-
374
- if (this._not) {
375
- if (matches) {
376
- throw new Error(`${objectPrint} was unexpected to contain ${valuePrint}`)
377
- }
378
- } else if (!matches) {
379
- throw new Error(`${objectPrint} doesn't contain ${valuePrint}`)
380
- }
381
- }
382
-
383
- /**
384
- * Runs to contain equal.
385
- * @param {ReturnType<typeof JSON.parse>} valueToContain - Value to contain.
386
- * @returns {void} - No return value.
387
- */
388
- toContainEqual(valueToContain) {
389
- if (!Array.isArray(this._object)) {
390
- throw new Error(`Expected array but got ${typeof this._object}`)
391
- }
392
-
393
- const matches = this._object.some((item) => !anythingDifferent(item, valueToContain))
394
- const objectPrint = formatValue(this._object)
395
- const valuePrint = typeof valueToContain == "string"
396
- ? minifiedStringify(valueToContain)
397
- : formatValue(valueToContain)
398
-
399
- if (this._not) {
400
- if (matches) {
401
- throw new Error(`${objectPrint} was unexpected to contain ${valuePrint}`)
402
- }
403
- } else if (!matches) {
404
- throw new Error(`${objectPrint} doesn't contain ${valuePrint}`)
405
- }
406
- }
407
-
408
- /**
409
- * Runs to include.
410
- * @param {ReturnType<typeof JSON.parse>} valueToInclude - Value to include.
411
- * @returns {void} - No return value.
412
- */
413
- toInclude(valueToInclude) {
414
- this.toContain(valueToInclude)
415
- }
416
-
417
- /**
418
- * Runs to equal.
419
- * @param {ReturnType<typeof JSON.parse>} result - Result.
420
- * @returns {void} - No return value.
421
- */
422
- toEqual(result) {
423
- if (this._object instanceof Set && result instanceof Set) {
424
- const objectPrint = formatValue(this._object)
425
- const resultPrint = formatValue(result)
426
- const actualItems = Array.from(this._object)
427
- const expectedItems = Array.from(result)
428
- const missingItems = expectedItems.filter((expectedItem) => {
429
- return !actualItems.some((actualItem) => !anythingDifferent(actualItem, expectedItem))
430
- })
431
- const unexpectedItems = actualItems.filter((actualItem) => {
432
- return !expectedItems.some((expectedItem) => !anythingDifferent(actualItem, expectedItem))
433
- })
434
- const isEqual = missingItems.length === 0 && unexpectedItems.length === 0
435
-
436
- if (this._not) {
437
- if (isEqual) {
438
- throw new Error(`${objectPrint} was unexpected equal to ${resultPrint}`)
439
- }
440
- } else if (!isEqual) {
441
- const missingStrings = missingItems.map((item) => minifiedStringify(item))
442
- const unexpectedStrings = unexpectedItems.map((item) => minifiedStringify(item))
443
- const diffParts = []
444
-
445
- if (missingStrings.length > 0) diffParts.push(`missing ${missingStrings.join(", ")}`)
446
- if (unexpectedStrings.length > 0) diffParts.push(`unexpected ${unexpectedStrings.join(", ")}`)
447
-
448
- const diffMessage = diffParts.length > 0 ? ` (diff: ${diffParts.join("; ")})` : ""
449
-
450
- throw new Error(`${objectPrint} wasn't equal to ${resultPrint}${diffMessage}`)
451
- }
452
-
453
- return
454
- }
455
-
456
- if (isObjectContaining(result)) {
457
- const expectedValue = /** @type {ReturnType<typeof JSON.parse>} */ (result).value
458
- const {matches, differences} = matchObject(this._object, expectedValue)
459
- const objectPrint = formatValue(this._object)
460
- const expectedPrint = formatValue(expectedValue)
461
-
462
- if (this._not) {
463
- if (matches) {
464
- throw new Error(`Expected ${objectPrint} not to match ${expectedPrint}`)
465
- }
466
- } else if (!matches) {
467
- const diffPrint = Object.keys(differences).length > 0 ? ` (diff: ${minifiedStringify(differences)})` : ""
468
-
469
- throw new Error(`Expected ${objectPrint} to match ${expectedPrint}${diffPrint}`)
470
- }
471
-
472
- return
473
- }
474
-
475
- if (isArrayContaining(result)) {
476
- const expectedValue = /** @type {Array<ReturnType<typeof JSON.parse>>} */ (/** @type {ReturnType<typeof JSON.parse>} */ (result).value)
477
- const {matches, differences} = matchArrayContaining(this._object, expectedValue)
478
- const objectPrint = formatValue(this._object)
479
- const expectedPrint = formatValue(expectedValue)
480
-
481
- if (this._not) {
482
- if (matches) {
483
- throw new Error(`Expected ${objectPrint} not to match ${expectedPrint}`)
484
- }
485
- } else if (!matches) {
486
- const diffPrint = Object.keys(differences).length > 0 ? ` (diff: ${minifiedStringify(differences)})` : ""
487
-
488
- throw new Error(`Expected ${objectPrint} to match ${expectedPrint}${diffPrint}`)
489
- }
490
-
491
- return
492
- }
493
-
494
- if (this._not) {
495
- if (typeof this._object == "object" && typeof result == "object") {
496
- if (!anythingDifferent(this._object, result)) {
497
- const objectPrint = formatValue(this._object)
498
- const resultPrint = formatValue(result)
499
-
500
- throw new Error(`${objectPrint} was unexpected equal to ${resultPrint}`)
501
- }
502
- } else {
503
- if (this._object == result) {
504
- const objectPrint = formatValue(this._object)
505
- const resultPrint = formatValue(result)
506
-
507
- throw new Error(`${objectPrint} was unexpected equal to ${resultPrint}`)
508
- }
509
- }
510
- } else {
511
- if (typeof this._object == "object" && typeof result == "object") {
512
- if (anythingDifferent(this._object, result)) {
513
- const objectPrint = formatValue(this._object)
514
- const resultPrint = formatValue(result)
515
-
516
- if (Array.isArray(this._object) && Array.isArray(result)) {
517
- const actualStrings = this._object.map((item) => minifiedStringify(item))
518
- const expectedStrings = result.map((item) => minifiedStringify(item))
519
-
520
- const missingItems = expectedStrings.filter((item) => !actualStrings.includes(item))
521
- const unexpectedItems = actualStrings.filter((item) => !expectedStrings.includes(item))
522
-
523
- const diffParts = []
524
-
525
- if (missingItems.length > 0) diffParts.push(`missing ${missingItems.join(", ")}`)
526
- if (unexpectedItems.length > 0) diffParts.push(`unexpected ${unexpectedItems.join(", ")}`)
527
-
528
- const diffMessage = diffParts.length > 0 ? ` (diff: ${diffParts.join("; ")})` : ""
529
-
530
- throw new Error(`${objectPrint} wasn't equal to ${resultPrint}${diffMessage}`)
531
- }
532
-
533
- throw new Error(`${objectPrint} wasn't equal to ${resultPrint}`)
534
- }
535
- } else {
536
- if (this._object != result) {
537
- const objectPrint = formatValue(this._object)
538
- const resultPrint = formatValue(result)
539
-
540
- throw new Error(`${objectPrint} wasn't equal to ${resultPrint}`)
541
- }
542
- }
543
- }
544
- }
545
-
546
- /**
547
- * Runs to match.
548
- * @param {RegExp} regex - Regex.
549
- * @returns {void} - No return value.
550
- */
551
- toMatch(regex) {
552
- if (typeof this._object !== "string") {
553
- throw new Error(`Expected string but got ${typeof this._object}`)
554
- }
555
-
556
- const match = this._object.match(regex)
557
- const objectPrint = minifiedStringify(this._object)
558
-
559
- if (this._not) {
560
- if (match) {
561
- throw new Error(`${objectPrint} shouldn't match ${regex}`)
562
- }
563
- } else {
564
- if (!match) {
565
- throw new Error(`${objectPrint} didn't match ${regex}`)
566
- }
567
- }
568
- }
569
-
570
- /**
571
- * Runs to match object.
572
- * @param {Record<string, ReturnType<typeof JSON.parse>> | Array<ReturnType<typeof JSON.parse>>} expected - Expected partial object.
573
- * @returns {void} - No return value.
574
- */
575
- toMatchObject(expected) {
576
- if (expected === null || typeof expected !== "object") {
577
- throw new Error(`Expected object but got ${typeof expected}`)
578
- }
579
-
580
- const {matches, differences} = matchObject(this._object, expected)
581
- const objectPrint = formatValue(this._object)
582
- const expectedPrint = formatValue(expected)
583
-
584
- if (this._not) {
585
- if (matches) {
586
- throw new Error(`Expected ${objectPrint} not to match ${expectedPrint}`)
587
- }
588
- } else if (!matches) {
589
- const diffPrint = Object.keys(differences).length > 0 ? ` (diff: ${minifiedStringify(differences)})` : ""
590
-
591
- throw new Error(`Expected ${objectPrint} to match ${expectedPrint}${diffPrint}`)
592
- }
593
- }
594
-
595
- /**
596
- * Runs to throw error.
597
- * @template T extends Error
598
- * @param {string|T} expectedError - Expected error.
599
- * @returns {Promise<void>} - Resolves when complete.
600
- */
601
- async toThrowError(expectedError) {
602
- if (this._not) throw new Error("not stub")
603
-
604
- let failedError
605
-
606
- try {
607
- if (typeof this._object !== "function") {
608
- throw new Error(`Expected function but got ${typeof this._object}`)
609
- }
610
-
611
- await this._object()
612
- } catch (error) {
613
- failedError = error
614
- }
615
-
616
- if (!failedError) throw new Error("Expected to fail but didn't")
617
-
618
- let expectedErrorMessage, failedErrorMessage
619
-
620
- if (typeof failedError == "string") {
621
- failedErrorMessage = failedError
622
- } else if (failedError instanceof Error) {
623
- failedErrorMessage = failedError.message
624
- } else {
625
- failedErrorMessage = String(failedError)
626
- }
627
-
628
- if (typeof expectedError == "string") {
629
- expectedErrorMessage = expectedError
630
- } else if (expectedError instanceof Error) {
631
- expectedErrorMessage = expectedError.message
632
- } else {
633
- expectedErrorMessage = String(expectedError)
634
- }
635
-
636
- if (failedErrorMessage != expectedErrorMessage) {
637
- throw new Error(`Expected to fail with '${expectedErrorMessage}' but failed with '${failedErrorMessage}'`)
638
- }
639
- }
640
-
641
- /**
642
- * Runs to throw.
643
- * @param {string|RegExp|Error|((new (...args: Array<ReturnType<typeof JSON.parse>>) => Error))} [expected] - Expected error.
644
- * @returns {Promise<void>} - Resolves when complete.
645
- */
646
- async toThrow(expected) {
647
- if (typeof this._object !== "function") {
648
- throw new Error(`Expected function but got ${typeof this._object}`)
649
- }
650
-
651
- let failedError
652
-
653
- try {
654
- await this._object()
655
- } catch (error) {
656
- failedError = error
657
- }
658
-
659
- const objectPrint = formatValue(this._object)
660
-
661
- if (this._not) {
662
- if (failedError) {
663
- throw new Error(`${objectPrint} was unexpected to throw`)
664
- }
665
-
666
- return
667
- }
668
-
669
- if (!failedError) throw new Error("Expected to fail but didn't")
670
- if (expected === undefined) return
671
-
672
- const failedErrorMessage = failedError instanceof Error ? failedError.message : String(failedError)
673
- const failedErrorName = failedError instanceof Error ? failedError.name : typeof failedError
674
-
675
- if (expected instanceof RegExp) {
676
- if (!expected.test(failedErrorMessage)) {
677
- throw new Error(`Expected to fail with message matching ${expected} but failed with '${failedErrorMessage}'`)
678
- }
679
-
680
- return
681
- }
682
-
683
- if (typeof expected === "function" && (expected.prototype instanceof Error || expected === Error)) {
684
- if (!(failedError instanceof expected)) {
685
- throw new Error(`Expected to throw ${expected.name} but threw ${failedErrorName}`)
686
- }
687
-
688
- return
689
- }
690
-
691
- let expectedMessage
692
-
693
- if (typeof expected === "string") {
694
- expectedMessage = expected
695
- } else if (expected instanceof Error) {
696
- expectedMessage = expected.message
697
- } else {
698
- expectedMessage = String(expected)
699
- }
700
-
701
- if (failedErrorMessage != expectedMessage) {
702
- throw new Error(`Expected to fail with '${expectedMessage}' but failed with '${failedErrorMessage}'`)
703
- }
704
- }
705
-
706
- /**
707
- * Runs execute.
708
- * @returns {Promise<ReturnType<typeof JSON.parse>>} - Resolves with the execute.
709
- */
710
- async execute() {
711
- for (const expectation of this.expectations) {
712
- await expectation.runBefore()
713
- }
714
-
715
- if (typeof this._object !== "function") {
716
- throw new Error(`Expected function but got ${typeof this._object}`)
717
- }
718
-
719
- const result = await this._object()
720
-
721
- for (const expectation of this.expectations) {
722
- await expectation.runAfter()
723
- }
724
-
725
- for (const expectation of this.expectations) {
726
- await expectation.execute()
727
- }
728
-
729
- return result
730
- }
731
-
732
- /**
733
- * Runs to have attributes.
734
- * @param {Record<string, ReturnType<typeof JSON.parse>>} result - Result.
735
- * @returns {void} - No return value.
736
- */
737
- toHaveAttributes(result) {
738
- if (this._not) throw new Error("not stub")
739
-
740
- /**
741
- * Differences.
742
- * @type {Record<string, Array<ReturnType<typeof JSON.parse>>>} */
743
- const differences = {}
744
- const objectAsRecord = /** @type {Record<string, ReturnType<typeof JSON.parse>>} */ (this._object)
745
-
746
- for (const key in result) {
747
- const value = result[key]
748
-
749
- if (!(key in objectAsRecord)) throw new Error(`${this._object.constructor.name} doesn't respond to ${key}`)
750
-
751
- const objectValue = /** @type {() => ReturnType<typeof JSON.parse>} */ (objectAsRecord[key])()
752
-
753
- if (value != objectValue) {
754
- differences[key] = [value, objectValue]
755
- }
756
- }
757
-
758
- if (Object.keys(differences).length > 0) {
759
- throw new Error(`Object had differet values: ${minifiedStringify(differences)}`)
760
- }
761
- }
762
- }
3
+ export {Expect as default} from "@velocious/testing"