[gjs: 1/4] minijasmine: Print test JS errors output if any




commit ab168be8fd1e8438ae5d5e03b7323bbf9f3cf78c
Author: Marco Trevisan (Treviño) <mail 3v1n0 net>
Date:   Wed Jun 8 18:04:26 2022 +0200

    minijasmine: Print test JS errors output if any
    
    JS errors on tests are currently only visible by checking the log files,
    and this can make debugging annoying.
    
    So, save error output in a global jasmine array and print it in case we
    got errors.

 installed-tests/js/minijasmine.js | 17 ++++++++++++++---
 installed-tests/minijasmine.cpp   | 14 ++++++++++++--
 2 files changed, 26 insertions(+), 5 deletions(-)
---
diff --git a/installed-tests/js/minijasmine.js b/installed-tests/js/minijasmine.js
index c1771c44e..5b7885dd6 100644
--- a/installed-tests/js/minijasmine.js
+++ b/installed-tests/js/minijasmine.js
@@ -27,6 +27,7 @@ globalThis._jasmineEnv.configure({
 });
 globalThis._jasmineMain = GLib.MainLoop.new(null, false);
 globalThis._jasmineRetval = 0;
+globalThis._jasmineErrorsOutput = [];
 
 // Install Jasmine API on the global object
 let jasmineInterface = jasmineRequire.interface(jasmineCore, globalThis._jasmineEnv);
@@ -87,10 +88,20 @@ class TapReporter {
         // Print additional diagnostic info on failure
         if (result.status === 'failed' && result.failedExpectations) {
             result.failedExpectations.forEach(failedExpectation => {
-                print('# Message:', _removeNewlines(failedExpectation.message));
-                print('# Stack:');
+                const output = [];
+                output.push(`Message: ${_removeNewlines(failedExpectation.message)}`);
+                output.push('Stack:');
                 let stackTrace = _filterStack(failedExpectation.stack).trim();
-                print(stackTrace.split('\n').map(str => `#   ${str}`).join('\n'));
+                output.push(...stackTrace.split('\n').map(str => `  ${str}`));
+
+                if (globalThis._jasmineErrorsOutput.length) {
+                    globalThis._jasmineErrorsOutput.push(
+                        Array(GLib.getenv('COLUMNS') || 80).fill('―').join(''));
+                }
+
+                globalThis._jasmineErrorsOutput.push(`Test: ${result.fullName}`);
+                globalThis._jasmineErrorsOutput.push(...output);
+                print(output.map(l => `# ${l}`).join('\n'));
             });
         }
     }
diff --git a/installed-tests/minijasmine.cpp b/installed-tests/minijasmine.cpp
index 9cc1d7b35..17b564d10 100644
--- a/installed-tests/minijasmine.cpp
+++ b/installed-tests/minijasmine.cpp
@@ -104,8 +104,18 @@ main(int argc, char **argv)
     if (!success)
         bail_out(cx, error->message);
 
-    if (code != 0)
-        g_print("# Test script failed; see test log for assertions\n");
+    if (code != 0) {
+        success = gjs_context_eval(cx, R"js(
+            printerr(globalThis._jasmineErrorsOutput.join('\n'));
+        )js",
+                                   -1, "<jasmine-error-logs>", &code, &error);
+
+        if (!success)
+            bail_out(cx, error->message);
+
+        if (code != 0)
+            g_print("# Test script failed; see test log for assertions\n");
+    }
 
     if (coverage) {
         gjs_coverage_write_statistics(coverage);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]