summaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
Diffstat (limited to 'std')
-rw-r--r--std/node/_util/_util_callbackify_test.ts4
-rw-r--r--std/node/_util/_util_promisify.ts25
-rw-r--r--std/node/_util/_util_promisify_test.ts2
-rw-r--r--std/testing/asserts.ts55
-rw-r--r--std/testing/asserts_test.ts11
5 files changed, 84 insertions, 13 deletions
diff --git a/std/node/_util/_util_callbackify_test.ts b/std/node/_util/_util_callbackify_test.ts
index d6a5d8664..e8a313905 100644
--- a/std/node/_util/_util_callbackify_test.ts
+++ b/std/node/_util/_util_callbackify_test.ts
@@ -246,7 +246,7 @@ Deno.test("callbackify passes arguments to the original", async () => {
for (const value of values) {
// eslint-disable-next-line require-await
- async function asyncFn<T>(arg: T): Promise<T> {
+ async function asyncFn(arg: typeof value): Promise<typeof value> {
assertStrictEquals(arg, value);
return arg;
}
@@ -263,7 +263,7 @@ Deno.test("callbackify passes arguments to the original", async () => {
});
});
- function promiseFn<T>(arg: T): Promise<T> {
+ function promiseFn<T>(arg: typeof value): Promise<typeof value> {
assertStrictEquals(arg, value);
return Promise.resolve(arg);
}
diff --git a/std/node/_util/_util_promisify.ts b/std/node/_util/_util_promisify.ts
index e3cc36a0c..ea2fb6a5e 100644
--- a/std/node/_util/_util_promisify.ts
+++ b/std/node/_util/_util_promisify.ts
@@ -21,13 +21,30 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
+// Hack: work around the following TypeScript error:
+// error: TS2345 [ERROR]: Argument of type 'typeof kCustomPromisifiedSymbol'
+// is not assignable to parameter of type 'typeof kCustomPromisifiedSymbol'.
+// assertStrictEquals(kCustomPromisifiedSymbol, promisify.custom);
+// ~~~~~~~~~~~~~~~~
+declare const _CustomPromisifiedSymbol: unique symbol;
+declare const _CustomPromisifyArgsSymbol: unique symbol;
+declare let Symbol: SymbolConstructor;
+interface SymbolConstructor {
+ for(key: "nodejs.util.promisify.custom"): typeof _CustomPromisifiedSymbol;
+ for(
+ key: "nodejs.util.promisify.customArgs"
+ ): typeof _CustomPromisifyArgsSymbol;
+}
+// End hack.
+
// In addition to being accessible through util.promisify.custom,
-// this symbol is registered globally and can be accessed in any environment as Symbol.for('nodejs.util.promisify.custom')
+// this symbol is registered globally and can be accessed in any environment as
+// Symbol.for('nodejs.util.promisify.custom').
const kCustomPromisifiedSymbol = Symbol.for("nodejs.util.promisify.custom");
-// This is an internal Node symbol used by functions returning multiple arguments
-// e.g. ['bytesRead', 'buffer'] for fs.read.
+// This is an internal Node symbol used by functions returning multiple
+// arguments, e.g. ['bytesRead', 'buffer'] for fs.read().
const kCustomPromisifyArgsSymbol = Symbol.for(
- "deno.nodejs.util.promisify.customArgs"
+ "nodejs.util.promisify.customArgs"
);
class NodeInvalidArgTypeError extends TypeError {
diff --git a/std/node/_util/_util_promisify_test.ts b/std/node/_util/_util_promisify_test.ts
index c6dbbd45a..4369a0132 100644
--- a/std/node/_util/_util_promisify_test.ts
+++ b/std/node/_util/_util_promisify_test.ts
@@ -30,7 +30,7 @@ import { promisify } from "./_util_promisify.ts";
import * as fs from "../fs.ts";
const readFile = promisify(fs.readFile);
-const customPromisifyArgs = Symbol.for("deno.nodejs.util.promisify.customArgs");
+const customPromisifyArgs = Symbol.for("nodejs.util.promisify.customArgs");
Deno.test(
"Errors should reject the promise",
diff --git a/std/testing/asserts.ts b/std/testing/asserts.ts
index ea15aa6bc..0cbd6d2ad 100644
--- a/std/testing/asserts.ts
+++ b/std/testing/asserts.ts
@@ -133,7 +133,7 @@ export function equal(c: unknown, d: unknown): boolean {
})(c, d);
}
-/** Make an assertion, if not `true`, then throw. */
+/** Make an assertion, error will be thrown if `expr` does not have truthy value. */
export function assert(expr: unknown, msg = ""): asserts expr {
if (!expr) {
throw new AssertionError(msg);
@@ -143,11 +143,23 @@ export function assert(expr: unknown, msg = ""): asserts expr {
/**
* Make an assertion that `actual` and `expected` are equal, deeply. If not
* deeply equal, then throw.
+ *
+ * Type parameter can be specified to ensure values under comparison have the same type.
+ * For example:
+ *```ts
+ *assertEquals<number>(1, 2)
+ *```
*/
export function assertEquals(
actual: unknown,
expected: unknown,
msg?: string
+): void;
+export function assertEquals<T>(actual: T, expected: T, msg?: string): void;
+export function assertEquals(
+ actual: unknown,
+ expected: unknown,
+ msg?: string
): void {
if (equal(actual, expected)) {
return;
@@ -174,11 +186,23 @@ export function assertEquals(
/**
* Make an assertion that `actual` and `expected` are not equal, deeply.
* If not then throw.
+ *
+ * Type parameter can be specified to ensure values under comparison have the same type.
+ * For example:
+ *```ts
+ *assertNotEquals<number>(1, 2)
+ *```
*/
export function assertNotEquals(
actual: unknown,
expected: unknown,
msg?: string
+): void;
+export function assertNotEquals<T>(actual: T, expected: T, msg?: string): void;
+export function assertNotEquals(
+ actual: unknown,
+ expected: unknown,
+ msg?: string
): void {
if (!equal(actual, expected)) {
return;
@@ -204,10 +228,13 @@ export function assertNotEquals(
/**
* Make an assertion that `actual` and `expected` are strictly equal. If
* not then throw.
+ * ```ts
+ * assertStrictEquals(1, 2)
+ * ```
*/
-export function assertStrictEquals(
- actual: unknown,
- expected: unknown,
+export function assertStrictEquals<T>(
+ actual: T,
+ expected: T,
msg?: string
): void {
if (actual === expected) {
@@ -265,13 +292,29 @@ export function assertStringContains(
}
/**
- * Make an assertion that `actual` contains the `expected` values
- * If not then thrown.
+ * Make an assertion that `actual` contains the `expected` values.
+ * If not then an error will be thrown.
+ *
+ * Type parameter can be specified to ensure values under comparison have the same type.
+ * For example:
+ *```ts
+ *assertArrayContains<number>([1, 2], [2])
+ *```
*/
export function assertArrayContains(
actual: ArrayLike<unknown>,
expected: ArrayLike<unknown>,
msg?: string
+): void;
+export function assertArrayContains<T>(
+ actual: ArrayLike<T>,
+ expected: ArrayLike<T>,
+ msg?: string
+): void;
+export function assertArrayContains(
+ actual: ArrayLike<unknown>,
+ expected: ArrayLike<unknown>,
+ msg?: string
): void {
const missing: unknown[] = [];
for (let i = 0; i < expected.length; i++) {
diff --git a/std/testing/asserts_test.ts b/std/testing/asserts_test.ts
index 5537b41e7..854cb9730 100644
--- a/std/testing/asserts_test.ts
+++ b/std/testing/asserts_test.ts
@@ -412,6 +412,17 @@ Deno.test({
},
});
+Deno.test({
+ name: "assert* functions with specified type paratemeter",
+ fn(): void {
+ assertEquals<string>("hello", "hello");
+ assertNotEquals<number>(1, 2);
+ assertArrayContains<boolean>([true, false], [true]);
+ const value = { x: 1 };
+ assertStrictEquals<typeof value>(value, value);
+ },
+});
+
Deno.test("Assert Throws Non-Error Fail", () => {
assertThrows(
() => {