@augment-vir/assert 30.0.0

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.
Files changed (99) hide show
  1. package/LICENSE-CC0 +121 -0
  2. package/LICENSE-MIT +21 -0
  3. package/dist/assertions/boolean.d.ts +97 -0
  4. package/dist/assertions/boolean.js +47 -0
  5. package/dist/assertions/boundary.d.ts +83 -0
  6. package/dist/assertions/boundary.js +104 -0
  7. package/dist/assertions/enum.d.ts +38 -0
  8. package/dist/assertions/enum.js +42 -0
  9. package/dist/assertions/equality/entry-equality.d.ts +40 -0
  10. package/dist/assertions/equality/entry-equality.js +56 -0
  11. package/dist/assertions/equality/json-equality.d.ts +42 -0
  12. package/dist/assertions/equality/json-equality.js +75 -0
  13. package/dist/assertions/equality/simple-equality.d.ts +85 -0
  14. package/dist/assertions/equality/simple-equality.js +61 -0
  15. package/dist/assertions/equality/ts-type-equality.d.ts +42 -0
  16. package/dist/assertions/equality/ts-type-equality.js +18 -0
  17. package/dist/assertions/extendable-assertions.d.ts +762 -0
  18. package/dist/assertions/extendable-assertions.js +108 -0
  19. package/dist/assertions/http.d.ts +36 -0
  20. package/dist/assertions/http.js +41 -0
  21. package/dist/assertions/instance.d.ts +43 -0
  22. package/dist/assertions/instance.js +41 -0
  23. package/dist/assertions/keys.d.ts +90 -0
  24. package/dist/assertions/keys.js +111 -0
  25. package/dist/assertions/length.d.ts +56 -0
  26. package/dist/assertions/length.js +68 -0
  27. package/dist/assertions/nullish.d.ts +43 -0
  28. package/dist/assertions/nullish.js +42 -0
  29. package/dist/assertions/numeric.d.ts +69 -0
  30. package/dist/assertions/numeric.js +60 -0
  31. package/dist/assertions/output.d.ts +83 -0
  32. package/dist/assertions/output.example.d.ts +1 -0
  33. package/dist/assertions/output.example.js +13 -0
  34. package/dist/assertions/output.js +173 -0
  35. package/dist/assertions/primitive.d.ts +58 -0
  36. package/dist/assertions/primitive.js +62 -0
  37. package/dist/assertions/promise.d.ts +65 -0
  38. package/dist/assertions/promise.js +69 -0
  39. package/dist/assertions/regexp.d.ts +19 -0
  40. package/dist/assertions/regexp.js +18 -0
  41. package/dist/assertions/runtime-type.d.ts +274 -0
  42. package/dist/assertions/runtime-type.example.d.ts +1 -0
  43. package/dist/assertions/runtime-type.example.js +3 -0
  44. package/dist/assertions/runtime-type.js +242 -0
  45. package/dist/assertions/throws.d.ts +109 -0
  46. package/dist/assertions/throws.example.d.ts +1 -0
  47. package/dist/assertions/throws.example.js +11 -0
  48. package/dist/assertions/throws.js +166 -0
  49. package/dist/assertions/uuid.d.ts +35 -0
  50. package/dist/assertions/uuid.js +33 -0
  51. package/dist/assertions/values.d.ts +106 -0
  52. package/dist/assertions/values.js +113 -0
  53. package/dist/augments/assertion-exports.d.ts +5 -0
  54. package/dist/augments/assertion-exports.js +1 -0
  55. package/dist/augments/assertion.error.d.ts +23 -0
  56. package/dist/augments/assertion.error.example.d.ts +1 -0
  57. package/dist/augments/assertion.error.example.js +3 -0
  58. package/dist/augments/assertion.error.js +26 -0
  59. package/dist/augments/guards/assert-wrap.d.ts +244 -0
  60. package/dist/augments/guards/assert-wrap.example.d.ts +1 -0
  61. package/dist/augments/guards/assert-wrap.example.js +9 -0
  62. package/dist/augments/guards/assert-wrap.js +32 -0
  63. package/dist/augments/guards/assert.d.ts +177 -0
  64. package/dist/augments/guards/assert.example.d.ts +1 -0
  65. package/dist/augments/guards/assert.example.js +4 -0
  66. package/dist/augments/guards/assert.js +32 -0
  67. package/dist/augments/guards/check-wrap.d.ts +247 -0
  68. package/dist/augments/guards/check-wrap.example.d.ts +1 -0
  69. package/dist/augments/guards/check-wrap.example.js +9 -0
  70. package/dist/augments/guards/check-wrap.js +34 -0
  71. package/dist/augments/guards/check.d.ts +229 -0
  72. package/dist/augments/guards/check.example.d.ts +1 -0
  73. package/dist/augments/guards/check.example.js +5 -0
  74. package/dist/augments/guards/check.js +21 -0
  75. package/dist/augments/guards/wait-until.d.ts +262 -0
  76. package/dist/augments/guards/wait-until.example.d.ts +1 -0
  77. package/dist/augments/guards/wait-until.example.js +13 -0
  78. package/dist/augments/guards/wait-until.js +34 -0
  79. package/dist/augments/if-equals.d.ts +17 -0
  80. package/dist/augments/if-equals.js +1 -0
  81. package/dist/guard-types/assert-function.d.ts +2 -0
  82. package/dist/guard-types/assert-function.js +3 -0
  83. package/dist/guard-types/assert-wrap-function.d.ts +9 -0
  84. package/dist/guard-types/assert-wrap-function.js +14 -0
  85. package/dist/guard-types/check-function.d.ts +11 -0
  86. package/dist/guard-types/check-function.js +22 -0
  87. package/dist/guard-types/check-wrap-wrapper-function.d.ts +9 -0
  88. package/dist/guard-types/check-wrap-wrapper-function.js +19 -0
  89. package/dist/guard-types/guard-group.d.ts +13 -0
  90. package/dist/guard-types/guard-group.js +3 -0
  91. package/dist/guard-types/guard-override.d.ts +4 -0
  92. package/dist/guard-types/guard-override.js +10 -0
  93. package/dist/guard-types/wait-until-function.d.ts +63 -0
  94. package/dist/guard-types/wait-until-function.js +95 -0
  95. package/dist/index.d.ts +9 -0
  96. package/dist/index.js +8 -0
  97. package/dist/test-timeout.mock.d.ts +3 -0
  98. package/dist/test-timeout.mock.js +8 -0
  99. package/package.json +54 -0
@@ -0,0 +1,42 @@
1
+ import { stringify } from '@augment-vir/core';
2
+ import { AssertionError } from '../augments/assertion.error.js';
3
+ import { autoGuard } from '../guard-types/guard-override.js';
4
+ function isDefined(
5
+ /** The value to check. */
6
+ input,
7
+ /** Message to include in error message if this assertion fails. */
8
+ failureMessage) {
9
+ if (input == undefined) {
10
+ throw new AssertionError(`'${stringify(input)}' is not defined.`, failureMessage);
11
+ }
12
+ }
13
+ function isNullish(
14
+ /** The value to check. */
15
+ input,
16
+ /** Message to include in error message if this assertion fails. */
17
+ failureMessage) {
18
+ if (input != undefined) {
19
+ throw new AssertionError(`'${stringify(input)}' is not a nullish.`, failureMessage);
20
+ }
21
+ }
22
+ const assertions = {
23
+ isDefined,
24
+ isNullish,
25
+ };
26
+ export const nullishGuards = {
27
+ assertions: assertions,
28
+ checkOverrides: {
29
+ isDefined: autoGuard(),
30
+ },
31
+ assertWrapOverrides: {
32
+ isDefined: autoGuard(),
33
+ },
34
+ /** Nullish checks don't make any sense on `checkWrap`. */
35
+ checkWrapOverrides: {
36
+ isDefined: undefined,
37
+ isNullish: undefined,
38
+ },
39
+ waitUntilOverrides: {
40
+ isDefined: autoGuard(),
41
+ },
42
+ };
@@ -0,0 +1,69 @@
1
+ declare function isAbove(actual: number, expected: number, failureMessage?: string | undefined): void;
2
+ declare function isAtLeast(actual: number, expected: number, failureMessage?: string | undefined): void;
3
+ declare function isBelow(actual: number, expected: number, failureMessage?: string | undefined): void;
4
+ declare function isAtMost(actual: number, expected: number, failureMessage?: string | undefined): void;
5
+ declare function isNaNGuard(actual: number, failureMessage?: string | undefined): void;
6
+ declare function isFiniteGuard(actual: number, failureMessage?: string | undefined): void;
7
+ declare function isInfinite(actual: number, failureMessage?: string | undefined): void;
8
+ declare function isApproximately(actual: number, expected: number, delta: number, failureMessage?: string | undefined): void;
9
+ declare function isNotApproximately(actual: number, expected: number, delta: number, failureMessage?: string | undefined): void;
10
+ export declare const numericGuards: {
11
+ assertions: {
12
+ /**
13
+ * Check if a number is above the expectation (`actual > expected`).
14
+ *
15
+ * Performs no type guarding.
16
+ */
17
+ isAbove: typeof isAbove;
18
+ /**
19
+ * Check if a number is at least the expectation (`actual >= expected`).
20
+ *
21
+ * Performs no type guarding.
22
+ */
23
+ isAtLeast: typeof isAtLeast;
24
+ /**
25
+ * Check if a number is below the expectation (`actual < expected`).
26
+ *
27
+ * Performs no type guarding.
28
+ */
29
+ isBelow: typeof isBelow;
30
+ /**
31
+ * Check if a number is at most the expectation (`actual <= expected`).
32
+ *
33
+ * Performs no type guarding.
34
+ */
35
+ isAtMost: typeof isAtMost;
36
+ /**
37
+ * Check if a number is
38
+ * [`NaN`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN).
39
+ *
40
+ * Performs no type guarding.
41
+ */
42
+ isNaN: typeof isNaNGuard;
43
+ /**
44
+ * Check if a number is finite: meaning, not `NaN` and not `Infinity` or `-Infinity`.
45
+ *
46
+ * Performs no type guarding.
47
+ */
48
+ isFinite: typeof isFiniteGuard;
49
+ /**
50
+ * Check if a number is either `Infinity` or `-Infinity`.
51
+ *
52
+ * Performs no type guarding.
53
+ */
54
+ isInfinite: typeof isInfinite;
55
+ /**
56
+ * Check if a number is with ±`delta` of the expectation.
57
+ *
58
+ * Performs no type guarding.
59
+ */
60
+ isApproximately: typeof isApproximately;
61
+ /**
62
+ * Check if a number is outside ±`delta` of the expectation.
63
+ *
64
+ * Performs no type guarding.
65
+ */
66
+ isNotApproximately: typeof isNotApproximately;
67
+ };
68
+ };
69
+ export {};
@@ -0,0 +1,60 @@
1
+ import { AssertionError } from '../augments/assertion.error.js';
2
+ function isAbove(actual, expected, failureMessage) {
3
+ if (actual <= expected) {
4
+ throw new AssertionError(`${actual} is not above ${expected}`, failureMessage);
5
+ }
6
+ }
7
+ function isAtLeast(actual, expected, failureMessage) {
8
+ if (actual < expected) {
9
+ throw new AssertionError(`${actual} is not at least ${expected}`, failureMessage);
10
+ }
11
+ }
12
+ function isBelow(actual, expected, failureMessage) {
13
+ if (actual >= expected) {
14
+ throw new AssertionError(`${actual} is not below ${expected}`, failureMessage);
15
+ }
16
+ }
17
+ function isAtMost(actual, expected, failureMessage) {
18
+ if (actual > expected) {
19
+ throw new AssertionError(`${actual} is not at most ${expected}`, failureMessage);
20
+ }
21
+ }
22
+ function isNaNGuard(actual, failureMessage) {
23
+ if (!isNaN(actual)) {
24
+ throw new AssertionError(`${actual} is not NaN`, failureMessage);
25
+ }
26
+ }
27
+ function isFiniteGuard(actual, failureMessage) {
28
+ if (isNaN(actual) || actual === Infinity || actual === -Infinity) {
29
+ throw new AssertionError(`${actual} is not finite`, failureMessage);
30
+ }
31
+ }
32
+ function isInfinite(actual, failureMessage) {
33
+ if (actual !== Infinity && actual !== -Infinity) {
34
+ throw new AssertionError(`${actual} is not infinite`, failureMessage);
35
+ }
36
+ }
37
+ function isApproximately(actual, expected, delta, failureMessage) {
38
+ if (actual < expected - delta || actual > expected + delta) {
39
+ throw new AssertionError(`${actual} is not within ±${delta} of ${expected}`, failureMessage);
40
+ }
41
+ }
42
+ function isNotApproximately(actual, expected, delta, failureMessage) {
43
+ if (actual >= expected - delta && actual <= expected + delta) {
44
+ throw new AssertionError(`${actual} is within ±${delta} of ${expected}`, failureMessage);
45
+ }
46
+ }
47
+ const assertions = {
48
+ isAbove,
49
+ isAtLeast,
50
+ isBelow,
51
+ isAtMost,
52
+ isNaN: isNaNGuard,
53
+ isFinite: isFiniteGuard,
54
+ isInfinite,
55
+ isApproximately,
56
+ isNotApproximately,
57
+ };
58
+ export const numericGuards = {
59
+ assertions,
60
+ };
@@ -0,0 +1,83 @@
1
+ import { AnyFunction } from '@augment-vir/core';
2
+ import type { IsAny } from 'type-fest';
3
+ import { WaitUntilOptions } from '../guard-types/wait-until-function.js';
4
+ /**
5
+ * A customer asserter for `.output` guards (`assert.output`, `check.output`, etc.). This is
6
+ * typically not necessary, as the `.output` guards already perform deep equality checks by
7
+ * default.
8
+ *
9
+ * @category Assert : Util
10
+ * @example
11
+ *
12
+ * ```ts
13
+ * import {assert, AssertionError, CustomOutputAsserter} from '@augment-vir/assert';
14
+ *
15
+ * function myFunctionToTest(name: string) {
16
+ * return `Hello there ${name}`;
17
+ * }
18
+ *
19
+ * const myCustomAsserter: CustomOutputAsserter<typeof myFunctionToTest> = (
20
+ * actual,
21
+ * expected,
22
+ * failureMessage,
23
+ * ) => {
24
+ * // Write your assertion in an `if`.
25
+ * if (!actual.startsWith('hello there') || actual.endsWith(expected)) {
26
+ * // Throw an `AssertionError` if the `if` fails.
27
+ * throw new AssertionError('', failureMessage);
28
+ * }
29
+ * };
30
+ * // Use your custom asserter as the first input to any `.output` guard.
31
+ * assert.output(myCustomAsserter, myFunctionToTest, ['John'], 'John', 'Name insertion failed');
32
+ * ```
33
+ *
34
+ * @param FunctionToCall The function type that your custom asserter will be run on.
35
+ * @package @augment-vir/assert
36
+ */
37
+ export type CustomOutputAsserter<FunctionToCall extends AnyFunction> = (actual: Awaited<ReturnType<FunctionToCall>>, expected: Awaited<ReturnType<FunctionToCall>>, failureMessage?: string | undefined) => void;
38
+ type OutputReturn<FunctionToCall extends AnyFunction, Return> = Promise<any> extends ReturnType<NoInfer<FunctionToCall>> ? IsAny<ReturnType<FunctionToCall>> extends true ? Return : Promise<Return> : Return;
39
+ export type OutputAssertWithoutAsserter = <const FunctionToCall extends AnyFunction>(functionToCall: FunctionToCall, inputs: Parameters<NoInfer<FunctionToCall>>, expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>, failureMessage?: string | undefined) => OutputReturn<NoInfer<FunctionToCall>, void>;
40
+ export type OutputAssertWithAsserter = <const FunctionToCall extends AnyFunction>(asserter: CustomOutputAsserter<NoInfer<FunctionToCall>>, functionToCall: FunctionToCall, inputs: Parameters<NoInfer<FunctionToCall>>, expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>, failureMessage?: string | undefined) => OutputReturn<NoInfer<FunctionToCall>, void>;
41
+ declare function assertOutput<const FunctionToCall extends AnyFunction>(functionToCall: FunctionToCall, inputs: Parameters<NoInfer<FunctionToCall>>, expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>, failureMessage?: string | undefined): OutputReturn<NoInfer<FunctionToCall>, void>;
42
+ declare function assertOutput<const FunctionToCall extends AnyFunction>(asserter: CustomOutputAsserter<NoInfer<FunctionToCall>>, functionToCall: FunctionToCall, inputs: Parameters<NoInfer<FunctionToCall>>, expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>, failureMessage?: string | undefined): OutputReturn<NoInfer<FunctionToCall>, void>;
43
+ export type OutputCheckWithoutAsserter = <const FunctionToCall extends AnyFunction>(functionToCall: FunctionToCall, inputs: Parameters<NoInfer<FunctionToCall>>, expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>, failureMessage?: string | undefined) => OutputReturn<NoInfer<FunctionToCall>, boolean>;
44
+ export type OutputCheckWithAsserter = <const FunctionToCall extends AnyFunction>(asserter: CustomOutputAsserter<NoInfer<FunctionToCall>>, functionToCall: FunctionToCall, inputs: Parameters<NoInfer<FunctionToCall>>, expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>, failureMessage?: string | undefined) => OutputReturn<NoInfer<FunctionToCall>, boolean>;
45
+ declare function checkOutput<const FunctionToCall extends AnyFunction>(functionToCall: FunctionToCall, inputs: Parameters<NoInfer<FunctionToCall>>, expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>, failureMessage?: string | undefined): OutputReturn<FunctionToCall, boolean>;
46
+ declare function checkOutput<const FunctionToCall extends AnyFunction>(asserter: CustomOutputAsserter<NoInfer<FunctionToCall>>, functionToCall: FunctionToCall, inputs: Parameters<NoInfer<FunctionToCall>>, expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>, failureMessage?: string | undefined): OutputReturn<FunctionToCall, boolean>;
47
+ export type OutputAssertWrapWithoutAsserter = <const FunctionToCall extends AnyFunction>(functionToCall: FunctionToCall, inputs: Parameters<NoInfer<FunctionToCall>>, expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>, failureMessage?: string | undefined) => OutputReturn<NoInfer<FunctionToCall>, Awaited<ReturnType<NoInfer<FunctionToCall>>>>;
48
+ export type OutputAssertWrapWithAsserter = <const FunctionToCall extends AnyFunction>(asserter: CustomOutputAsserter<NoInfer<FunctionToCall>>, functionToCall: FunctionToCall, inputs: Parameters<NoInfer<FunctionToCall>>, expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>, failureMessage?: string | undefined) => OutputReturn<NoInfer<FunctionToCall>, Awaited<ReturnType<NoInfer<FunctionToCall>>>>;
49
+ declare function assertWrapOutput<const FunctionToCall extends AnyFunction>(functionToCall: FunctionToCall, inputs: Parameters<NoInfer<FunctionToCall>>, expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>, failureMessage?: string | undefined): OutputReturn<FunctionToCall, Awaited<ReturnType<NoInfer<FunctionToCall>>>>;
50
+ declare function assertWrapOutput<const FunctionToCall extends AnyFunction>(asserter: CustomOutputAsserter<NoInfer<FunctionToCall>>, functionToCall: FunctionToCall, inputs: Parameters<NoInfer<FunctionToCall>>, expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>, failureMessage?: string | undefined): OutputReturn<FunctionToCall, Awaited<ReturnType<NoInfer<FunctionToCall>>>>;
51
+ export type OutputCheckWrapWithoutAsserter = <const FunctionToCall extends AnyFunction>(functionToCall: FunctionToCall, inputs: Parameters<NoInfer<FunctionToCall>>, expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>, failureMessage?: string | undefined) => OutputReturn<NoInfer<FunctionToCall>, Awaited<ReturnType<NoInfer<FunctionToCall>>> | undefined>;
52
+ export type OutputCheckWrapWithAsserter = <const FunctionToCall extends AnyFunction>(asserter: CustomOutputAsserter<NoInfer<FunctionToCall>>, functionToCall: FunctionToCall, inputs: Parameters<NoInfer<FunctionToCall>>, expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>, failureMessage?: string | undefined) => OutputReturn<NoInfer<FunctionToCall>, Awaited<ReturnType<NoInfer<FunctionToCall>>> | undefined>;
53
+ declare function checkWrapOutput<const FunctionToCall extends AnyFunction>(functionToCall: FunctionToCall, inputs: Parameters<NoInfer<FunctionToCall>>, expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>, failureMessage?: string | undefined): OutputReturn<FunctionToCall, Awaited<ReturnType<NoInfer<FunctionToCall>>> | undefined>;
54
+ declare function checkWrapOutput<const FunctionToCall extends AnyFunction>(asserter: CustomOutputAsserter<NoInfer<FunctionToCall>>, functionToCall: FunctionToCall, inputs: Parameters<NoInfer<FunctionToCall>>, expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>, failureMessage?: string | undefined): OutputReturn<FunctionToCall, Awaited<ReturnType<NoInfer<FunctionToCall>>> | undefined>;
55
+ export type OutputWaitUntilWithoutAsserter = <const FunctionToCall extends AnyFunction>(functionToCall: FunctionToCall, inputs: Parameters<NoInfer<FunctionToCall>>, expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Awaited<ReturnType<NoInfer<FunctionToCall>>>>;
56
+ export type OutputWaitUntilWithAsserter = <const FunctionToCall extends AnyFunction>(asserter: CustomOutputAsserter<NoInfer<FunctionToCall>>, functionToCall: FunctionToCall, inputs: Parameters<NoInfer<FunctionToCall>>, expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Awaited<ReturnType<NoInfer<FunctionToCall>>>>;
57
+ export declare function waitUntilOutput<const FunctionToCall extends AnyFunction>(functionToCall: FunctionToCall, inputs: Parameters<NoInfer<FunctionToCall>>, expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined): Promise<Awaited<ReturnType<NoInfer<FunctionToCall>>>>;
58
+ export declare function waitUntilOutput<const FunctionToCall extends AnyFunction>(asserter: CustomOutputAsserter<NoInfer<FunctionToCall>>, functionToCall: FunctionToCall, inputs: Parameters<NoInfer<FunctionToCall>>, expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined): Promise<Awaited<ReturnType<NoInfer<FunctionToCall>>>>;
59
+ export declare const outputGuards: {
60
+ assertions: {
61
+ /**
62
+ * Checks that the output of the given function deeply equals expectations. A custom asserter
63
+ * can optionally be provided as the first argument to change the expectation checking from
64
+ * "deeply equals" to whatever you want.
65
+ *
66
+ * Performs no type guarding.
67
+ */
68
+ output: typeof assertOutput;
69
+ };
70
+ checkOverrides: {
71
+ output: typeof checkOutput;
72
+ };
73
+ assertWrapOverrides: {
74
+ output: typeof assertWrapOutput;
75
+ };
76
+ checkWrapOverrides: {
77
+ output: typeof checkWrapOutput;
78
+ };
79
+ waitUntilOverrides: {
80
+ output: typeof waitUntilOutput;
81
+ };
82
+ };
83
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ import { assert, AssertionError } from '@augment-vir/assert';
2
+ function myFunctionToTest(name) {
3
+ return `Hello there ${name}`;
4
+ }
5
+ const myCustomAsserter = (actual, expected, failureMessage) => {
6
+ // Write your assertion in an `if`.
7
+ if (!actual.startsWith('hello there') || actual.endsWith(expected)) {
8
+ // Throw an `AssertionError` if the `if` fails.
9
+ throw new AssertionError('', failureMessage);
10
+ }
11
+ };
12
+ // Use your custom asserter as the first input to any `.output` guard.
13
+ assert.output(myCustomAsserter, myFunctionToTest, ['John'], 'John', 'Name insertion failed');
@@ -0,0 +1,173 @@
1
+ import { combineErrorMessages, ensureError, ensureErrorAndPrependMessage, extractErrorMessage, stringify, wait, } from '@augment-vir/core';
2
+ import { convertDuration, DurationUnit } from '@date-vir/duration';
3
+ import { AssertionError } from '../augments/assertion.error.js';
4
+ import { parseWaitUntilOptions } from '../guard-types/wait-until-function.js';
5
+ import { deepEquals } from './equality/simple-equality.js';
6
+ function assertOutput(functionToCallOrAsserter, inputsOrFunctionToCall, expectedOutputOrInputs, failureMessageOrExpectedOutput, emptyOrFailureMessage) {
7
+ return innerAssertOutput(...extractOutputArgs(functionToCallOrAsserter, inputsOrFunctionToCall, expectedOutputOrInputs, failureMessageOrExpectedOutput, emptyOrFailureMessage), false);
8
+ }
9
+ function extractOutputArgs(functionToCallOrAsserter, inputsOrFunctionToCall, expectedOutputOrInputs, failureMessageOrExpectedOutput, emptyOrFailureMessage) {
10
+ const usingCustomAsserter = Array.isArray(expectedOutputOrInputs);
11
+ const asserter = usingCustomAsserter
12
+ ? functionToCallOrAsserter
13
+ : deepEquals;
14
+ const functionToCall = usingCustomAsserter
15
+ ? inputsOrFunctionToCall
16
+ : functionToCallOrAsserter;
17
+ const inputs = usingCustomAsserter
18
+ ? expectedOutputOrInputs
19
+ : inputsOrFunctionToCall;
20
+ const expectedOutput = usingCustomAsserter
21
+ ? failureMessageOrExpectedOutput
22
+ : expectedOutputOrInputs;
23
+ const failureMessage = usingCustomAsserter
24
+ ? emptyOrFailureMessage
25
+ : failureMessageOrExpectedOutput;
26
+ return [
27
+ asserter,
28
+ functionToCall,
29
+ inputs,
30
+ expectedOutput,
31
+ failureMessage,
32
+ ];
33
+ }
34
+ function innerAssertOutput(asserter, functionToCall, inputs, expectedOutput, failureMessage, shouldReturnResult) {
35
+ const result = functionToCall(...inputs);
36
+ if (result instanceof Promise) {
37
+ return new Promise(async (resolve, reject) => {
38
+ try {
39
+ const awaitedResult = await result;
40
+ asserter(awaitedResult, expectedOutput);
41
+ if (shouldReturnResult) {
42
+ resolve(awaitedResult);
43
+ }
44
+ else {
45
+ resolve();
46
+ }
47
+ }
48
+ catch (error) {
49
+ reject(new AssertionError(`Output from '${functionToCall.name}' did not produce expected output with input: ${stringify(inputs)}: ${extractErrorMessage(error)}`, failureMessage));
50
+ }
51
+ });
52
+ }
53
+ else {
54
+ try {
55
+ asserter(result, expectedOutput);
56
+ if (shouldReturnResult) {
57
+ return result;
58
+ }
59
+ else {
60
+ return;
61
+ }
62
+ }
63
+ catch (error) {
64
+ throw new AssertionError(`Output from '${functionToCall.name}' did not produce expected output with input: ${stringify(inputs)}: ${extractErrorMessage(error)}`, failureMessage);
65
+ }
66
+ }
67
+ }
68
+ function checkOutput(functionToCallOrAsserter, inputsOrFunctionToCall, expectedOutputOrInputs, failureMessageOrExpectedOutput, emptyOrFailureMessage) {
69
+ try {
70
+ const assertionResult = innerAssertOutput(...extractOutputArgs(functionToCallOrAsserter, inputsOrFunctionToCall, expectedOutputOrInputs, failureMessageOrExpectedOutput, emptyOrFailureMessage), false);
71
+ if (assertionResult instanceof Promise) {
72
+ return new Promise(async (resolve) => {
73
+ try {
74
+ await assertionResult;
75
+ resolve(true);
76
+ }
77
+ catch {
78
+ resolve(false);
79
+ }
80
+ });
81
+ }
82
+ else {
83
+ return true;
84
+ }
85
+ }
86
+ catch {
87
+ return false;
88
+ }
89
+ }
90
+ function assertWrapOutput(functionToCallOrAsserter, inputsOrFunctionToCall, expectedOutputOrInputs, failureMessageOrExpectedOutput, emptyOrFailureMessage) {
91
+ return innerAssertOutput(...extractOutputArgs(functionToCallOrAsserter, inputsOrFunctionToCall, expectedOutputOrInputs, failureMessageOrExpectedOutput, emptyOrFailureMessage), true);
92
+ }
93
+ function checkWrapOutput(functionToCallOrAsserter, inputsOrFunctionToCall, expectedOutputOrInputs, failureMessageOrExpectedOutput, emptyOrFailureMessage) {
94
+ try {
95
+ const assertionResult = innerAssertOutput(...extractOutputArgs(functionToCallOrAsserter, inputsOrFunctionToCall, expectedOutputOrInputs, failureMessageOrExpectedOutput, emptyOrFailureMessage), true);
96
+ if (assertionResult instanceof Promise) {
97
+ return new Promise(async (resolve) => {
98
+ try {
99
+ resolve(await assertionResult);
100
+ }
101
+ catch {
102
+ resolve(undefined);
103
+ }
104
+ });
105
+ }
106
+ else {
107
+ return assertionResult;
108
+ }
109
+ }
110
+ catch {
111
+ return undefined;
112
+ }
113
+ }
114
+ const notSetSymbol = Symbol('not set');
115
+ export async function waitUntilOutput(functionToCallOrAsserter, inputsOrFunctionToCall, expectedOutputOrInputs, optionsOrExpectedOutput, emptyOrFailureMessageOrOptions, emptyOrFailureMessage) {
116
+ const usingCustomAsserter = Array.isArray(expectedOutputOrInputs);
117
+ const asserter = usingCustomAsserter
118
+ ? functionToCallOrAsserter
119
+ : deepEquals;
120
+ const functionToCall = usingCustomAsserter
121
+ ? inputsOrFunctionToCall
122
+ : functionToCallOrAsserter;
123
+ const inputs = usingCustomAsserter
124
+ ? expectedOutputOrInputs
125
+ : inputsOrFunctionToCall;
126
+ const expectedOutput = usingCustomAsserter
127
+ ? optionsOrExpectedOutput
128
+ : expectedOutputOrInputs;
129
+ const options = parseWaitUntilOptions((usingCustomAsserter ? emptyOrFailureMessageOrOptions : optionsOrExpectedOutput));
130
+ const failureMessage = usingCustomAsserter
131
+ ? emptyOrFailureMessage
132
+ : emptyOrFailureMessageOrOptions;
133
+ const timeout = convertDuration(options.timeout, DurationUnit.Milliseconds).milliseconds;
134
+ const interval = convertDuration(options.interval, DurationUnit.Milliseconds);
135
+ let lastCallbackOutput = notSetSymbol;
136
+ let lastError = undefined;
137
+ async function checkCondition() {
138
+ try {
139
+ lastCallbackOutput = await innerAssertOutput(asserter, functionToCall, inputs, expectedOutput, undefined, true);
140
+ }
141
+ catch (error) {
142
+ lastCallbackOutput = notSetSymbol;
143
+ lastError = ensureError(error);
144
+ }
145
+ }
146
+ const startTime = Date.now();
147
+ while (lastCallbackOutput === notSetSymbol) {
148
+ await checkCondition();
149
+ await wait(interval);
150
+ if (Date.now() - startTime >= timeout) {
151
+ throw ensureErrorAndPrependMessage(lastError, combineErrorMessages(failureMessage, `Timeout of '${timeout}' milliseconds exceeded waiting for callback value to match expectations`));
152
+ }
153
+ }
154
+ return lastCallbackOutput;
155
+ }
156
+ const assertions = {
157
+ output: assertOutput,
158
+ };
159
+ export const outputGuards = {
160
+ assertions,
161
+ checkOverrides: {
162
+ output: checkOutput,
163
+ },
164
+ assertWrapOverrides: {
165
+ output: assertWrapOutput,
166
+ },
167
+ checkWrapOverrides: {
168
+ output: checkWrapOutput,
169
+ },
170
+ waitUntilOverrides: {
171
+ output: waitUntilOutput,
172
+ },
173
+ };
@@ -0,0 +1,58 @@
1
+ import type { MaybePromise } from '@augment-vir/core';
2
+ import { Primitive } from 'type-fest';
3
+ import { WaitUntilOptions } from '../guard-types/wait-until-function.js';
4
+ export type { Primitive } from 'type-fest';
5
+ /** Asserts that the given value is a primitive. */
6
+ declare function isPrimitive(input: unknown, failureMessage?: string | undefined): asserts input is Primitive;
7
+ declare function isNotPrimitive<const Actual>(input: Actual, failureMessage?: string | undefined): asserts input is Exclude<Actual, Primitive>;
8
+ /** Asserts that the given value is a PropertyKey ( string | number | symbol). */
9
+ declare function isPropertyKey(input: unknown, failureMessage?: string | undefined): asserts input is PropertyKey;
10
+ declare function isNotPropertyKey<const Actual>(input: Actual, failureMessage?: string | undefined): asserts input is Exclude<Actual, PropertyKey>;
11
+ export declare const primitiveGuards: {
12
+ assertions: {
13
+ /**
14
+ * Check if a value is a valid `PropertyKey`. `PropertyKey` is a built-in TypeScript type which
15
+ * refers to all possible key types for a JavaScript object.
16
+ *
17
+ * Type guards the value.
18
+ */
19
+ isPropertyKey: typeof isPropertyKey;
20
+ /**
21
+ * Check if a value is _not_ a valid `PropertyKey`. `PropertyKey` is a built-in TypeScript type
22
+ * which refers to all possible key types for a JavaScript object.
23
+ *
24
+ * Type guards the value.
25
+ */
26
+ isNotPropertyKey: typeof isNotPropertyKey;
27
+ /**
28
+ * Check if a value is a JavaScript
29
+ * [primitive](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
30
+ *
31
+ * Type guards the value.
32
+ */
33
+ isPrimitive: typeof isPrimitive;
34
+ /**
35
+ * Check if a value is _not_ a JavaScript
36
+ * [primitive](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
37
+ *
38
+ * Type guards the value.
39
+ */
40
+ isNotPrimitive: typeof isNotPrimitive;
41
+ };
42
+ checkOverrides: {
43
+ isNotPrimitive: <const Actual>(input: Actual) => input is Exclude<Actual, Primitive>;
44
+ isNotPropertyKey: <const Actual>(input: Actual) => input is Exclude<Actual, PropertyKey>;
45
+ };
46
+ assertWrapOverrides: {
47
+ isNotPrimitive: <const Actual>(input: Actual, failureMessage?: string | undefined) => Exclude<Actual, Primitive>;
48
+ isNotPropertyKey: <const Actual>(input: Actual, failureMessage?: string | undefined) => Exclude<Actual, PropertyKey>;
49
+ };
50
+ checkWrapOverrides: {
51
+ isNotPrimitive: <const Actual>(input: Actual) => Exclude<Actual, Primitive> | undefined;
52
+ isNotPropertyKey: <const Actual>(input: Actual) => Exclude<Actual, PropertyKey> | undefined;
53
+ };
54
+ waitUntilOverrides: {
55
+ isNotPrimitive: <const Actual>(callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Exclude<Actual, Primitive>>;
56
+ isNotPropertyKey: <const Actual>(callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Exclude<Actual, PropertyKey>>;
57
+ };
58
+ };
@@ -0,0 +1,62 @@
1
+ import { stringify } from '@augment-vir/core';
2
+ import { AssertionError } from '../augments/assertion.error.js';
3
+ import { autoGuard } from '../guard-types/guard-override.js';
4
+ /** Asserts that the given value is a primitive. */
5
+ function isPrimitive(input, failureMessage) {
6
+ /**
7
+ * `null` is a primitive but `typeof null` gives `'object'` so we have to special case `null`
8
+ * here.
9
+ */
10
+ if (input !== null && (typeof input === 'object' || typeof input === 'function')) {
11
+ throw new AssertionError(`'${stringify(input)}' is not a Primitive.`, failureMessage);
12
+ }
13
+ }
14
+ function isNotPrimitive(input, failureMessage) {
15
+ try {
16
+ isPrimitive(input);
17
+ }
18
+ catch {
19
+ return;
20
+ }
21
+ throw new AssertionError(`'${stringify(input)}' is a Primitive.`, failureMessage);
22
+ }
23
+ /** Asserts that the given value is a PropertyKey ( string | number | symbol). */
24
+ function isPropertyKey(input, failureMessage) {
25
+ if (typeof input !== 'string' && typeof input !== 'number' && typeof input !== 'symbol') {
26
+ throw new AssertionError(`'${stringify(input)}' is not a PropertyKey.`, failureMessage);
27
+ }
28
+ }
29
+ function isNotPropertyKey(input, failureMessage) {
30
+ try {
31
+ isPropertyKey(input);
32
+ }
33
+ catch {
34
+ return;
35
+ }
36
+ throw new AssertionError(`'${stringify(input)}' is a PropertyKey.`, failureMessage);
37
+ }
38
+ const assertions = {
39
+ isPropertyKey,
40
+ isNotPropertyKey,
41
+ isPrimitive,
42
+ isNotPrimitive,
43
+ };
44
+ export const primitiveGuards = {
45
+ assertions,
46
+ checkOverrides: {
47
+ isNotPrimitive: autoGuard(),
48
+ isNotPropertyKey: autoGuard(),
49
+ },
50
+ assertWrapOverrides: {
51
+ isNotPrimitive: autoGuard(),
52
+ isNotPropertyKey: autoGuard(),
53
+ },
54
+ checkWrapOverrides: {
55
+ isNotPrimitive: autoGuard(),
56
+ isNotPropertyKey: autoGuard(),
57
+ },
58
+ waitUntilOverrides: {
59
+ isNotPrimitive: autoGuard(),
60
+ isNotPropertyKey: autoGuard(),
61
+ },
62
+ };
@@ -0,0 +1,65 @@
1
+ import { WaitUntilOptions } from '../guard-types/wait-until-function.js';
2
+ declare function isPromiseLike(actual: unknown, failureMessage?: string | undefined): asserts actual is PromiseLike<any>;
3
+ declare function isNotPromiseLike<const Actual>(actual: Actual, failureMessage?: string | undefined): asserts actual is Exclude<Actual, PromiseLike<any>>;
4
+ /**
5
+ * Checks if a value is an actual `Promise` object. In reality this is just a simple wrapper for
6
+ * `instanceof Promise`, but it makes checking a bit more ergonomic.
7
+ */
8
+ declare function isPromise(actual: unknown, failureMessage?: string | undefined): asserts actual is Promise<any>;
9
+ declare function isNotPromise<const Actual>(actual: Actual, failureMessage?: string | undefined): asserts actual is Exclude<Actual, Promise<any>>;
10
+ export declare const promiseGuards: {
11
+ assertions: {
12
+ /**
13
+ * Check if a value is a `PromiseLike`. `PromiseLike` is TypeScript built-in type that simply
14
+ * includes a `.then` method. This enables the use of third-party promise implementations that
15
+ * aren't instances of the built-in `Promise` class.
16
+ *
17
+ * Type guards the value.
18
+ */
19
+ isPromiseLike: typeof isPromiseLike;
20
+ /**
21
+ * Check if a value is _not_ a `PromiseLike`. `PromiseLike` is TypeScript built-in type that
22
+ * simply includes a `.then` method.
23
+ *
24
+ * Type guards the value.
25
+ */
26
+ isNotPromiseLike: typeof isNotPromiseLike;
27
+ /**
28
+ * Check if a value is an instance of the built-in `Promise` class.
29
+ *
30
+ * Type guards the value.
31
+ */
32
+ isPromise: typeof isPromise;
33
+ /**
34
+ * Check if a value is _not_ an instance of the built-in `Promise` class.
35
+ *
36
+ * Type guards the value.
37
+ */
38
+ isNotPromise: typeof isNotPromise;
39
+ };
40
+ checkOverrides: {
41
+ isNotPromise: <const Actual>(actual: Actual, failureMessage?: string | undefined) => actual is Exclude<Actual, Promise<any>>;
42
+ isNotPromiseLike: <const Actual>(actual: Actual, failureMessage?: string | undefined) => actual is Exclude<Actual, PromiseLike<any>>;
43
+ };
44
+ assertWrapOverrides: {
45
+ isPromise: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Extract<Actual, Promise<any>>;
46
+ isNotPromise: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, Promise<any>>;
47
+ isPromiseLike: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Extract<Actual, PromiseLike<any>>;
48
+ isNotPromiseLike: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, PromiseLike<any>>;
49
+ };
50
+ checkWrapOverrides: {
51
+ isNotPromise: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, Promise<any>> | undefined;
52
+ isNotPromiseLike: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, PromiseLike<any>> | undefined;
53
+ };
54
+ /**
55
+ * These overrides must explicitly use `createWaitUntil` so they can pass in `true` for the
56
+ * second parameter, `requireSynchronousResult`.
57
+ */
58
+ waitUntilOverrides: {
59
+ isPromise: <const Actual>(callback: () => Actual, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Extract<Actual, Promise<any>>>;
60
+ isNotPromise: <const Actual>(callback: () => Actual, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Exclude<Actual, Promise<any>>>;
61
+ isPromiseLike: <const Actual>(callback: () => Actual, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Extract<Actual, PromiseLike<any>>>;
62
+ isNotPromiseLike: <const Actual>(callback: () => Actual, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Exclude<Actual, PromiseLike<any>>>;
63
+ };
64
+ };
65
+ export {};