summaryrefslogtreecommitdiff
path: root/cli/js/40_test.js
diff options
context:
space:
mode:
authorLuke Edwards <lukeed@github.com>2024-05-29 16:16:27 -0700
committerGitHub <noreply@github.com>2024-05-29 23:16:27 +0000
commit13723f267eb87f8c28ef0769cdf7e233b971326e (patch)
treefd97b4c13889aa657601440678f2e31316d8e47d /cli/js/40_test.js
parentcf611fbf548ea0bbd38c82ab02249b7a2aa3b3c9 (diff)
feat: Add `Deno.exitCode` API (#23609)
This commits adds the ability to set a would-be exit code for the Deno process without forcing an immediate exit, through the new `Deno.exitCode` API. - **Implements `Deno.exitCode` getter and setter**: Adds support for setting and retrieving a would-be exit code via `Deno.exitCode`. This allows for asynchronous cleanup before process termination without immediately exiting. - **Ensures type safety**: The setter for `Deno.exitCode` validates that the provided value is a number, throwing a TypeError if not, to ensure that only valid exit codes are set. Closes to #23605 --------- Co-authored-by: Bartek IwaƄczuk <biwanczuk@gmail.com>
Diffstat (limited to 'cli/js/40_test.js')
-rw-r--r--cli/js/40_test.js19
1 files changed, 18 insertions, 1 deletions
diff --git a/cli/js/40_test.js b/cli/js/40_test.js
index 2877bfa9b..5a081e217 100644
--- a/cli/js/40_test.js
+++ b/cli/js/40_test.js
@@ -28,6 +28,10 @@ const {
import { setExitHandler } from "ext:runtime/30_os.js";
+// Capture `Deno` global so that users deleting or mangling it, won't
+// have impact on our sanitizers.
+const DenoNs = globalThis.Deno;
+
/**
* @typedef {{
* id: number,
@@ -101,7 +105,20 @@ function assertExit(fn, isTest) {
try {
const innerResult = await fn(...new SafeArrayIterator(params));
- if (innerResult) return innerResult;
+ const exitCode = DenoNs.exitCode;
+ if (exitCode !== 0) {
+ // Reset the code to allow other tests to run...
+ DenoNs.exitCode = 0;
+ // ...and fail the current test.
+ throw new Error(
+ `${
+ isTest ? "Test case" : "Bench"
+ } finished with exit code set to ${exitCode}.`,
+ );
+ }
+ if (innerResult) {
+ return innerResult;
+ }
} finally {
setExitHandler(null);
}