@kerebron/test-utils 0.4.28 → 0.4.29

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 (109) hide show
  1. package/esm/_dnt.shims.js +1 -0
  2. package/esm/_dnt.shims.js.map +1 -0
  3. package/esm/deps/jsr.io/@std/assert/1.0.16/almost_equals.js +1 -0
  4. package/esm/deps/jsr.io/@std/assert/1.0.16/almost_equals.js.map +1 -0
  5. package/esm/deps/jsr.io/@std/assert/1.0.16/array_includes.js +1 -0
  6. package/esm/deps/jsr.io/@std/assert/1.0.16/array_includes.js.map +1 -0
  7. package/esm/deps/jsr.io/@std/assert/1.0.16/assert.js +1 -0
  8. package/esm/deps/jsr.io/@std/assert/1.0.16/assert.js.map +1 -0
  9. package/esm/deps/jsr.io/@std/assert/1.0.16/assertion_error.js +1 -0
  10. package/esm/deps/jsr.io/@std/assert/1.0.16/assertion_error.js.map +1 -0
  11. package/esm/deps/jsr.io/@std/assert/1.0.16/equal.js +1 -0
  12. package/esm/deps/jsr.io/@std/assert/1.0.16/equal.js.map +1 -0
  13. package/esm/deps/jsr.io/@std/assert/1.0.16/equals.js +1 -0
  14. package/esm/deps/jsr.io/@std/assert/1.0.16/equals.js.map +1 -0
  15. package/esm/deps/jsr.io/@std/assert/1.0.16/exists.js +1 -0
  16. package/esm/deps/jsr.io/@std/assert/1.0.16/exists.js.map +1 -0
  17. package/esm/deps/jsr.io/@std/assert/1.0.16/fail.js +1 -0
  18. package/esm/deps/jsr.io/@std/assert/1.0.16/fail.js.map +1 -0
  19. package/esm/deps/jsr.io/@std/assert/1.0.16/false.js +1 -0
  20. package/esm/deps/jsr.io/@std/assert/1.0.16/false.js.map +1 -0
  21. package/esm/deps/jsr.io/@std/assert/1.0.16/greater.js +1 -0
  22. package/esm/deps/jsr.io/@std/assert/1.0.16/greater.js.map +1 -0
  23. package/esm/deps/jsr.io/@std/assert/1.0.16/greater_or_equal.js +1 -0
  24. package/esm/deps/jsr.io/@std/assert/1.0.16/greater_or_equal.js.map +1 -0
  25. package/esm/deps/jsr.io/@std/assert/1.0.16/instance_of.js +1 -0
  26. package/esm/deps/jsr.io/@std/assert/1.0.16/instance_of.js.map +1 -0
  27. package/esm/deps/jsr.io/@std/assert/1.0.16/is_error.js +1 -0
  28. package/esm/deps/jsr.io/@std/assert/1.0.16/is_error.js.map +1 -0
  29. package/esm/deps/jsr.io/@std/assert/1.0.16/less.js +1 -0
  30. package/esm/deps/jsr.io/@std/assert/1.0.16/less.js.map +1 -0
  31. package/esm/deps/jsr.io/@std/assert/1.0.16/less_or_equal.js +1 -0
  32. package/esm/deps/jsr.io/@std/assert/1.0.16/less_or_equal.js.map +1 -0
  33. package/esm/deps/jsr.io/@std/assert/1.0.16/match.js +1 -0
  34. package/esm/deps/jsr.io/@std/assert/1.0.16/match.js.map +1 -0
  35. package/esm/deps/jsr.io/@std/assert/1.0.16/mod.js +1 -0
  36. package/esm/deps/jsr.io/@std/assert/1.0.16/mod.js.map +1 -0
  37. package/esm/deps/jsr.io/@std/assert/1.0.16/not_equals.js +1 -0
  38. package/esm/deps/jsr.io/@std/assert/1.0.16/not_equals.js.map +1 -0
  39. package/esm/deps/jsr.io/@std/assert/1.0.16/not_instance_of.js +1 -0
  40. package/esm/deps/jsr.io/@std/assert/1.0.16/not_instance_of.js.map +1 -0
  41. package/esm/deps/jsr.io/@std/assert/1.0.16/not_match.js +1 -0
  42. package/esm/deps/jsr.io/@std/assert/1.0.16/not_match.js.map +1 -0
  43. package/esm/deps/jsr.io/@std/assert/1.0.16/not_strict_equals.js +1 -0
  44. package/esm/deps/jsr.io/@std/assert/1.0.16/not_strict_equals.js.map +1 -0
  45. package/esm/deps/jsr.io/@std/assert/1.0.16/object_match.js +1 -0
  46. package/esm/deps/jsr.io/@std/assert/1.0.16/object_match.js.map +1 -0
  47. package/esm/deps/jsr.io/@std/assert/1.0.16/rejects.js +1 -0
  48. package/esm/deps/jsr.io/@std/assert/1.0.16/rejects.js.map +1 -0
  49. package/esm/deps/jsr.io/@std/assert/1.0.16/strict_equals.js +1 -0
  50. package/esm/deps/jsr.io/@std/assert/1.0.16/strict_equals.js.map +1 -0
  51. package/esm/deps/jsr.io/@std/assert/1.0.16/string_includes.js +1 -0
  52. package/esm/deps/jsr.io/@std/assert/1.0.16/string_includes.js.map +1 -0
  53. package/esm/deps/jsr.io/@std/assert/1.0.16/throws.js +1 -0
  54. package/esm/deps/jsr.io/@std/assert/1.0.16/throws.js.map +1 -0
  55. package/esm/deps/jsr.io/@std/assert/1.0.16/unimplemented.js +1 -0
  56. package/esm/deps/jsr.io/@std/assert/1.0.16/unimplemented.js.map +1 -0
  57. package/esm/deps/jsr.io/@std/assert/1.0.16/unreachable.js +1 -0
  58. package/esm/deps/jsr.io/@std/assert/1.0.16/unreachable.js.map +1 -0
  59. package/esm/deps/jsr.io/@std/internal/1.0.12/build_message.js +1 -0
  60. package/esm/deps/jsr.io/@std/internal/1.0.12/build_message.js.map +1 -0
  61. package/esm/deps/jsr.io/@std/internal/1.0.12/diff.js +1 -0
  62. package/esm/deps/jsr.io/@std/internal/1.0.12/diff.js.map +1 -0
  63. package/esm/deps/jsr.io/@std/internal/1.0.12/diff_str.js +1 -0
  64. package/esm/deps/jsr.io/@std/internal/1.0.12/diff_str.js.map +1 -0
  65. package/esm/deps/jsr.io/@std/internal/1.0.12/format.js +1 -0
  66. package/esm/deps/jsr.io/@std/internal/1.0.12/format.js.map +1 -0
  67. package/esm/deps/jsr.io/@std/internal/1.0.12/styles.js +1 -0
  68. package/esm/deps/jsr.io/@std/internal/1.0.12/styles.js.map +1 -0
  69. package/esm/deps/jsr.io/@std/internal/1.0.12/types.js +1 -0
  70. package/esm/deps/jsr.io/@std/internal/1.0.12/types.js.map +1 -0
  71. package/esm/test-utils.js +1 -0
  72. package/esm/test-utils.js.map +1 -0
  73. package/package.json +5 -1
  74. package/src/_dnt.shims.ts +60 -0
  75. package/src/deps/jsr.io/@std/assert/1.0.16/almost_equals.ts +55 -0
  76. package/src/deps/jsr.io/@std/assert/1.0.16/array_includes.ts +57 -0
  77. package/src/deps/jsr.io/@std/assert/1.0.16/assert.ts +23 -0
  78. package/src/deps/jsr.io/@std/assert/1.0.16/assertion_error.ts +31 -0
  79. package/src/deps/jsr.io/@std/assert/1.0.16/equal.ts +218 -0
  80. package/src/deps/jsr.io/@std/assert/1.0.16/equals.ts +66 -0
  81. package/src/deps/jsr.io/@std/assert/1.0.16/exists.ts +31 -0
  82. package/src/deps/jsr.io/@std/assert/1.0.16/fail.ts +21 -0
  83. package/src/deps/jsr.io/@std/assert/1.0.16/false.ts +26 -0
  84. package/src/deps/jsr.io/@std/assert/1.0.16/greater.ts +30 -0
  85. package/src/deps/jsr.io/@std/assert/1.0.16/greater_or_equal.ts +36 -0
  86. package/src/deps/jsr.io/@std/assert/1.0.16/instance_of.ts +64 -0
  87. package/src/deps/jsr.io/@std/assert/1.0.16/is_error.ts +65 -0
  88. package/src/deps/jsr.io/@std/assert/1.0.16/less.ts +29 -0
  89. package/src/deps/jsr.io/@std/assert/1.0.16/less_or_equal.ts +36 -0
  90. package/src/deps/jsr.io/@std/assert/1.0.16/match.ts +30 -0
  91. package/src/deps/jsr.io/@std/assert/1.0.16/mod.ts +47 -0
  92. package/src/deps/jsr.io/@std/assert/1.0.16/not_equals.ts +37 -0
  93. package/src/deps/jsr.io/@std/assert/1.0.16/not_instance_of.ts +33 -0
  94. package/src/deps/jsr.io/@std/assert/1.0.16/not_match.ts +30 -0
  95. package/src/deps/jsr.io/@std/assert/1.0.16/not_strict_equals.ts +42 -0
  96. package/src/deps/jsr.io/@std/assert/1.0.16/object_match.ts +214 -0
  97. package/src/deps/jsr.io/@std/assert/1.0.16/rejects.ts +123 -0
  98. package/src/deps/jsr.io/@std/assert/1.0.16/strict_equals.ts +68 -0
  99. package/src/deps/jsr.io/@std/assert/1.0.16/string_includes.ts +30 -0
  100. package/src/deps/jsr.io/@std/assert/1.0.16/throws.ts +111 -0
  101. package/src/deps/jsr.io/@std/assert/1.0.16/unimplemented.ts +21 -0
  102. package/src/deps/jsr.io/@std/assert/1.0.16/unreachable.ts +21 -0
  103. package/src/deps/jsr.io/@std/internal/1.0.12/build_message.ts +147 -0
  104. package/src/deps/jsr.io/@std/internal/1.0.12/diff.ts +317 -0
  105. package/src/deps/jsr.io/@std/internal/1.0.12/diff_str.ts +208 -0
  106. package/src/deps/jsr.io/@std/internal/1.0.12/format.ts +89 -0
  107. package/src/deps/jsr.io/@std/internal/1.0.12/styles.ts +233 -0
  108. package/src/deps/jsr.io/@std/internal/1.0.12/types.ts +30 -0
  109. package/src/test-utils.ts +12 -0
@@ -0,0 +1,29 @@
1
+ // Copyright 2018-2025 the Deno authors. MIT license.
2
+ // This module is browser compatible.
3
+ import { format } from "../../internal/1.0.12/format.js";
4
+ import { AssertionError } from "./assertion_error.js";
5
+
6
+ /**
7
+ * Make an assertion that `actual` is less than `expected`.
8
+ * If not then throw.
9
+ *
10
+ * @example Usage
11
+ * ```ts ignore
12
+ * import { assertLess } from "@std/assert";
13
+ *
14
+ * assertLess(1, 2); // Doesn't throw
15
+ * assertLess(2, 1); // Throws
16
+ * ```
17
+ *
18
+ * @typeParam T The type of the values to compare.
19
+ * @param actual The actual value to compare.
20
+ * @param expected The expected value to compare.
21
+ * @param msg The optional message to display if the assertion fails.
22
+ */
23
+ export function assertLess<T>(actual: T, expected: T, msg?: string) {
24
+ if (actual < expected) return;
25
+
26
+ const actualString = format(actual);
27
+ const expectedString = format(expected);
28
+ throw new AssertionError(msg ?? `Expect ${actualString} < ${expectedString}`);
29
+ }
@@ -0,0 +1,36 @@
1
+ // Copyright 2018-2025 the Deno authors. MIT license.
2
+ // This module is browser compatible.
3
+ import { format } from "../../internal/1.0.12/format.js";
4
+ import { AssertionError } from "./assertion_error.js";
5
+
6
+ /**
7
+ * Make an assertion that `actual` is less than or equal to `expected`.
8
+ * If not then throw.
9
+ *
10
+ * @example Usage
11
+ * ```ts ignore
12
+ * import { assertLessOrEqual } from "@std/assert";
13
+ *
14
+ * assertLessOrEqual(1, 2); // Doesn't throw
15
+ * assertLessOrEqual(1, 1); // Doesn't throw
16
+ * assertLessOrEqual(1, 0); // Throws
17
+ * ```
18
+ *
19
+ * @typeParam T The type of the values to compare.
20
+ * @param actual The actual value to compare.
21
+ * @param expected The expected value to compare.
22
+ * @param msg The optional message to display if the assertion fails.
23
+ */
24
+ export function assertLessOrEqual<T>(
25
+ actual: T,
26
+ expected: T,
27
+ msg?: string,
28
+ ) {
29
+ if (actual <= expected) return;
30
+
31
+ const actualString = format(actual);
32
+ const expectedString = format(expected);
33
+ throw new AssertionError(
34
+ msg ?? `Expect ${actualString} <= ${expectedString}`,
35
+ );
36
+ }
@@ -0,0 +1,30 @@
1
+ // Copyright 2018-2025 the Deno authors. MIT license.
2
+ // This module is browser compatible.
3
+ import { AssertionError } from "./assertion_error.js";
4
+
5
+ /**
6
+ * Make an assertion that `actual` match RegExp `expected`. If not
7
+ * then throw.
8
+ *
9
+ * @example Usage
10
+ * ```ts ignore
11
+ * import { assertMatch } from "@std/assert";
12
+ *
13
+ * assertMatch("Raptor", /Raptor/); // Doesn't throw
14
+ * assertMatch("Denosaurus", /Raptor/); // Throws
15
+ * ```
16
+ *
17
+ * @param actual The actual value to be matched.
18
+ * @param expected The expected pattern to match.
19
+ * @param msg The optional message to display if the assertion fails.
20
+ */
21
+ export function assertMatch(
22
+ actual: string,
23
+ expected: RegExp,
24
+ msg?: string,
25
+ ) {
26
+ if (expected.test(actual)) return;
27
+ const msgSuffix = msg ? `: ${msg}` : ".";
28
+ msg = `Expected actual: "${actual}" to match: "${expected}"${msgSuffix}`;
29
+ throw new AssertionError(msg);
30
+ }
@@ -0,0 +1,47 @@
1
+ // Copyright 2018-2025 the Deno authors. MIT license.
2
+ // This module is browser compatible.
3
+
4
+ /** A library of assertion functions.
5
+ * If the assertion is false an `AssertionError` will be thrown which will
6
+ * result in pretty-printed diff of the failing assertion.
7
+ *
8
+ * This module is browser compatible, but do not rely on good formatting of
9
+ * values for AssertionError messages in browsers.
10
+ *
11
+ * ```ts ignore
12
+ * import { assert } from "@std/assert";
13
+ *
14
+ * assert("I am truthy"); // Doesn't throw
15
+ * assert(false); // Throws `AssertionError`
16
+ * ```
17
+ *
18
+ * @module
19
+ */
20
+
21
+ export * from "./almost_equals.js";
22
+ export * from "./array_includes.js";
23
+ export * from "./equals.js";
24
+ export * from "./exists.js";
25
+ export * from "./false.js";
26
+ export * from "./greater_or_equal.js";
27
+ export * from "./greater.js";
28
+ export * from "./instance_of.js";
29
+ export * from "./is_error.js";
30
+ export * from "./less_or_equal.js";
31
+ export * from "./less.js";
32
+ export * from "./match.js";
33
+ export * from "./not_equals.js";
34
+ export * from "./not_instance_of.js";
35
+ export * from "./not_match.js";
36
+ export * from "./not_strict_equals.js";
37
+ export * from "./object_match.js";
38
+ export * from "./rejects.js";
39
+ export * from "./strict_equals.js";
40
+ export * from "./string_includes.js";
41
+ export * from "./throws.js";
42
+ export * from "./assert.js";
43
+ export * from "./assertion_error.js";
44
+ export * from "./equal.js";
45
+ export * from "./fail.js";
46
+ export * from "./unimplemented.js";
47
+ export * from "./unreachable.js";
@@ -0,0 +1,37 @@
1
+ // Copyright 2018-2025 the Deno authors. MIT license.
2
+ // This module is browser compatible.
3
+
4
+ import { equal } from "./equal.js";
5
+ import { AssertionError } from "./assertion_error.js";
6
+ import { format } from "../../internal/1.0.12/format.js";
7
+
8
+ /**
9
+ * Make an assertion that `actual` and `expected` are not equal, deeply.
10
+ * If not then throw.
11
+ *
12
+ * Type parameter can be specified to ensure values under comparison have the same type.
13
+ *
14
+ * @example Usage
15
+ * ```ts ignore
16
+ * import { assertNotEquals } from "@std/assert";
17
+ *
18
+ * assertNotEquals(1, 2); // Doesn't throw
19
+ * assertNotEquals(1, 1); // Throws
20
+ * ```
21
+ *
22
+ * @typeParam T The type of the values to compare.
23
+ * @param actual The actual value to compare.
24
+ * @param expected The expected value to compare.
25
+ * @param msg The optional message to display if the assertion fails.
26
+ */
27
+ export function assertNotEquals<T>(actual: T, expected: T, msg?: string) {
28
+ if (!equal(actual, expected)) {
29
+ return;
30
+ }
31
+ const actualString = format(actual);
32
+ const expectedString = format(expected);
33
+ const msgSuffix = msg ? `: ${msg}` : ".";
34
+ throw new AssertionError(
35
+ `Expected actual: ${actualString} not to be: ${expectedString}${msgSuffix}`,
36
+ );
37
+ }
@@ -0,0 +1,33 @@
1
+ // Copyright 2018-2025 the Deno authors. MIT license.
2
+ // This module is browser compatible.
3
+ import { assertFalse } from "./false.js";
4
+
5
+ /**
6
+ * Make an assertion that `obj` is not an instance of `type`.
7
+ * If so, then throw.
8
+ *
9
+ * @example Usage
10
+ * ```ts ignore
11
+ * import { assertNotInstanceOf } from "@std/assert";
12
+ *
13
+ * assertNotInstanceOf(new Date(), Number); // Doesn't throw
14
+ * assertNotInstanceOf(new Date(), Date); // Throws
15
+ * ```
16
+ *
17
+ * @typeParam A The type of the object to check.
18
+ * @typeParam T The type of the class to check against.
19
+ * @param actual The object to check.
20
+ * @param unexpectedType The class constructor to check against.
21
+ * @param msg The optional message to display if the assertion fails.
22
+ */
23
+ export function assertNotInstanceOf<A, T>(
24
+ actual: A,
25
+ // deno-lint-ignore no-explicit-any
26
+ unexpectedType: abstract new (...args: any[]) => T,
27
+ msg?: string,
28
+ ): asserts actual is Exclude<A, T> {
29
+ const msgSuffix = msg ? `: ${msg}` : ".";
30
+ msg =
31
+ `Expected object to not be an instance of "${typeof unexpectedType}"${msgSuffix}`;
32
+ assertFalse(actual instanceof unexpectedType, msg);
33
+ }
@@ -0,0 +1,30 @@
1
+ // Copyright 2018-2025 the Deno authors. MIT license.
2
+ // This module is browser compatible.
3
+ import { AssertionError } from "./assertion_error.js";
4
+
5
+ /**
6
+ * Make an assertion that `actual` not match RegExp `expected`. If match
7
+ * then throw.
8
+ *
9
+ * @example Usage
10
+ * ```ts ignore
11
+ * import { assertNotMatch } from "@std/assert";
12
+ *
13
+ * assertNotMatch("Denosaurus", /Raptor/); // Doesn't throw
14
+ * assertNotMatch("Raptor", /Raptor/); // Throws
15
+ * ```
16
+ *
17
+ * @param actual The actual value to match.
18
+ * @param expected The expected value to not match.
19
+ * @param msg The optional message to display if the assertion fails.
20
+ */
21
+ export function assertNotMatch(
22
+ actual: string,
23
+ expected: RegExp,
24
+ msg?: string,
25
+ ) {
26
+ if (!expected.test(actual)) return;
27
+ const msgSuffix = msg ? `: ${msg}` : ".";
28
+ msg = `Expected actual: "${actual}" to not match: "${expected}"${msgSuffix}`;
29
+ throw new AssertionError(msg);
30
+ }
@@ -0,0 +1,42 @@
1
+ // Copyright 2018-2025 the Deno authors. MIT license.
2
+ // This module is browser compatible.
3
+ import { AssertionError } from "./assertion_error.js";
4
+ import { format } from "../../internal/1.0.12/format.js";
5
+
6
+ /**
7
+ * Make an assertion that `actual` and `expected` are not strictly equal, using
8
+ * {@linkcode Object.is} for equality comparison. If the values are strictly
9
+ * equal then throw.
10
+ *
11
+ * @example Usage
12
+ * ```ts ignore
13
+ * import { assertNotStrictEquals } from "@std/assert";
14
+ *
15
+ * assertNotStrictEquals(1, 1); // Throws
16
+ * assertNotStrictEquals(1, 2); // Doesn't throw
17
+ *
18
+ * assertNotStrictEquals(0, 0); // Throws
19
+ * assertNotStrictEquals(0, -0); // Doesn't throw
20
+ * ```
21
+ *
22
+ * @typeParam T The type of the values to compare.
23
+ * @param actual The actual value to compare.
24
+ * @param expected The expected value to compare.
25
+ * @param msg The optional message to display if the assertion fails.
26
+ */
27
+ export function assertNotStrictEquals<T>(
28
+ actual: T,
29
+ expected: T,
30
+ msg?: string,
31
+ ) {
32
+ if (!Object.is(actual, expected)) {
33
+ return;
34
+ }
35
+
36
+ const msgSuffix = msg ? `: ${msg}` : ".";
37
+ throw new AssertionError(
38
+ `Expected "actual" to not be strictly equal to: ${
39
+ format(actual)
40
+ }${msgSuffix}\n`,
41
+ );
42
+ }
@@ -0,0 +1,214 @@
1
+ // Copyright 2018-2025 the Deno authors. MIT license.
2
+ // This module is browser compatible.
3
+ import { assertEquals } from "./equals.js";
4
+
5
+ /**
6
+ * Make an assertion that `expected` object is a subset of `actual` object,
7
+ * deeply. If not, then throw a diff of the objects, with mismatching
8
+ * properties highlighted.
9
+ *
10
+ * @example Usage
11
+ * ```ts ignore
12
+ * import { assertObjectMatch } from "@std/assert";
13
+ *
14
+ * assertObjectMatch({ foo: "bar" }, { foo: "bar" }); // Doesn't throw
15
+ * assertObjectMatch({ foo: "bar" }, { foo: "baz" }); // Throws
16
+ * assertObjectMatch({ foo: 1, bar: 2 }, { foo: 1 }); // Doesn't throw
17
+ * assertObjectMatch({ foo: 1 }, { foo: 1, bar: 2 }); // Throws
18
+ * ```
19
+ *
20
+ * @example Usage with nested objects
21
+ * ```ts ignore
22
+ * import { assertObjectMatch } from "@std/assert";
23
+ *
24
+ * assertObjectMatch({ foo: { bar: 3, baz: 4 } }, { foo: { bar: 3 } }); // Doesn't throw
25
+ * assertObjectMatch({ foo: { bar: 3 } }, { foo: { bar: 3, baz: 4 } }); // Throws
26
+ * ```
27
+ *
28
+ * @param actual The actual value to be matched.
29
+ * @param expected The expected value to match.
30
+ * @param msg The optional message to display if the assertion fails.
31
+ */
32
+ export function assertObjectMatch(
33
+ // deno-lint-ignore no-explicit-any
34
+ actual: Record<PropertyKey, any>,
35
+ expected: Record<PropertyKey, unknown>,
36
+ msg?: string,
37
+ ): void {
38
+ return assertEquals(
39
+ // get the intersection of "actual" and "expected"
40
+ // side effect: all the instances' constructor field is "Object" now.
41
+ filter(actual, expected),
42
+ // set (nested) instances' constructor field to be "Object" without changing expected value.
43
+ // see https://github.com/denoland/std/pull/1419
44
+ filter(expected, expected),
45
+ msg,
46
+ );
47
+ }
48
+
49
+ type Loose = Record<PropertyKey, unknown>;
50
+
51
+ function isObject(val: unknown): boolean {
52
+ return typeof val === "object" && val !== null;
53
+ }
54
+
55
+ function defineProperty(target: object, key: PropertyKey, value: unknown) {
56
+ return Object.defineProperty(target, key, {
57
+ value,
58
+ configurable: true,
59
+ enumerable: true,
60
+ writable: true,
61
+ });
62
+ }
63
+
64
+ function filter(a: Loose, b: Loose): Loose {
65
+ const seen = new WeakMap<Loose | unknown[], Loose | unknown[]>();
66
+ return filterObject(a, b);
67
+
68
+ function filterObject(a: Loose, b: Loose): Loose {
69
+ // Prevent infinite loop with circular references with same filter
70
+ const memo = seen.get(a);
71
+ if (memo && (memo === b)) return a;
72
+
73
+ try {
74
+ seen.set(a, b);
75
+ } catch (err) {
76
+ if (err instanceof TypeError) {
77
+ throw new TypeError(
78
+ `Cannot assertObjectMatch ${a === null ? null : `type ${typeof a}`}`,
79
+ );
80
+ }
81
+ }
82
+
83
+ // Filter keys and symbols which are present in both actual and expected
84
+ const filtered = {} as Loose;
85
+ const keysA = Reflect.ownKeys(a);
86
+ const keysB = Reflect.ownKeys(b);
87
+ const entries = keysA.filter((key) => keysB.includes(key))
88
+ .map((key) => [key, a[key as string]]) as Array<[string, unknown]>;
89
+
90
+ if (keysA.length && keysB.length && !entries.length) {
91
+ // If both objects are not empty but don't have the same keys or symbols,
92
+ // returns the entries in object a.
93
+ for (const key of keysA) defineProperty(filtered, key, a[key]);
94
+ return filtered;
95
+ }
96
+
97
+ for (const [key, value] of entries) {
98
+ // On regexp references, keep value as it to avoid losing pattern and flags
99
+ if (value instanceof RegExp) {
100
+ defineProperty(filtered, key, value);
101
+ continue;
102
+ }
103
+
104
+ const subset = (b as Loose)[key];
105
+
106
+ // On array references, build a filtered array and filter nested objects inside
107
+ if (Array.isArray(value) && Array.isArray(subset)) {
108
+ defineProperty(filtered, key, filterArray(value, subset));
109
+ continue;
110
+ }
111
+
112
+ // On nested objects references, build a filtered object recursively
113
+ if (isObject(value) && isObject(subset)) {
114
+ // When both operands are maps, build a filtered map with common keys and filter nested objects inside
115
+ if ((value instanceof Map) && (subset instanceof Map)) {
116
+ defineProperty(
117
+ filtered,
118
+ key,
119
+ new Map(
120
+ [...value].filter(([k]) => subset.has(k)).map(
121
+ ([k, v]) => {
122
+ const v2 = subset.get(k);
123
+ if (isObject(v) && isObject(v2)) {
124
+ return [k, filterObject(v as Loose, v2 as Loose)];
125
+ }
126
+ return [k, v];
127
+ },
128
+ ),
129
+ ),
130
+ );
131
+ continue;
132
+ }
133
+
134
+ // When both operands are set, build a filtered set with common values
135
+ if ((value instanceof Set) && (subset instanceof Set)) {
136
+ defineProperty(filtered, key, value.intersection(subset));
137
+ continue;
138
+ }
139
+
140
+ defineProperty(
141
+ filtered,
142
+ key,
143
+ filterObject(value as Loose, subset as Loose),
144
+ );
145
+ continue;
146
+ }
147
+
148
+ defineProperty(filtered, key, value);
149
+ }
150
+
151
+ return filtered;
152
+ }
153
+
154
+ function filterArray(a: unknown[], b: unknown[]): unknown[] {
155
+ // Prevent infinite loop with circular references with same filter
156
+ const memo = seen.get(a);
157
+ if (memo && (memo === b)) return a;
158
+
159
+ seen.set(a, b);
160
+
161
+ const filtered: unknown[] = [];
162
+ const count = Math.min(a.length, b.length);
163
+
164
+ for (let i = 0; i < count; ++i) {
165
+ const value = a[i];
166
+ const subset = b[i];
167
+
168
+ // On regexp references, keep value as it to avoid losing pattern and flags
169
+ if (value instanceof RegExp) {
170
+ filtered.push(value);
171
+ continue;
172
+ }
173
+
174
+ // On array references, build a filtered array and filter nested objects inside
175
+ if (Array.isArray(value) && Array.isArray(subset)) {
176
+ filtered.push(filterArray(value, subset));
177
+ continue;
178
+ }
179
+
180
+ // On nested objects references, build a filtered object recursively
181
+ if (isObject(value) && isObject(subset)) {
182
+ // When both operands are maps, build a filtered map with common keys and filter nested objects inside
183
+ if ((value instanceof Map) && (subset instanceof Map)) {
184
+ const map = new Map(
185
+ [...value].filter(([k]) => subset.has(k))
186
+ .map(([k, v]) => {
187
+ const v2 = subset.get(k);
188
+ if (isObject(v) && isObject(v2)) {
189
+ return [k, filterObject(v as Loose, v2 as Loose)];
190
+ }
191
+
192
+ return [k, v];
193
+ }),
194
+ );
195
+ filtered.push(map);
196
+ continue;
197
+ }
198
+
199
+ // When both operands are set, build a filtered set with common values
200
+ if ((value instanceof Set) && (subset instanceof Set)) {
201
+ filtered.push(value.intersection(subset));
202
+ continue;
203
+ }
204
+
205
+ filtered.push(filterObject(value as Loose, subset as Loose));
206
+ continue;
207
+ }
208
+
209
+ filtered.push(value);
210
+ }
211
+
212
+ return filtered;
213
+ }
214
+ }
@@ -0,0 +1,123 @@
1
+ // Copyright 2018-2025 the Deno authors. MIT license.
2
+ // This module is browser compatible.
3
+ import { AssertionError } from "./assertion_error.js";
4
+ import { assertIsError } from "./is_error.js";
5
+
6
+ /**
7
+ * Executes a function which returns a promise, expecting it to reject.
8
+ *
9
+ * To assert that a synchronous function throws, use {@linkcode assertThrows}.
10
+ *
11
+ * @example Usage
12
+ * ```ts ignore
13
+ * import { assertRejects } from "@std/assert";
14
+ *
15
+ * await assertRejects(async () => Promise.reject(new Error())); // Doesn't throw
16
+ * await assertRejects(async () => console.log("Hello world")); // Throws
17
+ * ```
18
+ *
19
+ * @param fn The function to execute.
20
+ * @param msg The optional message to display if the assertion fails.
21
+ * @returns The promise which resolves to the thrown error.
22
+ */
23
+ export function assertRejects(
24
+ fn: () => PromiseLike<unknown>,
25
+ msg?: string,
26
+ ): Promise<unknown>;
27
+ /**
28
+ * Executes a function which returns a promise, expecting it to reject.
29
+ * If it does not, then it throws. An error class and a string that should be
30
+ * included in the error message can also be asserted.
31
+ *
32
+ * To assert that a synchronous function throws, use {@linkcode assertThrows}.
33
+ *
34
+ * @example Usage
35
+ * ```ts ignore
36
+ * import { assertRejects } from "@std/assert";
37
+ *
38
+ * await assertRejects(async () => Promise.reject(new Error()), Error); // Doesn't throw
39
+ * await assertRejects(async () => Promise.reject(new Error()), SyntaxError); // Throws
40
+ * ```
41
+ *
42
+ * @typeParam E The error class to assert.
43
+ * @param fn The function to execute.
44
+ * @param ErrorClass The error class to assert.
45
+ * @param msgIncludes The string that should be included in the error message.
46
+ * @param msg The optional message to display if the assertion fails.
47
+ * @returns The promise which resolves to the thrown error.
48
+ */
49
+ export function assertRejects<E extends Error = Error>(
50
+ fn: () => PromiseLike<unknown>,
51
+ // deno-lint-ignore no-explicit-any
52
+ ErrorClass: abstract new (...args: any[]) => E,
53
+ msgIncludes?: string,
54
+ msg?: string,
55
+ ): Promise<E>;
56
+ export async function assertRejects<E extends Error = Error>(
57
+ fn: () => PromiseLike<unknown>,
58
+ errorClassOrMsg?:
59
+ // deno-lint-ignore no-explicit-any
60
+ | (abstract new (...args: any[]) => E)
61
+ | string,
62
+ msgIncludesOrMsg?: string,
63
+ msg?: string,
64
+ ): Promise<E | Error | unknown> {
65
+ // deno-lint-ignore no-explicit-any
66
+ let ErrorClass: (abstract new (...args: any[]) => E) | undefined;
67
+ let msgIncludes: string | undefined;
68
+ let err;
69
+
70
+ if (typeof errorClassOrMsg !== "string") {
71
+ if (
72
+ errorClassOrMsg === undefined ||
73
+ errorClassOrMsg.prototype instanceof Error ||
74
+ errorClassOrMsg.prototype === Error.prototype
75
+ ) {
76
+ ErrorClass = errorClassOrMsg;
77
+ msgIncludes = msgIncludesOrMsg;
78
+ }
79
+ } else {
80
+ msg = errorClassOrMsg;
81
+ }
82
+ let doesThrow = false;
83
+ let isPromiseReturned = false;
84
+ const msgSuffix = msg ? `: ${msg}` : ".";
85
+ try {
86
+ const possiblePromise = fn();
87
+ if (
88
+ possiblePromise &&
89
+ typeof possiblePromise === "object" &&
90
+ typeof possiblePromise.then === "function"
91
+ ) {
92
+ isPromiseReturned = true;
93
+ await possiblePromise;
94
+ } else {
95
+ throw new Error();
96
+ }
97
+ } catch (error) {
98
+ if (!isPromiseReturned) {
99
+ throw new AssertionError(
100
+ `Function throws when expected to reject${msgSuffix}`,
101
+ );
102
+ }
103
+ if (ErrorClass) {
104
+ if (!(error instanceof Error)) {
105
+ throw new AssertionError(`A non-Error object was rejected${msgSuffix}`);
106
+ }
107
+ assertIsError(
108
+ error,
109
+ ErrorClass,
110
+ msgIncludes,
111
+ msg,
112
+ );
113
+ }
114
+ err = error;
115
+ doesThrow = true;
116
+ }
117
+ if (!doesThrow) {
118
+ throw new AssertionError(
119
+ `Expected function to reject${msgSuffix}`,
120
+ );
121
+ }
122
+ return err;
123
+ }