Search code examples
clojurescript

Does with-redefs requires multiple arities?


I executed the following code (extracted from a real use case), and expected to get "Fake 2 a b":

(defn real-func  
  ([a] (real-func a "S"))
  ([a b] (real-func a b "S")) 
  ([a b c] (println "Real " a b c)))

(defn fake-func 
  ([a b] (println "Fake 2" a b)))

(deftest blah-test
  (testing "blah blah"
    (with-redefs [real-func fake-func]  (real-func "a" "b"))))

But instead I get an error: #object[TypeError TypeError: videra_web.effects.graphql_test.real_func.cljs$core$IFn$_invoke$arity$2 is not a function]

Oddly, if I add another arity (any arity) to fake-func it works: e.g.

(defn fake-func 
  ([a b] (println "Fake 2" a b))
  ([a b c d e] (println "Fake 5" a b c d e))
)

Does this seem like a bug, or is there a language feature I don't understand?


Solution

  • You might be running the code compiled with :static-fns true which prevents such things from working.

    This defaults to true in shadow-cljs so if you are using that set :compiler-options {:static-fns false} in your build config.