summaryrefslogtreecommitdiff
path: root/cli/js/tests
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2020-03-19 00:25:55 +0100
committerGitHub <noreply@github.com>2020-03-18 19:25:55 -0400
commit6e2df8c64feb92653077a5494d2c64a9f6fd2f48 (patch)
treed359310f24622ef38b57538fcdc698f6cadb620d /cli/js/tests
parent070464e2cc617ecbd2c63dc5c4ac0432a77a29fd (diff)
feat: Deno.test() sanitizes ops and resources (#4399)
This PR brings assertOps and assertResources sanitizers to Deno.test() API. assertOps checks that test doesn't leak async ops, ie. there are no unresolved promises originating from Deno APIs. Enabled by default, can be disabled using Deno.TestDefinition.disableOpSanitizer. assertResources checks that test doesn't leak resources, ie. all resources used in test are closed. For example; if a file is opened during a test case it must be explicitly closed before test case finishes. It's most useful for asynchronous generators. Enabled by default, can be disabled using Deno.TestDefinition.disableResourceSanitizer. We've used those sanitizers in internal runtime tests and it proved very useful in surfacing incorrect tests which resulted in interference between the tests. All tests have been sanitized. Closes #4208
Diffstat (limited to 'cli/js/tests')
-rw-r--r--cli/js/tests/test_util.ts43
1 files changed, 1 insertions, 42 deletions
diff --git a/cli/js/tests/test_util.ts b/cli/js/tests/test_util.ts
index 621d86bfd..851596b11 100644
--- a/cli/js/tests/test_util.ts
+++ b/cli/js/tests/test_util.ts
@@ -112,47 +112,6 @@ function normalizeTestPermissions(perms: UnitTestPermissions): 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 resourceSanitizer(): Promise<void> {
- const pre = Deno.resources();
- await fn();
- const post = Deno.resources();
- const msg = `Test case is leaking resources.
- Before: ${JSON.stringify(pre, null, 2)}
- After: ${JSON.stringify(post, null, 2)}`;
- assertEquals(pre, post, msg);
- };
-}
-
interface UnitTestPermissions {
read?: boolean;
write?: boolean;
@@ -209,7 +168,7 @@ export function unitTest(
const unitTestDefinition: UnitTestDefinition = {
name,
- fn: assertResources(assertOps(fn)),
+ fn,
skip: !!options.skip,
perms: normalizedPerms
};