Skip to content

Commit

Permalink
LibJS/Tests: Use hasOwnProperty() for duplicate test check
Browse files Browse the repository at this point in the history
The current way of doing this would also traverse the prototype chain,
and therefore yield false positive results for keys like "toString".
  • Loading branch information
linusg committed May 5, 2021
1 parent d9702ff commit 346560d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
10 changes: 8 additions & 2 deletions Userland/Libraries/LibJS/Tests/test-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 346560d

Please sign in to comment.