diff options
author | Linus Groh <mail@linusgroh.de> | 2021-06-16 20:44:53 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-06-16 22:49:04 +0100 |
commit | 748918964592b2fb560ce08de47779b6d5db30da (patch) | |
tree | 766492106e6f985d5654f5703512710c6456a127 | |
parent | 66e5e74374fde80452d58ce9ac991fcb232168c0 (diff) | |
download | serenity-748918964592b2fb560ce08de47779b6d5db30da.zip |
LibJS/Tests: Use Object.prototype.toString() for values in test details
Using String() like we did before depends on objects having either
toString, valueOf, or @@toPrimitive, which is not the case for objects
with no prototype.
-rw-r--r-- | Userland/Libraries/LibJS/Tests/test-common.js | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/Userland/Libraries/LibJS/Tests/test-common.js b/Userland/Libraries/LibJS/Tests/test-common.js index b761fb3be8..911d194dd9 100644 --- a/Userland/Libraries/LibJS/Tests/test-common.js +++ b/Userland/Libraries/LibJS/Tests/test-common.js @@ -51,6 +51,8 @@ class ExpectationError extends Error { return true; }; + const valueToString = value => Object.prototype.toString.call(value); + class Expector { constructor(target, inverted) { this.target = target; @@ -65,7 +67,10 @@ class ExpectationError extends Error { this.__doMatcher(() => { this.__expect( Object.is(this.target, value), - () => `toBe: expected _${String(value)}_, got _${String(this.target)}_` + () => + `toBe: expected _${valueToString(value)}_, got _${valueToString( + this.target + )}_` ); }); } @@ -167,7 +172,7 @@ class ExpectationError extends Error { this.__expect( this.target === undefined, () => - `toBeUndefined: expected target to be undefined, got _${String( + `toBeUndefined: expected target to be undefined, got _${valueToString( this.target )}_` ); @@ -178,7 +183,7 @@ class ExpectationError extends Error { this.__doMatcher(() => { this.__expect( isNaN(this.target), - () => `toBeNaN: expected target to be NaN, got _${String(this.target)}_` + () => `toBeNaN: expected target to be NaN, got _${valueToString(this.target)}_` ); }); } @@ -187,7 +192,8 @@ class ExpectationError extends Error { this.__doMatcher(() => { this.__expect( this.target === true, - () => `toBeTrue: expected target to be true, got _${String(this.target)}_` + () => + `toBeTrue: expected target to be true, got _${valueToString(this.target)}_` ); }); } @@ -196,7 +202,8 @@ class ExpectationError extends Error { this.__doMatcher(() => { this.__expect( this.target === false, - () => `toBeTrue: expected target to be false, got _${String(this.target)}_` + () => + `toBeTrue: expected target to be false, got _${valueToString(this.target)}_` ); }); } @@ -318,16 +325,16 @@ class ExpectationError extends Error { this.__expect( e instanceof class_, () => - `toThrowWithMessage: expected error to be instance of ${ + `toThrowWithMessage: expected error to be instance of ${valueToString( class_.name - }, got ${String(e.name)}` + )}, got ${valueToString(e.name)}` ); this.__expect( e.message.includes(message), () => - `toThrowWithMessage: expected error message to include _${String( + `toThrowWithMessage: expected error message to include _${valueToString( message - )}_, got _${String(e.message)}_` + )}_, got _${valueToString(e.message)}_` ); } }); |