Unit testing

Shadow comes bundled with a test runner. Clojure comes bundled with a unit testing library. In this chapter, we'll implement the two and learn about testing techniques for Reframe handlers.

Unit testing#

Clojure core ships with a unit testing library called cljs.test. The tests are generally put next to the file being tested. Tests for namespace foo.bar can be defined in foo.bar-test.

The default Shadow config that ships with create-shadow-app sets up tests with devcards. We are not going to use devcards, instead we will rely on cljs.test. Let's change the :test config:

  • :target specifies the runtime where the tests should run (could also be :node-test)

  • :test-dir is the location of compiled test files

  • :devtools lets us run a server that serves a UI dashboard

Shadow considers all namespaces that :required cljs.test as tests.

Sample test namespace#

Let's set up app.core-test namespace to learn about how the tests are set up:

  • testing macro helps us define a scenario

  • deftest macro helps us define the test

  • is macro lets us assert predicates (for a test to pass, the predicate should resolve to true)

In our first-test, we asserted if 1 is equal to 1 - complex stuff!

Running tests#

You can start the test in watch mode using yarn test:watch or run it once with yarn test. The watch mode will start a test UI server along with the app server:

Test UI server on http://localhost:8016

With the server running in watch mode, the relevant tests will be re-run as you change the source code. You can also connect to the :test environment over nREPL.

Testing Reframe event handlers#

Reframe event handlers (and event subscriptions) are easy to test, since they are pure functions. But we need to modify our code a bit. Consider this event handler ::create-graphic-error. You should have it in :a.d.firebase namespace:

In a unit test, we need to check: 1. if this handler removes the key ::creating-graphic? and 2. adds the keys ::create-graphic-error with value error

But our handler is an anonymous function. We can fix this by defining the handler:


This page is a preview of Tinycanva: Clojure for React Developers

No discussions yet