diff options
Diffstat (limited to 'cli/js/test_util.ts')
-rw-r--r-- | cli/js/test_util.ts | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/cli/js/test_util.ts b/cli/js/test_util.ts index 6f52c01df..027610dd9 100644 --- a/cli/js/test_util.ts +++ b/cli/js/test_util.ts @@ -107,10 +107,36 @@ function normalizeTestPermissions(perms: TestPermissions): Permissions { } // Wrap `TestFunction` in additional assertion that makes sure +// the test case does not leak async "ops" - ie. number of async +// completed ops after the test is the same as number of dispatched +// ops. Note that "unref" ops are ignored since in nature that are +// optional. +function assertOps(fn: Deno.TestFunction): Deno.TestFunction { + return async function asyncOpSanitizer(): Promise<void> { + const pre = Deno.metrics(); + await fn(); + const post = Deno.metrics(); + // We're checking diff because one might spawn HTTP server in the background + // that will be a pending async op before test starts. + assertEquals( + post.opsDispatchedAsync - pre.opsDispatchedAsync, + post.opsCompletedAsync - pre.opsCompletedAsync, + `Test case is leaking async ops. + Before: + - dispatched: ${pre.opsDispatchedAsync} + - completed: ${pre.opsCompletedAsync} + After: + - dispatched: ${post.opsDispatchedAsync} + - completed: ${post.opsCompletedAsync}` + ); + }; +} + +// Wrap `TestFunction` in additional assertion that makes sure // the test case does not "leak" resources - ie. resource table after // the test has exactly the same contents as before the test. function assertResources(fn: Deno.TestFunction): Deno.TestFunction { - return async function(): Promise<void> { + return async function resourceSanitizer(): Promise<void> { const preResources = Deno.resources(); await fn(); const postResources = Deno.resources(); @@ -131,7 +157,7 @@ export function testPerm(perms: TestPermissions, fn: Deno.TestFunction): void { return; } - Deno.test(fn.name, assertResources(fn)); + Deno.test(fn.name, assertResources(assertOps(fn))); } export function test(fn: Deno.TestFunction): void { |