LibJS/Tests: Use hasOwnProperty() for duplicate test check

The current way of doing this would also traverse the prototype chain,
and therefore yield false positive results for keys like "toString".
This commit is contained in:
Linus Groh 2021-05-05 15:54:39 +01:00
parent d9702ff561
commit 346560d7c8
Notes: sideshowbarker 2024-07-18 18:41:37 +09:00
2 changed files with 9 additions and 4 deletions

View file

@ -53,8 +53,7 @@ describe("ability to work with generic non-array objects", () => {
);
});
// FIXME: test-js asserts when this is just called "toString" ಠ_ಠ
test("toString (FIXME)", () => {
test("toString", () => {
expect(Array.prototype.toString.call({})).toBe("[object Object]");
expect(Array.prototype.toString.call({ join: "foo" })).toBe("[object Object]");
expect(Array.prototype.toString.call({ join: () => "foo" })).toBe("foo");

View file

@ -431,7 +431,7 @@ class ExpectationError extends Error {
if (!__TestResults__[suiteMessage]) __TestResults__[suiteMessage] = {};
const suite = __TestResults__[suiteMessage];
if (suite[message]) {
if (Object.prototype.hasOwnProperty.call(suite, message)) {
suite[message] = {
result: "fail",
details: "Another test with the same message did already run",
@ -459,7 +459,13 @@ class ExpectationError extends Error {
if (!__TestResults__[suiteMessage]) __TestResults__[suiteMessage] = {};
const suite = __TestResults__[suiteMessage];
if (suite[message]) throw new Error("Duplicate test name: " + message);
if (Object.prototype.hasOwnProperty.call(suite, message)) {
suite[message] = {
result: "fail",
details: "Another test with the same message did already run",
};
return;
}
suite[message] = {
result: "skip",