浏览代码

LibJS/Tests: Add support for expected failure (xfail) tests

To allow us to add tests that are failing now, but can be enabled as
soon as a change is made to make it pass (without any opportunity to
forget about enabling it).

Additionally, support is added for `xfailIf`, for tests that are
expected to fail given a certain condition, but are expected to pass
otherwise. This is intended to be used for tests that fail in bytecode
mode, but pass in AST (and vice versa).
Shannon Booth 2 年之前
父节点
当前提交
2401c0ff7e
共有 1 个文件被更改,包括 35 次插入0 次删除
  1. 35 0
      Userland/Libraries/LibJS/Tests/test-common.js

+ 35 - 0
Userland/Libraries/LibJS/Tests/test-common.js

@@ -630,6 +630,41 @@ class ExpectationError extends Error {
         };
     };
 
+    test.xfail = (message, callback) => {
+        if (!__TestResults__[suiteMessage]) __TestResults__[suiteMessage] = {};
+
+        const suite = __TestResults__[suiteMessage];
+        if (Object.prototype.hasOwnProperty.call(suite, message)) {
+            suite[message] = {
+                result: "fail",
+                details: "Another test with the same message did already run",
+                duration: 0,
+            };
+            return;
+        }
+
+        const now = () => Temporal.Now.instant().epochNanoseconds;
+        const start = now();
+        const time_us = () => Number(BigInt.asIntN(53, (now() - start) / 1000n));
+        try {
+            callback();
+            suite[message] = {
+                result: "fail",
+                details: "Expected test to fail, but it passed",
+                duration: time_us(),
+            };
+        } catch (e) {
+            suite[message] = {
+                result: "xfail",
+                duration: time_us(),
+            };
+        }
+    };
+
+    test.xfailIf = (condition, message, callback) => {
+        condition ? test.xfail(message, callback) : test(message, callback);
+    };
+
     withinSameSecond = callback => {
         let callbackDuration;
         for (let tries = 0; tries < 5; tries++) {