Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

console.assert should not throw error #1335

Merged
merged 6 commits into from
Dec 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions js/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
}
7 changes: 4 additions & 3 deletions js/console_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down