Skip to content

Commit 250a474

Browse files
committed
run-tests: scope :report-counters to the call
Previously an inner (run-tests ...) inflated the outer caller's counters and emitted a summary showing cumulative totals. Match clojure.test by saving the caller's :report-counters on entry, running the tests against fresh counters, then restoring the saved counters before returning. The returned counters map reflects only this run's tests. Smoke test gets a regression: inner run-tests must not change outer counters; its returned summary must equal its own scope.
1 parent 4e87543 commit 250a474

3 files changed

Lines changed: 32 additions & 4 deletions

File tree

bb/tasks.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
(shell "node" "node_cli.js" "compile" src)
8888
(let [out (:out (shell {:out :string} "node" out-file))]
8989
(fs/delete out-file)
90-
(assert (str/includes? out "Ran 6 tests containing 10 assertions") out)
90+
(assert (str/includes? out "Ran 7 tests containing 13 assertions") out)
9191
(assert (str/includes? out "1 failures, 0 errors") out))))
9292

9393
(defn test-squint []

src/squint/test.cljs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,24 @@
179179
((join-fixtures once-fixtures) run-all)
180180
(run-all))))
181181

182+
(def ^:private fresh-counters {:test 0 :pass 0 :fail 0 :error 0})
183+
182184
(defn run-tests
183185
"Runs tests and reports a summary. Accepts any of:
184186
- no args: run every registered test across all namespaces.
185187
- namespace name strings: run tests registered under each given ns.
186188
- explicit test fns: run those fns directly.
187189
Initializes the env if none is set. Tests are grouped by their ns
188190
(from deftest's metadata) so each ns's once-fixtures wrap that ns's
189-
tests, matching cljs.test. Returns (or resolves to, for async tests)
190-
the :report-counters summary map."
191+
tests, matching cljs.test.
192+
193+
Counters are scoped to this call: the caller's :report-counters are
194+
saved on entry and restored before return, so an inner run-tests
195+
doesn't pollute an outer run's totals (matches clojure.test, where
196+
*report-counters* is rebound per test-ns).
197+
198+
Returns (or resolves to, for async tests) the :report-counters
199+
summary map for this run."
191200
[& args]
192201
(let [test-vars (cond
193202
(empty? args)
@@ -197,6 +206,8 @@
197206
:else
198207
args)
199208
_ (when (nil? *current-env*) (set-env! (empty-env)))
209+
saved-counters (:report-counters (get-current-env))
210+
_ (update-current-env! [:report-counters] (constantly fresh-counters))
200211
;; preserve insertion order while grouping by ns
201212
groups (reduce (fn [acc v]
202213
(let [k (:ns (meta v))]
@@ -210,7 +221,9 @@
210221
nil groups))
211222
finish (fn [_]
212223
(report {:type :summary})
213-
(:report-counters (get-current-env)))
224+
(let [counters (:report-counters (get-current-env))]
225+
(update-current-env! [:report-counters] (constantly saved-counters))
226+
counters))
214227
chain (run-groups)]
215228
(if (async? chain)
216229
(.then chain finish)

test-resources/cljs_test_smoke.cljs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@
7070
"b-once-setup" "b-1" "b-once-teardown"] @log)
7171
"each ns's once-fixture wraps only that ns's tests"))))
7272

73+
(deftest run-tests-counter-isolation-test
74+
(testing "an inner run-tests doesn't disturb the caller's counters"
75+
(let [before (:report-counters (t/get-current-env))
76+
inner-result (t/run-tests
77+
(with-meta (fn [] (is true)) {:name "inner-1" :ns "x"})
78+
(with-meta (fn [] (is true)) {:name "inner-2" :ns "x"}))
79+
after (:report-counters (t/get-current-env))]
80+
(is (= before after)
81+
"outer counters are restored after run-tests returns")
82+
(is (= 2 (:test inner-result))
83+
"returned summary reflects only the inner run's tests")
84+
(is (= 2 (:pass inner-result))
85+
"returned summary reflects only the inner run's passes"))))
86+
7387
(defn ^:async -main []
7488
(t/set-env! (t/empty-env))
7589
(t/test-var math-test)
@@ -78,6 +92,7 @@
7892
(js-await (t/test-var async-test))
7993
(t/test-var per-ns-each-fixtures-test)
8094
(t/test-var per-ns-once-fixtures-test)
95+
(t/test-var run-tests-counter-isolation-test)
8196
(t/report {:type :summary}))
8297

8398
(-main)

0 commit comments

Comments
 (0)