1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 07:07:34 +00:00

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.
This commit is contained in:
Linus Groh 2021-06-16 20:44:53 +01:00
parent 66e5e74374
commit 7489189645

View file

@ -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)}_`
);
}
});