Skip to content

Testing

zalky edited this page Feb 14, 2023 · 3 revisions

The Re-frame library day8.re-frame/test provides test fixtures for running synchronous and asynchronous unit tests.

Reflet extends these via the io.zalky/reflet-test library. Simply include:

io.zalky/reflet-test {:mvn/version "0.1.0"}

in your deps.edn file. Then require the reflet.fixtures namespace to use Reflet's synchronous and asynchronous test fixtures:

(require '[reflet.fixtures :as fix]
         '[day8.re-frame.test :as rft])

(deftest my-sync-test
  (fix/run-test-sync
   ...))

(deftest my-async-test
  (fix/run-test-async
   ...
   (rft/wait-for [::timeout-success]
     ...)))

There are lots of good usage examples in the reflet.core-test and reflet.fsm-test namespaces.

References and Reactive Context

Components have no lifecycle outside a reactive context, and so things like lifecycle cleanup make no sense in unit tests.

What's more, code blocks inside a wait-for are returned and executed asynchronously outside of their originating with-ref scope, so cleanup must be avoided.

For this and other reasons, a number of behaviours are different from the running application. In unit tests, where there is no reactive context:

  1. All references created by with-ref are persistent, not transient

  2. There is no with-ref lifecycle cleanup

In the rare case when you really need to generate transient refs in unit tests, for example testing the properties of references, there is a macro in reflet.fixtures that can wrap a code block in a fake reactive context:

(fix/fake-reactive-context
  ...)

But you will rarely need this.

Queries and Reactive Context

Just like with regular Re-frame subscriptions, reg-pull queries in test fixtures run every time they are dereferenced.


Next: Advanced Usage

Home: Home