summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
Diffstat (limited to 'js')
-rw-r--r--js/console.ts22
-rw-r--r--js/console_test.ts7
2 files changed, 23 insertions, 6 deletions
diff --git a/js/console.ts b/js/console.ts
index bb597d9f2..311ca83b7 100644
--- a/js/console.ts
+++ b/js/console.ts
@@ -383,11 +383,27 @@ export class Console {
/** Writes an error message to stdout if the assertion is `false`. If the
* assertion is `true`, nothing happens.
+ *
+ * ref: https://console.spec.whatwg.org/#assert
*/
// tslint:disable-next-line:no-any
- assert = (condition: boolean, ...args: any[]): void => {
- if (!condition) {
- throw new Error(`Assertion failed: ${stringifyArgs(args)}`);
+ assert = (condition?: boolean, ...args: any[]): void => {
+ if (condition) {
+ return;
}
+
+ if (args.length === 0) {
+ this.error("Assertion failed");
+ return;
+ }
+
+ const [first, ...rest] = args;
+
+ if (typeof first === "string") {
+ this.error(`Assertion failed: ${first}`, ...rest);
+ return;
+ }
+
+ this.error(`Assertion failed:`, ...args);
};
}
diff --git a/js/console_test.ts b/js/console_test.ts
index 578fb02b4..3e6bd6905 100644
--- a/js/console_test.ts
+++ b/js/console_test.ts
@@ -7,16 +7,17 @@ function stringify(...args: any[]): string {
return stringifyArgs(args);
}
-test(function consoleTestAssert() {
+test(function consoleTestAssertShouldNotThrowError() {
console.assert(true);
- let hasThrown = false;
+ let hasThrown = undefined;
try {
console.assert(false);
+ hasThrown = false;
} catch {
hasThrown = true;
}
- assertEqual(hasThrown, true);
+ assertEqual(hasThrown, false);
});
test(function consoleTestStringifyComplexObjects() {