来自
https://gist.github.com/borkdude/a7c38295f19a0dd2a68a7da21d634661
;; 运行方法
;; clj -Srepro -Sdeps '{:deps {org.clojure/clojurescript {:git/url "
https://github.com/clojure/clojurescript" :sha "4fb83eff87cc456600a3fd21c111e99a41c61285"}}}' -m cljs.main -re node -i repro_test.cljs
(ns repro-test
(:require
[clojure.spec.alpha :as s]
[clojure.spec.test.alpha :as stest]
[clojure.test :as t :refer [deftest is testing]]))
(defn foo [n]
"ret")
(s/fdef foo
:args (s/cat :n number?)
:ret number?)
(deftest repro-test
(testing "finally块中的unstrument工作正常"
(is (= "oops"
(try
(stest/instrument `foo)
(foo "string")
(catch js/Error e "oops")
(finally
(stest/unstrument `foo))))))
(testing "应该在try/catch/finally后面uninstrument化")
(let [ret (try (foo "string")
(catch js/Error e "not-ret"))]
(is (= "ret" ret)) ;; 失败
(testing "如果ret不是ret,则foo仍然被instrument。正在uninstrument化..."
(is (seq (s/uninstrument `foo))) ;; 失败
))))
(t/run-tests)
这些测试在Clojure中通过,但在ClojureScript中没有通过。
可能是因为宏扩展时期的副作用。
Mike Fikes在#cljs-dev Slack中指出,Clojure与ClojureScript在分析finally块时的顺序不同。
我在为Clojure和ClojureScript实现with-instrumentation和with-uninstrumentation宏时遇到了这个问题。它们在Clojure中按预期工作,但在ClojureScript中表现出上述问题。