From 5d7d9d64434bd0a9f1fcf391dabc51693e8cf1ae Mon Sep 17 00:00:00 2001 From: David Sherret Date: Wed, 25 Aug 2021 16:04:14 -0400 Subject: [PATCH] chore(tests): improve unit tests using `deferred` (#11842) --- cli/tests/unit/http_test.ts | 16 ++++++---------- cli/tests/unit/net_test.ts | 8 ++------ cli/tests/unit/performance_test.ts | 4 +++- cli/tests/unit/timers_test.ts | 16 +++++++++++----- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/cli/tests/unit/http_test.ts b/cli/tests/unit/http_test.ts index 201ca12698..6805318763 100644 --- a/cli/tests/unit/http_test.ts +++ b/cli/tests/unit/http_test.ts @@ -803,8 +803,6 @@ unitTest( { perms: { net: true } }, async function httpServerIncompleteMessage() { const listener = Deno.listen({ port: 4501 }); - const def1 = deferred(); - const def2 = deferred(); const client = await Deno.connect({ port: 4501 }); await client.write(new TextEncoder().encode( @@ -830,23 +828,21 @@ unitTest( const errors: Error[] = []; - writeResponse() + const writePromise = writeResponse() .catch((error: Error) => { errors.push(error); - }) - .then(() => def1.resolve()); + }); const res = new Response(readable); - respondWith(res) - .catch((error: Error) => errors.push(error)) - .then(() => def2.resolve()); + const respondPromise = respondWith(res) + .catch((error: Error) => errors.push(error)); client.close(); await Promise.all([ - def1, - def2, + writePromise, + respondPromise, ]); listener.close(); diff --git a/cli/tests/unit/net_test.ts b/cli/tests/unit/net_test.ts index 65145edc21..bd949b02d3 100644 --- a/cli/tests/unit/net_test.ts +++ b/cli/tests/unit/net_test.ts @@ -394,8 +394,6 @@ unitTest( unitTest( { perms: { net: true } }, async function netTcpListenIteratorBreakClosesResource() { - const promise = deferred(); - async function iterate(listener: Deno.Listener) { let i = 0; @@ -407,13 +405,11 @@ unitTest( break; } } - - promise.resolve(); } const addr = { hostname: "127.0.0.1", port: 8888 }; const listener = Deno.listen(addr); - iterate(listener); + const iteratePromise = iterate(listener); await delay(100); const conn1 = await Deno.connect(addr); @@ -421,7 +417,7 @@ unitTest( const conn2 = await Deno.connect(addr); conn2.close(); - await promise; + await iteratePromise; }, ); diff --git a/cli/tests/unit/performance_test.ts b/cli/tests/unit/performance_test.ts index 989d221d87..7ea1720a57 100644 --- a/cli/tests/unit/performance_test.ts +++ b/cli/tests/unit/performance_test.ts @@ -11,12 +11,14 @@ import { unitTest({ perms: { hrtime: false } }, async function performanceNow() { const resolvable = deferred(); const start = performance.now(); + let totalTime = 0; setTimeout(() => { const end = performance.now(); - assert(end - start >= 10); + totalTime = end - start; resolvable.resolve(); }, 10); await resolvable; + assert(totalTime >= 10); }); unitTest(function performanceMark() { diff --git a/cli/tests/unit/timers_test.ts b/cli/tests/unit/timers_test.ts index 4eb470ba9a..2decddbd98 100644 --- a/cli/tests/unit/timers_test.ts +++ b/cli/tests/unit/timers_test.ts @@ -86,11 +86,10 @@ unitTest(async function timeoutEvalNoScopeLeak() { unitTest(async function timeoutArgs() { const promise = deferred(); const arg = 1; + let capturedArgs: unknown[] = []; setTimeout( - (a, b, c) => { - assertEquals(a, arg); - assertEquals(b, arg.toString()); - assertEquals(c, [arg]); + function () { + capturedArgs = [...arguments]; promise.resolve(); }, 10, @@ -99,6 +98,11 @@ unitTest(async function timeoutArgs() { [arg], ); await promise; + assertEquals(capturedArgs, [ + arg, + arg.toString(), + [arg], + ]); }); unitTest(async function timeoutCancelSuccess() { @@ -214,14 +218,16 @@ unitTest(async function fireCallbackImmediatelyWhenDelayOverMaxValue() { unitTest(async function timeoutCallbackThis() { const promise = deferred(); + let capturedThis: unknown; const obj = { foo() { - assertEquals(this, window); + capturedThis = this; promise.resolve(); }, }; setTimeout(obj.foo, 1); await promise; + assertEquals(capturedThis, window); }); unitTest(async function timeoutBindThis() {