Testing in node/clojurescript

I’m struggling to find a good workflow for writing node tests in clojurescript. There are a couple of problems I’m running into that I can’ find good solutions for:

  1. Unable to run a single test at a time.
    I’m writing browser automation specs using webdriverio, which are relatively slow. So I’d like to be able to just run one test at a time (even for normal tests this would be nice). The only problem is that there doesn’t seem to be any way to do that. The recommended approach for clojure is to run test-vars, but that doesn’t seem to run fixtures in clojurescript. Even if there was a way to run one test at a time from the REPL, connecting to a repl presents its own challenges.

  2. Can’t connect to nREPL easily
    I’m using shadow-cljs for building, and so I need to run the javascript in order to connect via repl and do some repl driven development. The problem is that it’s a node-test build so when I run it, it’ll just run the tests and then exit. My current solution is to create a separate build that targets node-script, create a ns that loads all of the test namespaces, then watch / run that script so that I can connect to it via the repl in my editor. This is obviously pretty hacky and not even that reliable.

If anyone out there is writing node tests in clojurescript, I would love to hear your experience and what your workflow is.

1 Like

To follow up with this I made a library (cljs-run-test) to run a single test with fixtures in clojurescript. So my current workflow looks like this:

  1. Create a duplicate build in shadow-cljs, e.g.
{:dependencies [[org.clojars.knubie/cljs-run-test "1.0.1"]
                [binaryage/devtools               "0.9.10"]]

 :source-paths ["src"]
 :builds {:test {:target    :node-test
                 :output-to "test/main.js"
                 :ns-regexp "test"}

          :test-dev :target    :node-script
                    :output-to "test/main.js"
                    :main      test.dev/main
                    :devtools  {:preloads [devtools.preload]}}}
  1. Watch the :test-dev build.
$ shadow-cljs watch test-dev
  1. Run the node script (this is just to get the JS runtime environment for the REPL)
$ node test/main.js
  1. Connect to the REPL
$ shadow-cljs cljs-repl test-dev
shadow-cljs - connected to server
[3:1]~cljs.user=>
  1. Run individual tests.
[3:0]~cljs.user=> (require '[cljs-run-test :refer [run-test]])
[3:1]~cljs.user=> (run-test my.project.test/some-test)

Steps 4 and 5 I usually do in my editor. I use vim with vim-iced and have a keybinding to send :IcedEval (run-test *symbol-under-cursor*). I think the next step would be to run whatever test the cursor is in, instead of needing the cursor to be on the specific symbol itself, but I haven’t gotten that far yet.

Note that you don’t need a build if you just want a REPL.

shadow-cljs node-repl will give you basically the same you have setup with :test-dev.

Dunno about the vim parts but :node-repl would be the build-id you connect to.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.