diff options
author | Casper Beyer <caspervonb@pm.me> | 2021-02-24 20:55:50 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-24 13:55:50 +0100 |
commit | 9cc7e32e37e6708980abc051f2cb71526c175d88 (patch) | |
tree | 42728035073e98745ebd022c6788ba4d4ba520fd /runtime/js/40_testing.js | |
parent | dc3683c7a433bf44656063c9eee87709fbe1e7d4 (diff) |
feat: add exit sanitizer to Deno.test (#9529)
This adds an exit sanitizer to ensure that code being tested or
dependencies of that code can't accidentally call "Deno.exit"
leading to partial test runs and false results.
Diffstat (limited to 'runtime/js/40_testing.js')
-rw-r--r-- | runtime/js/40_testing.js | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/runtime/js/40_testing.js b/runtime/js/40_testing.js index 2258dc7b6..eec75f133 100644 --- a/runtime/js/40_testing.js +++ b/runtime/js/40_testing.js @@ -4,7 +4,7 @@ ((window) => { const core = window.Deno.core; const colors = window.__bootstrap.colors; - const { exit } = window.__bootstrap.os; + const { setExitHandler, exit } = window.__bootstrap.os; const { Console, inspectArgs } = window.__bootstrap.console; const { stdout } = window.__bootstrap.files; const { exposeForTest } = window.__bootstrap.internals; @@ -86,6 +86,27 @@ finishing test case.`; }; } + // Wrap test function in additional assertion that makes sure + // that the test case does not accidentally exit prematurely. + function assertExit(fn) { + return async function exitSanitizer() { + setExitHandler((exitCode) => { + assert( + false, + `Test case attempted to exit with exit code: ${exitCode}`, + ); + }); + + try { + await fn(); + } catch (err) { + throw err; + } finally { + setExitHandler(null); + } + }; + } + const TEST_REGISTRY = []; // Main test function provided by Deno, as you can see it merely @@ -100,6 +121,7 @@ finishing test case.`; only: false, sanitizeOps: true, sanitizeResources: true, + sanitizeExit: true, }; if (typeof t === "string") { @@ -128,6 +150,10 @@ finishing test case.`; testDef.fn = assertResources(testDef.fn); } + if (testDef.sanitizeExit) { + testDef.fn = assertExit(testDef.fn); + } + TEST_REGISTRY.push(testDef); } |