summaryrefslogtreecommitdiff
path: root/cli/js/testing.ts
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/testing.ts
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/testing.ts')
-rw-r--r--cli/js/testing.ts61
1 files changed, 60 insertions, 1 deletions
diff --git a/cli/js/testing.ts b/cli/js/testing.ts
index ed34e08f7..0d21962c0 100644
--- a/cli/js/testing.ts
+++ b/cli/js/testing.ts
@@ -4,6 +4,9 @@ import { exit } from "./ops/os.ts";
import { Console, stringifyArgs } from "./web/console.ts";
import { stdout } from "./files.ts";
import { TextEncoder } from "./web/text_encoding.ts";
+import { metrics } from "./ops/runtime.ts";
+import { resources } from "./ops/resources.ts";
+import { assert } from "./util.ts";
const RED_FAILED = red("FAILED");
const GREEN_OK = green("ok");
@@ -15,12 +18,59 @@ function formatDuration(time = 0): string {
return gray(italic(timeStr));
}
+// 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: TestFunction): TestFunction {
+ return async function asyncOpSanitizer(): Promise<void> {
+ const pre = metrics();
+ await fn();
+ const post = metrics();
+ // We're checking diff because one might spawn HTTP server in the background
+ // that will be a pending async op before test starts.
+ const dispatchedDiff = post.opsDispatchedAsync - pre.opsDispatchedAsync;
+ const completedDiff = post.opsCompletedAsync - pre.opsCompletedAsync;
+ assert(
+ dispatchedDiff === completedDiff,
+ `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: TestFunction): TestFunction {
+ return async function resourceSanitizer(): Promise<void> {
+ const pre = resources();
+ await fn();
+ const post = resources();
+
+ const preStr = JSON.stringify(pre, null, 2);
+ const postStr = JSON.stringify(post, null, 2);
+ const msg = `Test case is leaking resources.
+Before: ${preStr}
+After: ${postStr}`;
+ assert(preStr === postStr, msg);
+ };
+}
+
export type TestFunction = () => void | Promise<void>;
export interface TestDefinition {
fn: TestFunction;
name: string;
skip?: boolean;
+ disableOpSanitizer?: boolean;
+ disableResourceSanitizer?: boolean;
}
const TEST_REGISTRY: TestDefinition[] = [];
@@ -56,7 +106,16 @@ export function test(
if (!t.name) {
throw new TypeError("The test name can't be empty");
}
- testDef = { fn: t.fn, name: t.name, skip: Boolean(t.skip) };
+
+ testDef = { ...t, skip: Boolean(t.skip) };
+ }
+
+ if (testDef.disableOpSanitizer !== true) {
+ testDef.fn = assertOps(testDef.fn);
+ }
+
+ if (testDef.disableResourceSanitizer !== true) {
+ testDef.fn = assertResources(testDef.fn);
}
TEST_REGISTRY.push(testDef);