DEV: lint run-qunit

DEV: lint run-qunit

also handles situation where startsWith is missing, when this happens
simply log

This corrects an issue where console.log explodes autospec

From 74b300110f2e2760e9a8ec6a3ea65b29d3bfd33c Mon Sep 17 00:00:00 2001
From: Sam <sam.saffron@gmail.com>
Date: Tue, 27 Nov 2018 17:40:55 +1100
Subject: [PATCH] DEV: lint run-qunit

also handles situation where startsWith is missing, when this happens
simply log

This corrects an issue where console.log explodes autospec

diff --git a/vendor/assets/javascripts/run-qunit.js b/vendor/assets/javascripts/run-qunit.js
index 9295969..f7eb97c 100644
--- a/vendor/assets/javascripts/run-qunit.js
+++ b/vendor/assets/javascripts/run-qunit.js
@@ -12,11 +12,11 @@ if (args.length < 1 || args.length > 3) {
   process.exit(1);
 }
 
-const chromeLauncher = require('chrome-launcher');
-const CDP = require('chrome-remote-interface');
+const chromeLauncher = require("chrome-launcher");
+const CDP = require("chrome-remote-interface");
 
 const QUNIT_RESULT = args[2];
-const fs = require('fs');
+const fs = require("fs");
 
 if (QUNIT_RESULT) {
   (async () => {
@@ -27,14 +27,9 @@ if (QUNIT_RESULT) {
 }
 
 async function runAllTests() {
-
   function launchChrome() {
     const options = {
-      chromeFlags: [
-        '--disable-gpu',
-       '--headless',
-       '--no-sandbox'
-      ]
+      chromeFlags: ["--disable-gpu", "--headless", "--no-sandbox"]
     };
 
     if (process.env.REMOTE_DEBUG) {
@@ -45,19 +40,23 @@ async function runAllTests() {
   }
 
   let chrome = await launchChrome();
-  let protocol = await CDP({ port: chrome.port});
+  let protocol = await CDP({ port: chrome.port });
 
-  const {Page, Runtime} = protocol;
+  const { Page, Runtime } = protocol;
 
   await Promise.all([Page.enable(), Runtime.enable()]);
 
-  Runtime.consoleAPICalled((response) => {
-    const message = response['args'][0].value;
+  Runtime.consoleAPICalled(response => {
+    const message = response["args"][0].value;
 
     // If it's a simple test result, write without newline
-    if(message === "." || message === "F"){
+    if (message === "." || message === "F") {
       process.stdout.write(message);
-    } else if (message && message.startsWith("AUTOSPEC:")) {
+    } else if (
+      message &&
+      message.startsWith &&
+      message.startsWith("AUTOSPEC:")
+    ) {
       fs.appendFileSync(QUNIT_RESULT, `${message.slice(10)}\n`);
     } else {
       console.log(message);
@@ -65,11 +64,10 @@ async function runAllTests() {
   });
 
   console.log("navigate to " + args[0]);
-  Page.navigate({url: args[0]});
+  Page.navigate({ url: args[0] });
 
   Page.loadEventFired(async () => {
-
-    await Runtime.evaluate({ expression: `(${qunit_script})()`});
+    await Runtime.evaluate({ expression: `(${qunit_script})()` });
 
     const timeout = parseInt(args[1] || 300000, 10);
     var start = Date.now();
@@ -84,9 +82,11 @@ async function runAllTests() {
         process.exit(124);
       }
 
-      let numFails = await Runtime.evaluate({expression: `(${check_script})()`});
+      let numFails = await Runtime.evaluate({
+        expression: `(${check_script})()`
+      });
 
-      if (numFails && numFails.result && numFails.result.type !== 'undefined') {
+      if (numFails && numFails.result && numFails.result.type !== "undefined") {
         clearInterval(interval);
         protocol.close();
         chrome.kill();
@@ -101,12 +101,11 @@ async function runAllTests() {
 
     interval = setInterval(runTests, 250);
   });
-
 }
 
 try {
   runAllTests();
-} catch(e) {
+} catch (e) {
   console.log("Failed to run tests: " + e);
   process.exit(1);
 }
@@ -133,7 +132,6 @@ function logQUnit() {
   let durations = {};
 
   QUnit.testDone(function(context) {
-
     durations[context.module + "::" + context.name] = context.runtime;
 
     if (context.failed) {
@@ -150,7 +148,9 @@ function logQUnit() {
   });
 
   QUnit.log(function(context) {
-    if (context.result) { return; }
+    if (context.result) {
+      return;
+    }
 
     var msg = "\n    Assertion Failed:";
     if (context.message) {
@@ -158,7 +158,8 @@ function logQUnit() {
     }
 
     if (context.expected) {
-      msg += "\n      Expected: " + context.expected + ", Actual: " + context.actual;
+      msg +=
+        "\n      Expected: " + context.expected + ", Actual: " + context.actual;
     }
 
     assertionErrors.push(msg);
@@ -168,15 +169,18 @@ function logQUnit() {
     console.log("\n");
 
     if (moduleErrors.length > 0) {
-      for (var idx=0; idx<moduleErrors.length; idx++) {
-        console.error(moduleErrors[idx]+"\n");
+      for (var idx = 0; idx < moduleErrors.length; idx++) {
+        console.error(moduleErrors[idx] + "\n");
       }
     }
 
     console.log("Slowest tests");
     console.log("----------------------------------------------");
-    let ary = Object.keys(durations).map((key) => ({ 'key': key, 'value': durations[key] }))
-    ary.sort((p1, p2) => (p2.value - p1.value));
+    let ary = Object.keys(durations).map(key => ({
+      key: key,
+      value: durations[key]
+    }));
+    ary.sort((p1, p2) => p2.value - p1.value);
     ary.slice(0, 30).forEach(pair => {
       console.log(pair.key + ": " + pair.value + "ms");
     });
@@ -189,19 +193,20 @@ function logQUnit() {
     ];
     console.log(stats.join(", "));
 
-
     window.qunitDone = context;
   });
 }
 let qunit_script = logQUnit.toString();
 
 if (QUNIT_RESULT) {
-  qunit_script = qunit_script.replace("/* QUNIT_RESULT */", "console.log(`AUTOSPEC: ${context.module}:::${context.testId}:::${context.name}`);");
-
+  qunit_script = qunit_script.replace(
+    "/* QUNIT_RESULT */",
+    "console.log(`AUTOSPEC: ${context.module}:::${context.testId}:::${context.name}`);"
+  );
 }
 
 function check() {
-  if(window.qunitDone){
+  if (window.qunitDone) {
     return window.qunitDone.failed;
   }
 }

GitHub

1 Like

@davidtaylorhq is there a reason this file is in the vendor directory? I feel like we should move it to a more obvious place like some sort of test helper directory.

2 Likes

It was in vendor because when I wrote it I published it on NPM, and we didn’t have any dependency management qunit-puppeteer - npm

The versions are probably horribly out of sync now, so moving it out of vendor makes sense :+1:

excellent, can you make it so?

Done in DEV: Move `run-qunit.js` out of the vendor directory · discourse/discourse@404acef · GitHub

2 Likes