Skip to content

Instantly share code, notes, and snippets.

@wdhowe
Last active December 18, 2025 18:39
Show Gist options
  • Select an option

  • Save wdhowe/3aa8b882a14a50e5ddb317458e46e8c6 to your computer and use it in GitHub Desktop.

Select an option

Save wdhowe/3aa8b882a14a50e5ddb317458e46e8c6 to your computer and use it in GitHub Desktop.
Clojure reflection warnings test
(ns myorg.myproject.reflection-test
"Tests for detecting reflection warnings in the codebase.
Usage:
clj -X:test :patterns '[\".*reflection.*\"]'
Lenient - prints warnings but passes
clj -X:test :patterns '[\".*reflection.*\"]' :includes '[:strict]'
Strict - fails on any warnings"
(:require [clojure.test :refer [deftest is testing]])
(:import [java.io PrintWriter StringWriter]))
(defn- collect-reflection-warnings
"Reload all namespaces with *warn-on-reflection* and capture warnings."
[]
(let [warnings (StringWriter.)]
(binding [*warn-on-reflection* true
*err* (PrintWriter. warnings)]
(require 'myorg.myproject.core :reload-all))
(str warnings)))
(deftest check-reflection-warnings
(testing "Compile all namespaces and print any reflection warnings"
(let [warnings (collect-reflection-warnings)]
(when (seq warnings)
(println "Reflection warnings:\n" warnings)))))
(deftest ^:strict check-reflection-warnings-strict
(testing "Compile all namespaces and fail if reflection warnings exist"
(let [warnings (collect-reflection-warnings)]
(is (empty? warnings)
(str "Reflection warnings found:\n" warnings)))))
;;; Pair with a :test alias in deps.edn that excludes :strict by default. ;;;
:aliases
{
;; Run tests: clj -X:test (excludes :strict tests by default)
:test {:extra-paths ["test"]
:extra-deps {org.clojure/test.check {:mvn/version "1.1.2"}
io.github.cognitect-labs/test-runner {:git/tag "v0.5.1" :git/sha "dfb30dd"}}
:exec-fn cognitect.test-runner.api/test
:exec-args {:excludes [:strict]}}
}
;;; Snippet to add to core.clj or another file to verify reflection test is working ;;;
(defn- reflection-test-trigger "No type hint; trigger reflection warning." [s]
(.length s))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment