mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 22:47:44 +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:
parent
66e5e74374
commit
7489189645
1 changed files with 16 additions and 9 deletions
|
@ -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)}_`
|
||||
);
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue