clojure.test unable to resolve symbol from `eval`. Works in REPL and lein run - testing

Please bear with this contrived example but it was the simplest thing I could think of to recreate the issue.
(ns something.core)
(defn call-foo [something & args]
(let [a-foo (:foo (eval (:quux something)))]
(apply a-foo args)))
(def Something {
:foo (fn [& args] args)
:bar (fn [something] (call-foo something))
})
(defn make-something []
{:quux 'Something})
Running the following in the REPL or with lein run works well.
(let [subject (make-something)
actual (call-foo subject "hello" "greetings")]
(println actual))
;;=> (hello greetings)
The problem occurs only during this test and executing lein test:
(ns something.core-test
(:require [clojure.test :refer :all]
[something.core :refer :all]))
(deftest a-test
(let [subject (make-something)
actual (call-foo subject "hello" "greetings")]
(is (= ["hello" "greetings"] actual))))
This throws an error. An example output:
ERROR in (a-test) (Compiler.java:6464)
Uncaught exception, not in assertion.
expected: nil
actual: clojure.lang.Compiler$CompilerException: java.lang.RuntimeException: Unable to resolve symbol: Something in this context, compiling:(/private/var/folders/0n/c7q7860j34xfc2r1x4q51jrh0000gn/T/form-init9215140948330409114.clj:1:6436)
The line "Unable to resolve symbol: Something in this context" makes me think Something is not in context for some reason while I eval in call-foo. But why is this the case only in the test?

The problem is that eval does not see context. Your 'Something resolves in something.core and something.core-test since you have refered all. It won't resolve from whatever namespace where lein test runs its tests.
To fix the immediate problem change
'Something
to
`Something
so that it is namespace-qualified. The test will then run (and fail), but that's another issue (println returns nil for one thing).

Related

fullstack clojurescript development with Calva Jack-in

Please comment how to start backend and frontend with Calva Jack-in.
There is a clojurescript project for a web-application, I started REPL with calva jack-in (deps.edn + shadow-cljs), and tried to evaluate the function start!/stop! in the REPL. Even though there was no error message, I didn't find a corresponding response with "localhost:3000/", it was expected that "Hello, world" should be shown on browser.
To compile of both frontend and backend successfully:
$ npm install && npx shadow-cljs watch frontend backend
Start the server server with node.js successfully
$ node target/main.js
Clojurescript backend code for reference.
(ns mern.backend.core
(:require ["express" :as express]))
;; currently broken in shadow-cljs
(set! *warn-on-infer* true)
(defonce server (atom nil))
(def port 3000)
(defn start-server []
(println "Starting server")
(let [app (express)]
(.get app "/" (fn [_req res] (.send res "Hello, world")))
(.listen app port (fn [] (println "Example app listening on port 3000!")))))
(defn start! []
;; called by main and after reloading code
(reset! server (start-server)))
(defn stop! []
;; called before reloading code
(.close #server)
(reset! server nil))
(defn main []
;; executed once, on startup, can do one time setup here
(start!))

Clojure in Intelij IDEA

I opened a Clojure project in Intelij IDEA and everything had been working well until I have made new Clojure workspace and have written some Clojure code in it. When I try to execute this piece of code in REPL I get always the same error. Whatever I write, I get the same error. For example when I try to execute (+ 1 1_ I get the following error:
Syntax error compiling at (test.clj:4:1).
Unable to resolve symbol: + in this context
Has someone already faced this issue?
Here is my project.clj
(defproject test-app "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:dependencies [[cheshire "5.8.1"]
[clojure.java-time "0.3.2"]
[cprop "0.1.13"]
[funcool/struct "1.3.0"]
[luminus-immutant "0.2.5"]
[luminus-transit "0.1.1"]
[luminus/ring-ttl-session "0.3.2"]
[markdown-clj "1.0.7"]
[metosin/muuntaja "0.6.4"]
[metosin/reitit "0.3.1"]
[metosin/ring-http-response "0.9.1"]
[mount "0.1.16"]
[nrepl "0.6.0"]
[org.clojure/clojure "1.10.0"]
[org.clojure/tools.cli "0.4.2"]
[org.clojure/tools.logging "0.4.1"]
[org.webjars.npm/bulma "0.7.4"]
[org.webjars.npm/material-icons "0.3.0"]
[org.webjars/webjars-locator "0.36"]
[org.webjars/webjars-locator-jboss-vfs "0.1.0"]
[ring-webjars "0.2.0"]
[ring/ring-core "1.7.1"]
[ring/ring-defaults "0.3.2"]
[selmer "1.12.12"]]
:min-lein-version "2.0.0"
:source-paths ["src/clj"]
:test-paths ["test/clj"]
:resource-paths ["resources"]
:target-path "target/%s/"
:main ^:skip-aot test-app.core
:plugins [[lein-immutant "2.1.0"]]
:profiles
{:uberjar {:omit-source true
:aot :all
:uberjar-name "test-app.jar"
:source-paths ["env/prod/clj"]
:resource-paths ["env/prod/resources"]}
:dev [:project/dev :profiles/dev]
:test [:project/dev :project/test :profiles/test]
:project/dev {:jvm-opts ["-Dconf=dev-config.edn"]
:dependencies [[expound "0.7.2"]
[pjstadig/humane-test-output "0.9.0"]
[prone "1.6.1"]
[ring/ring-devel "1.7.1"]
[ring/ring-mock "0.3.2"]]
:plugins [[com.jakemccrary/lein-test-refresh "0.24.1"]]
:source-paths ["env/dev/clj"]
:resource-paths ["env/dev/resources"]
:repl-options {:init-ns user}
:injections [(require 'pjstadig.humane-test-output)
(pjstadig.humane-test-output/activate!)]}
:project/test {:jvm-opts ["-Dconf=test-config.edn"]
:resource-paths ["env/test/resources"]}
:profiles/dev {}
:profiles/test {}})
Everything works fine when I write code in my namespace test-app.routes.home, but when I make new Clojure namespace test.clj in the same routes folder, I cant evaluate code in REPL. When I press ctrl+Enter I get this error:
Syntax error compiling at (test.clj:4:1).
Unable to resolve symbol: + in this context
(Maybe) You need to evaluate your namespace (ns ....) before trying to evaluate any other expression.

Undefined nameToPath when using ClojureScript's :npm-deps

I'm trying to install the Ant Design library via :npm-deps, so in my project.clj I have:
:cljsbuild {:builds {:app
{:source-paths ["src" "env/dev/cljs"]
:compiler
{:main "metro-parks-cljs-components.dev"
:output-to "public/js/app.js"
:output-dir "public/js/out"
:asset-path "js/out"
:source-map true
:optimizations :none
:install-deps true
:npm-deps {:react "^16.8.5"
:react-dom "^16.8.5"
:antd "^3.15.2"
:moment "^2.24.0"}}
...
And in my app code:
(ns metro-parks-cljs-components.event-finder
(:require
...
[reagent.core :as r]
[moment]
[antd :refer [DatePicker Pagination]]))
This compiles but on page load I get:
Error: Undefined nameToPath for module$home$ctamayo$workspace$metro_parks$wp_content$themes$metro_parks$metro_parks_cljs_components$node_modules$antd$es$affix base.js:1357:21
visitNode http://localhost:3449/js/out/goog/base.js:1357
visitNode http://localhost:3449/js/out/goog/base.js:1355
visitNode http://localhost:3449/js/out/goog/base.js:1355
visitNode http://localhost:3449/js/out/goog/base.js:1355
writeScripts_ http://localhost:3449/js/out/goog/base.js:1369
require http://localhost:3449/js/out/goog/base.js:706
<anonymous> http://localhost:3449/index.html:80
Is this a known issue with npm-deps right now? Is there a workaround?
Note: I have looked into using shadow-cljs but I'm hesitant to migrate to a whole new build system due to time constraints.

Clojure :pre report failing value when destructuring

Following this SO post, I would like to print the value of the preconditions in my function. However it fails for me in the following case (probably destructuring) :
I have a dir? helper function (feel free to skip this one) :
(defn dir? [s]
"returns true if the string passed is is an existing directory"
(->> (clojure.java.io/file s)
((juxt #(.exists %) #(.isDirectory %)))
(every? true?)))
It works just fine, and using the is macro, I get some nice error messages where I can see both the test and the parameters that were passed :
(is (dir? (io/file "resources/static"))) ;; => true
(is (dir? (io/file "resources/statice"))) ;; typo, error below
FAIL in clojure.lang.PersistentList$EmptyList#1
(boot.user4515592986834245937.clj:86) expected: (dir? (io/file
"resources/statice")) actual: (not (dir? #object[java.io.File
0x6730a420 "resources/statice"]))
However, when trying to use it in the precondition :pre, I get an ugly error :
(defn make-something
[&{:keys [dir]
:or {dir "default-dir"}}]
{:pre [(is (dir? (clojure.java.io/file dir)))]}
;;... do something with these
)
(make-something :dir "resources/statices") ;; doesn't exist
clojure.lang.Compiler$CompilerException: java.lang.AssertionError:
Assert failed: (is (dir? (io/file dir))),
compiling:(boot.user4515592986834245937.clj:80:12)
java.lang.AssertionError: Assert failed: (is (dir? (io/file dir)))
How can I get a nice error message in my function just like the one above ?
In case it matters, I am using Clojure 1.7.
You need to check your code (dir? function). Following snippet works for me:
(require '[clojure.java.io :as io])
(defn dir? [s]
(let [dir (io/file s)]
(and (.exists dir)
(.isDirectory dir))))
(defn make-something
[& {:keys [dir] :or {dir "default-dir"}}]
{:pre [(is (dir? dir))]}
(println dir))
(make-something :dir "/tmp")
out => /tmp
ret => nil
(make-something :dir "/xxxx")
FAIL in clojure.lang.PersistentList$EmptyList#1 (form-init3332271312167175068.clj:1)
expected: (dir? dir)
actual: (not (dir? "/xxxx"))
AssertionError Assert failed: (is (dir? dir)) user/make-sth (form-init3332271312167175068.clj:1)

Testing static routes in compojure

I am trying to write a test for the compojure static content route.
I am testing the routes by examining the ring response directly.
A minimal working example is as follows:
;; src/testing-webapps.core.clj
(ns testing-webapps.core
(:use [compojure.core]
[compojure.route :as route]))
(defroutes web-app
(route/resources "/")
(route/not-found "404"))
;; test/testing-webapps.core_test.clj
(ns testing-webapps.core-test
(:require [clojure.test :refer :all]
[testing-webapps.core :refer :all]))
(defn request [resource web-app & params]
(web-app {:request-method :get :uri resource :params (first params)}))
(deftest test-routes
(is (= 404 (:status (request "/flubber" web-app))))
(is (= "404" (:body (request "/flubber" web-app))))
(is (= 200 (:status (request "/test.txt" web-app)))))
Testing the 404 route works fine but calling (request "/test.txt" web-app) leads to an unexpected NullPointerException in ring.middleware.file-info/not-modified-since?.
Here is the top part of the stacktrace:
ERROR in (test-routes) (file_info.clj:27)
Uncaught exception, not in assertion.
expected: nil
actual: java.lang.NullPointerException: null
at ring.middleware.file_info$not_modified_since_QMARK_.invoke (file_info.clj:27)
ring.middleware.file_info$file_info_response.doInvoke (file_info.clj:44)
clojure.lang.RestFn.invoke (RestFn.java:442)
ring.middleware.file_info$wrap_file_info$fn__917.invoke (file_info.clj:64)
[...]
The static route works fine in the browser but not when called through my request function.
Is there an easier way to test a static route in compojure and why do I get a NullPointerException when calling the static route with my own request map?
Looking at the source for not-modified-since?, I think the issue is you have no headers in your request map, so it throws a NPE on this expr: (headers "if-modified-since"). Try changing your request method like so:
(defn request [resource web-app & params]
(web-app {:request-method :get
:headers {"content-type" "text/plain"} ; added a header
:uri resource
:params (first params)}))
You might also consider using ring-mock to create requests for testing. It insulates you a little bit from stuff like this.