Fork me on GitHub
#calva
<
2022-12-23
>
st08:12:19

Hi there, apologies in advance for basic question regarding test execution. I have an exemple project (used deps.edn) and clj -X:test works as expected. But, executing Calva: Run Current Test or All Tests gives:

clj꞉st.core2-test꞉> 
; Running all project tests…
; No tests found. 😱, ns: 0, vars: 0
clj꞉st.core2-test꞉> 
; Running test: test-say…
; No tests found. 😱, ns: 0, vars: 0
Any hint to what I am missing would be greatly appreciated. Thanks in advance.

pez09:12:54

It could be that the test namespaces are not loaded in the REPL.

st10:12:56

Thanks. So I need to load one by one every namespace I want to test via “run all tests”?

pez15:12:14

Well, the REPL needs to know about the tests... What you can do is to make a custom connect sequence that will require the test namespaces for you at startup. I've added an example of this here: https://github.com/BetterThanTomorrow/calva/tree/dev/test-data/projects/pirate-lang. The relevant parts are in deps.edn, where there's a :dev alias setting up :extra-paths ["env/dev"], and in env/dev/repl.clj there's an ns form requiring the test namespace (just one in this case). And then the workspace .vscode/settings.json, a connect sequence that looks like so:

"calva.replConnectSequences": [
    {
      "name": "pirate-lang",
      "projectType": "deps.edn",
      "afterCLJReplJackInCode": "(require 'repl)",
      "cljsType": "none",
      "menuSelections": {
        "cljAliases": ["dev", "test"]
      }
    }
  ]
See https://calva.io/connect-sequences/ for more on this.

st15:12:55

Thank you very much for your time. New to Calva (used to Intellij + Cursive) and trying to convert my team to adopt Clojure for our tooling.

seancorfield17:12:17

FYI: this is why I have a custom REPL snippet to require the expected test ns for a given src ns that I'm editing and then run those tests - so I would be editing st.core2 (based on your example) and hit ctrl+alt+space x and it will (require 'st.core2-test) and then call (clojure.test/run-tests st.core2-test) -- see my snippets at https://github.com/seancorfield/vscode-calva-setup/blob/develop/calva/config.edn

👏 1
bringe23:12:32

Also you may need to add the cider-nrepl middleware to your repl if it’s not included already. You can use Calva’s jack-in command for starting your repl, or copy the command it uses using the “Copy jack-in command” command and use that to start the repl. You can also add the middleware to an alias in your deps.edn. The test runner requires cider-nrepl, which Calva injects at jack-in.

👍 1
jqmtor19:01:43

I am trying out VSCode and Calva for the first time and encountered the same problem. I was a bit confused by https://calva.io/test-runner/#tests-are-not-found. When I read it, I guess I expected the work of actually requiring the namespaces to be done automatically. I realize that’s not what is said there, but it might be worth explicitly mentioning that the tests are not required? I was checking out whether there would be any reasonable way to have the REPL mimic the behaviour of the test runner in loading the tests, but I don’t think so :thinking_face:. I was using the https://github.com/cognitect-labs/test-runner, and the test selection, namespace loading and actual running of the tests seems to be concentrated https://github.com/cognitect-labs/test-runner/blob/master/src/cognitect/test_runner.clj#L62-L76. This operates under the assumption that the most common expectation for a user is that running all tests from the REPL mimics the behaviour of running them via the runner they selected for clj -X:test .

jqmtor20:01:34

I used the strategy suggested above with the REPL connect sequence and wrote this modified version of the cognitect test runner in the repl.clj file:

;; function copied as-is from:
;; 
(defn- ns-filter
  [{:keys [namespace namespace-regex]}]
  (let [[include-ns include-regexes]
        (if (or (seq namespace) (seq namespace-regex))
          [namespace namespace-regex]
          [nil [#".*\-test$"]])]
    (fn [ns]
      (or
       (get include-ns ns)
       (some #(re-matches % (name ns)) include-regexes)))))

;; function copied with modification from:
;; 
;; (the function is called test at the source)
(defn require-test-nses
  [options]
  (let [dirs  (or (:dir options)
                  #{"test"})
        nses (->> dirs
                  (map io/file)
                  (mapcat find/find-namespaces-in-dir))
        nses (filter (ns-filter options) nses)]
    (dorun (map require nses))))

(require-test-nses {})

seancorfield20:01:15

@U0A50TMJ5 Did you look at the custom REPL snippets I linked to?

seancorfield20:01:19

I tend to either want to run the tests for the current (src) ns, or the tests in the current (test) ns, or the current test under the cursor. The former snippet does require the test ns (but does not reload it), the other two do not -- because I always eval every change as I make it in any file so my REPL always has the current version of everything -- so I can be a bit more surgical about what tests get run when.

jqmtor20:01:20

Yeah, your workflow does look interesting to me, but being mostly used to my Emacs + Cider setup (mostly in Lein projects), I’d like to start by mimicking the behaviour I had, which felt “transparent” to me. I start the REPL, I ask for the tests to be run in the whole project and they do. The same for namespace or test at point. Of course, I need to know what code I need to reload in the process.

seancorfield20:01:35

Fair enough. I came from Emacs + CIDER + Leiningen too -- but more years ago (I came to VS Code via Atom, after Emacs, and switched from lein to boot in 2015 and then to the CLI and deps.edn in 2018). It's definitely taken time for my workflow to evolve to where it is now.