Skip to content

Instantly share code, notes, and snippets.

@thheller
Last active November 18, 2020 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thheller/bb6e190c6559a849cf239ea6dcfe8288 to your computer and use it in GitHub Desktop.
Save thheller/bb6e190c6559a849cf239ea6dcfe8288 to your computer and use it in GitHub Desktop.
module.exports = function (config) {
config.set({
singleRun: true,
browsers: ['ChromeHeadlessNoSandbox'],
files: ['target/karma-test.js'].concat(require("./target/files.json")),
preprocessors: {
'target/files/coverage.*.js': ['coverage']
},
frameworks: ['cljs-test'],
plugins: [
'karma-cljs-test',
'karma-chrome-launcher',
'karma-coverage'
],
colors: true,
logLevel: config.LOG_INFO,
client: {
args: ['shadow.test.karma.init']
},
customLaunchers: {
ChromeHeadlessNoSandbox: {
base: 'ChromeHeadless',
flags: ['--no-sandbox']
}
},
reporters: ['coverage'],
coverageReporter: {
reporters: [
{type: 'text'},
{type: 'text-summary'},
]
}
})
}
(ns shadow.build.targets.karma2
(:refer-clojure :exclude (compile flush resolve))
(:require
[clojure.string :as str]
[shadow.build :as build]
[shadow.build.modules :as modules]
[shadow.build.classpath :as cp]
[shadow.build.targets.browser :as browser]
[shadow.cljs.util :as util]
[hiccup.page :refer (html5)]
[clojure.java.io :as io]
[cljs.compiler :as cljs-comp]
[shadow.build.api :as build-api]
[shadow.build.output :as output]
[shadow.build.data :as data]
[shadow.build.test-util :as tu]
[clojure.data.json :as json]))
(defn configure [state mode {:keys [runner-ns output-to js-options] :or {runner-ns 'shadow.test.karma} :as config}]
(let [output-to
(io/file output-to)
output-dir
(.getParentFile output-to)]
(io/make-parents output-to)
(-> state
(tu/configure-common)
(assoc
::tu/runner-ns runner-ns
::output-to output-to)
(build-api/with-compiler-options
{:source-map true})
(build-api/with-build-options
{:output-dir output-dir
:greedy true
:dynamic-resolve true})
(build-api/with-js-options {:js-provider :shadow})
(cond->
js-options
(build-api/with-js-options js-options)
(not (get-in config [:compiler-options :output-feature-set]))
(build-api/with-compiler-options {:output-feature-set :es8}))
(build-api/configure-modules {:test {:entries []
:output-name (.getName output-to)}})
;; FIXME: maybe add devtools but given how odd karma loads js that might not be reliable
)))
;; since :configure is only called once in :dev
;; we delay setting the :entries until compile-prepare which is called every cycle
;; need to come up with a cleaner API for this
(defn test-resolve
[{::tu/keys [runner-ns] :as state} mode config]
(let [test-namespaces
(tu/find-test-namespaces state config)
entries
(-> '[shadow.test.env] ;; must be included before any deftest because of the cljs.test mod
(cond->
(= :dev mode)
(into (get-in config [:devtools :preloads])))
(into test-namespaces)
(conj runner-ns))]
#_(build/log state {:type ::test-namespaces
:test-namespaces test-namespaces
:entries entries})
(-> state
(assoc ::tu/test-namespaces test-namespaces)
(assoc-in [::modules/config :test :entries] entries)
;; re-analyze modules since we modified the entries
(modules/analyze)
(tu/inject-extra-requires)
)))
(defn flush-karma-test-file
[{::keys [output-to] :keys [polyfill-js build-options build-sources] :as state} config]
(let [prepend
(str "var shadow$provide = {};\n"
"var $jscomp = {};\n"
(output/closure-defines-and-base state)
(when (seq polyfill-js)
(str "\n" polyfill-js "\n"))
"goog.global[\"$CLJS\"] = goog.global;\n")]
(spit output-to prepend)
(let [resources
(->> build-sources
(map #(data/get-source-by-id state %))
(remove #(= "goog/base.js" (:resource-name %))))
filenames
(->> resources
(map :output-name)
(map #(str "target/files/" %))
(into []))]
(spit (io/file "target" "files.json") (json/write-str filenames))
(doseq [rc resources]
(let [{:keys [js]} (data/get-output! state rc)
out-file
(io/file "target" "files" (:output-name rc))]
(io/make-parents out-file)
(spit out-file js)))))
state)
(defn flush [state mode config]
(case mode
:dev
(flush-karma-test-file state config)
:release
(output/flush-optimized state)))
(defn process
[{::build/keys [stage mode config] :as state}]
(case stage
:configure
(configure state mode config)
:resolve
(test-resolve state mode config)
:flush
(flush state mode config)
state
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment