diff --git a/builtins/web/console.cpp b/builtins/web/console.cpp index a2551d52..a5a18f5e 100644 --- a/builtins/web/console.cpp +++ b/builtins/web/console.cpp @@ -471,9 +471,9 @@ static bool console_out(JSContext *cx, unsigned argc, JS::Value *vp) { static bool assert_(JSContext *cx, unsigned argc, JS::Value *vp) { JS::CallArgs args = CallArgsFromVp(argc, vp); args.rval().setUndefined(); - auto condition = args.get(0).toBoolean(); + auto condition = JS::ToBoolean(args.get(0)); // 1. If condition is true, return. - if (!condition) { + if (condition) { return true; } diff --git a/tests/e2e/console-assert/console-assert.js b/tests/e2e/console-assert/console-assert.js new file mode 100644 index 00000000..094fe8fc --- /dev/null +++ b/tests/e2e/console-assert/console-assert.js @@ -0,0 +1,16 @@ +// https://console.spec.whatwg.org/#assert +// console.assert must stay SILENT when the condition is truthy and only log +// "Assertion failed" when the condition is falsy. The condition is coerced to +// a boolean, so non-boolean values must be accepted too. +addEventListener("fetch", (event) => { + console.log("a"); + console.assert(true); // truthy -> silent + console.log("b"); + console.assert(false); // falsy -> logs "Assertion failed" + console.log("c"); + console.assert(1); // truthy non-boolean -> silent + console.log("d"); + console.assert(0); // falsy non-boolean -> logs "Assertion failed" + console.log("e"); + event.respondWith(new Response("ok")); +}); diff --git a/tests/e2e/console-assert/expect_serve_stdout.txt b/tests/e2e/console-assert/expect_serve_stdout.txt new file mode 100644 index 00000000..05f2695c --- /dev/null +++ b/tests/e2e/console-assert/expect_serve_stdout.txt @@ -0,0 +1,7 @@ +stdout [0] :: Log: a +stdout [0] :: Log: b +stdout [0] :: Error: Assertion failed +stdout [0] :: Log: c +stdout [0] :: Log: d +stdout [0] :: Error: Assertion failed +stdout [0] :: Log: e diff --git a/tests/tests.cmake b/tests/tests.cmake index 7b7840f3..7045a364 100644 --- a/tests/tests.cmake +++ b/tests/tests.cmake @@ -46,6 +46,7 @@ function(integration_tests) endfunction() test_e2e(blob) +test_e2e(console-assert) test_e2e(eventloop-stall) test_e2e(headers) test_e2e(runtime-err)