Skip to content

Instantly share code, notes, and snippets.

@borkdude
Created November 9, 2021 13:37
Show Gist options
  • Save borkdude/cd9eb17b7d32a8d972441a848541debd to your computer and use it in GitHub Desktop.
Save borkdude/cd9eb17b7d32a8d972441a848541debd to your computer and use it in GitHub Desktop.
Nbb browser test runner @ Nextjournal
(ns com.nextjournal.tests.browser.runner
(:require [clojure.string :as str]
[clojure.test :as t :refer [test-vars]]
[com.nextjournal.tests.browser.article-test-nbb :as article-test]))
(defmethod t/report [:cljs.test/default :begin-test-var] [m]
(println "===" (-> m :var meta :name))
(println))
(defn print-summary []
(t/report (assoc (:report-counters (t/get-current-env)) :type :summary)))
(defmethod t/report [:cljs.test/default :end-test-vars] [_]
(let [env (t/get-current-env)
counters (:report-counters env)
failures (:fail counters)
errors (:error counters)]
(when (or (pos? failures)
(pos? errors))
(set! (.-exitCode js/process) 1))
(print-summary)))
(defn parse-opts [opts]
(let [[cmds opts] (split-with #(not (str/starts-with? % ":")) opts)]
(into {:cmds cmds}
(for [[arg-name arg-val] (partition-all 2 opts)]
[(keyword (subs arg-name 1)) arg-val]))))
(def opts (parse-opts *command-line-args*))
(when (= "false" (:headless opts))
(set! (.-headless article-test/default-launch-options) false))
(defn print-help []
(println "Subcommands:
help Print this help.
list Print list of testable vars.
run Run tests. Defaults to running all tests.
Options:
:headless (true|false) Run in headless mode, defaults to false.
:only <name from list or namespace/test> Run single test.
"))
(defn get-test-vars []
(->> (ns-publics 'com.nextjournal.tests.browser.article-test-nbb)
vals
(filter (comp :test meta))
(remove (comp :pending meta))))
(cond
(some (set (:cmds opts)) ["help" "-h" "--help"]) (print-help)
(= ["list"] (:cmds opts))
(run! (fn [var]
(println (:name (meta var))))
(get-test-vars))
(= ["run"] (:cmds opts))
(cond (:only opts)
(let [only-var (:only opts)
var-sym (symbol only-var)]
(if (qualified-symbol? var-sym)
(test-vars [(resolve (symbol only-var))])
(test-vars (filter #(= var-sym (:name (meta %))) (get-test-vars)))))
:else
(let [vars (get-test-vars)]
(println "Testing" (count vars) "vars")
(test-vars vars)))
:else (print-help))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment