Browse Source

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".
Linus Groh 4 năm trước cách đây
mục cha
commit
346560d7c8

+ 1 - 2
Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype-generic-functions.js

@@ -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");

+ 8 - 2
Userland/Libraries/LibJS/Tests/test-common.js

@@ -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",