Loading...
Loading...
Set up and write tests for Clojure and ClojureScript projects using cljs.test, cljs-init-tests, and shadow-cljs
npx skill4agent add riatzukiza/devel testing-clojure-cljs;; shadow-cljs.edn
{:source-paths ["src" "test"]
:dependencies [[cider/cider-nrepl "0.28.5"]
[cider/orchard "0.11.0"]]
:builds {:test {:target :browser-test
:output-to "target/test/test.js"
:tests {:matches #".*-test$"}
:devtools {:http-port 8080
:http-resource-root "target/test"}}
:node-test {:target :node-test
:output-to "target/test/node-test.js"
:tests {:matches #".*-test$"}}}}{
"scripts": {
"test:cljs": "shadow-cljs compile node-test && node target/test/node-test.js",
"test:browser": "shadow-cljs compile test && npx http-server target/test -p 8080",
"test:watch": "shadow-cljs watch test"
}
}(ns my-project.core-test
(:require [cljs.test :refer [deftest is testing are]]
[my-project.core :as core]))
(deftest add-test
(testing "addition"
(is (= (core/add 2 3) 5))
(is (= (core/add -1 1) 0))
(is (= (core/add 0 0) 0))))
(deftest subtract-test
(testing "subtraction"
(is (= (core/subtract 5 3) 2))
(is (= (core/subtract 3 5) -2))))
(deftest multiply-test
(testing "multiplication"
(are [x y] (= (core/multiply x y) (* x y))
2 3
-1 5
0 10)))(deftest assertion-examples
(testing "basic assertions"
(is true)
(is (= 1 1))
(is (not false)))
(testing "collection assertions"
(is (empty? []))
(is (seq [1 2 3]))
(is (contains? {:a 1} :a))
(is (contains? [1 2 3] 0)))
(testing "exception handling"
(is (thrown? js/Error
(throw (js/Error. "test")))))
(testing "approx assertions for floats"
(is (== 0.3 (+ 0.1 0.2)))))(ns my-project.async-test
(:require [cljs.test :refer [deftest is testing async]]
[my-project.async :as async]))
(deftest async-test
(async done
(async/timeout 100
(is true))
(done)))
(deftest promise-test
(async done
(-> (async/load-data)
(.then (fn [data]
(is (= (:status data) 200))
(done)))
(.catch (fn [err]
(is false "Should not error")
(done))))))cljs-init-tests(ns my-project.init-test
(:require [cljs-init-tests.core :refer [init-tests deftest-test]]
[my-project.math :as math]
[cljs.test :refer [deftest is testing]]))
;; Initialize test infrastructure
(init-tests)
;; Test definitions work normally
(deftest math-tests
(testing "basic math operations"
(is (= (math/add 2 3) 5))
(is (= (math/subtract 5 3) 2))))(ns my-project.fixtures-test
(:require [cljs.test :refer [deftest use-fixtures testing]]
[my-project.db :as db]))
;; Define fixtures
(defn setup-db [f]
(db/reset!)
(f)
(db/cleanup!))
(defn with-logging [f]
(println "Starting test")
(f)
(println "Finished test"))
;; Use fixtures
(use-fixtures :once setup-db)
(use-fixtures :each with-logging)
(deftest database-test
(testing "database operations"
(is (some? (db/connect)))
(is (db/insert {:name "test"}))))(ns my-project.cljs-specific-test
(:require [cljs.test :refer [deftest is testing]]
[cljs.core :as c]))
(deftest atom-test
(let [counter (atom 0)]
(swap! counter inc)
(is (= @counter 1))
(swap! counter inc)
(is (= @counter 2))))
(deftest reagent-test
(let [component (fn []
[:div "Hello"])]
(is (fn? component))))
(deftest protocol-test
(let [record (->Record. :field)]
(is (= (:field record) :field))))# Compile for Node.js
shadow-cljs compile node-test
# Compile for browser
shadow-cljs compile test
# Watch and test
shadow-cljs watch test#!/bin/bash
# run-cljs-tests.sh
set -e
# Install dependencies
yarn install
# Compile tests
shadow-cljs compile node-test
# Run tests
node target/test/node-test.js
# Check exit code
if [ $? -eq 0 ]; then
echo "Tests passed!"
exit 0
else
echo "Tests failed!"
exit 1
fisrc/
└── my_project/
├── core.cljs
└── core_test.cljs # Test in same namespace
test/
└── my_project/
├── integration_test.cljs
└── e2e_test.cljs;; GOOD - descriptive test names
(deftest add-two-positive-numbers-returns-sum)
(deftest handle-empty-input-gracefully)
;; BAD - vague names
(deftest test-add)
(deftest test-stuff)(deftest arithmetic-tests
(testing "addition"
(is (= (+ 2 3) 5))
(is (= (+ 0 0) 0)))
(testing "subtraction"
(is (= (- 5 3) 2))));; With test.check
(deftest sort-is-idempotent
(let [gen (gen/vector gen/int)]
(is (forall [v gen]
(= (sort v) (sort (sort v)))))))| Command | Purpose |
|---|---|
| Compile for browser |
| Compile for Node.js |
| Watch and test |
| Run tests via CLI |