diff --git a/std/node/_util/_util_callbackify_test.ts b/std/node/_util/_util_callbackify_test.ts index d6a5d86644..e8a3139053 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(arg: T): Promise { + async function asyncFn(arg: typeof value): Promise { assertStrictEquals(arg, value); return arg; } @@ -263,7 +263,7 @@ Deno.test("callbackify passes arguments to the original", async () => { }); }); - function promiseFn(arg: T): Promise { + function promiseFn(arg: typeof value): Promise { assertStrictEquals(arg, value); return Promise.resolve(arg); } diff --git a/std/node/_util/_util_promisify.ts b/std/node/_util/_util_promisify.ts index e3cc36a0c1..ea2fb6a5e3 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 c6dbbd45a5..4369a01329 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 ea15aa6bc1..0cbd6d2ad5 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,7 +143,19 @@ 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(1, 2) + *``` */ +export function assertEquals( + actual: unknown, + expected: unknown, + msg?: string +): void; +export function assertEquals(actual: T, expected: T, msg?: string): void; export function assertEquals( actual: unknown, expected: unknown, @@ -174,7 +186,19 @@ 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(1, 2) + *``` */ +export function assertNotEquals( + actual: unknown, + expected: unknown, + msg?: string +): void; +export function assertNotEquals(actual: T, expected: T, msg?: string): void; export function assertNotEquals( actual: unknown, expected: unknown, @@ -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( + actual: T, + expected: T, msg?: string ): void { if (actual === expected) { @@ -265,9 +292,25 @@ 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([1, 2], [2]) + *``` */ +export function assertArrayContains( + actual: ArrayLike, + expected: ArrayLike, + msg?: string +): void; +export function assertArrayContains( + actual: ArrayLike, + expected: ArrayLike, + msg?: string +): void; export function assertArrayContains( actual: ArrayLike, expected: ArrayLike, diff --git a/std/testing/asserts_test.ts b/std/testing/asserts_test.ts index 5537b41e79..854cb97304 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("hello", "hello"); + assertNotEquals(1, 2); + assertArrayContains([true, false], [true]); + const value = { x: 1 }; + assertStrictEquals(value, value); + }, +}); + Deno.test("Assert Throws Non-Error Fail", () => { assertThrows( () => {