Last active
January 27, 2025 05:15
-
-
Save chr15m/37ecd1ceaddbd5edcfdc62fbed5a724b to your computer and use it in GitHub Desktop.
ClojureScript nbb testing with playwright example
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 testdemo | |
(:require | |
["playwright$default" :as pw] | |
[clojure.test :refer [deftest is use-fixtures async] :as t] | |
[promesa.core :as p])) | |
(def rig (atom nil)) | |
; set up playwright before running and tear it down afterwards | |
(use-fixtures | |
:once | |
{:before | |
#(async done | |
(p/let [browser (.launch pw/chromium #js {:headless true}) | |
context (.newContext browser) | |
page (.newPage context)] | |
(reset! rig {:page page :browser browser}) | |
(done))) | |
:after | |
#(async done | |
(p/let [{:keys [browser]} @rig] | |
(.close browser) | |
(done)))}) | |
; the actual example test which just loads localhost:8000 and checks the title | |
(deftest load-localhost | |
(async | |
done | |
(p/let [{:keys [page]} @rig] | |
(.goto page "http://localhost:8000") | |
(p/let [title (.title page)] | |
(is (= title "Expected Title")) | |
(done))))) | |
; handy report printing (prints the name of each test as it runs) | |
(defmethod t/report [:cljs.test/default :begin-test-var] [m] | |
(println "TEST ===>" (-> m :var meta :name))) | |
; prints a summary at the end | |
(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))) | |
; run every test in this namespace | |
(t/run-tests 'testdemo) | |
; run a smaller selection of tests instead of all of them | |
; (defn tests [#'testdemo/some-test #'testdemo/some-other-test]) | |
; (t/test-vars tests) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment