Last active
April 30, 2020 08:28
-
-
Save johesoman/16d765d158e0018ed20f9aafc6d4501f to your computer and use it in GitHub Desktop.
Run just one test with shadow-cljs. I put `test_runner.cljs` in the directory `dev`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
npx shadow-cljs compile run-one-test && node ./target/run-one-test.js $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{:paths ["dev" "src" "test"] | |
:buils {:run-one-test {:output-to "target/run-one-test.js" | |
:target :node-test | |
:ns-regexp "-test$" | |
:main test-runner/run-one-test | |
:closure-defines {"goog.DEBUG" true} | |
:autorun false}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns test-runner | |
"This ns is a copied and modified version of shadow.test.node. | |
NOTE: The :dev/always ns meta is required to tell shadow-cljs to skip | |
caching for this ns so the tests-collecting macro could be invoked | |
each time." | |
{:dev/always true} | |
(:require [cljs.test :as cljs.test] | |
[clojure.string :as string] | |
[shadow.test :as shadow.test] | |
[shadow.test.env :as shadow.test.env])) | |
(defn get-name-of-var [var] | |
(let [m (meta var)] | |
(str (:ns m) "/" (:name m)))) | |
(defn get-single-test-data-pairs [] | |
(let [test-data-map (shadow.test.env/get-test-data)] | |
(for [[namespace-name test-data] test-data-map | |
:let [{:keys [vars]} test-data] | |
var vars] | |
[(get-name-of-var var) {namespace-name (assoc test-data | |
:vars | |
[var])}]))) | |
(defn filter-test-data-pairs-by-name [name-predicate test-data-pairs] | |
(filter (comp name-predicate first) test-data-pairs)) | |
(defn make-includes-every-predicate [substrings-to-match] | |
(let [matcher-fns (map #(fn [test-name] (string/includes? test-name %1)) | |
substrings-to-match) | |
matcher-fn (apply juxt matcher-fns)] | |
(fn [test-name] | |
(every? true? (matcher-fn test-name))))) | |
(defn search-single-test-data-pairs-using-substrings [& test-name-substrings] | |
(let [test-data-pairs (get-single-test-data-pairs) | |
test-data-pairs (filter-test-data-pairs-by-name | |
(make-includes-every-predicate test-name-substrings) | |
test-data-pairs) | |
test-data-pairs (sort-by first test-data-pairs)] | |
test-data-pairs)) | |
(defmethod cljs.test/report [::cljs.test/default :end-run-tests] [m] | |
(if (cljs.test/successful? m) | |
(js/process.exit 0) | |
(js/process.exit 1))) | |
(defmethod cljs.test/report [::cljs.test/default :begin-test-var] [m] | |
(println ">>>>>>>> Testing" (-> m :var meta :name))) | |
(defn run-test-data [test-data] | |
(shadow.test.env/reset-test-data! test-data) | |
(shadow.test/run-all-tests)) | |
(defn run-one-test [& args] | |
(if-not (seq args) | |
(println "Please provide at least one substring that I can search for :)") | |
(let [test-data-pairs (apply search-single-test-data-pairs-using-substrings | |
args)] | |
(cond | |
(= 0 (count test-data-pairs)) | |
(println "No matching tests found :(") | |
(= 1 (count test-data-pairs)) | |
(let [[test-name test-data] (first test-data-pairs)] | |
(println "I found just one matching test:") | |
(println " " test-name) | |
(run-test-data test-data)) | |
:else | |
(do | |
(println "I found more than one matching tests. To fix this, try to narrow your search :)") | |
(println "Tip: to narrow you search, add another substring to the command line args.") | |
(println "Here's a list of the tests I found:") | |
(doseq [[test-name _] test-data-pairs] | |
(println " " test-name))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment