Skip to content
David Nolen edited this page May 21, 2015 · 34 revisions

ClojureScript now ships with a port of clojure.test in the form of cljs.test. It attempts to preserve most of the functionality provided by clojure.test along with enhancements for asynchronous testing in a single threaded environment.

Most of the functionality is provided via macros as cljs.test relies on compiler reflection and static vars to provide most of its functionality.

For example your testing ns form will probably look something like the following:

(ns my-project.tests
  (:require [cljs.test :refer-macros [deftest is testing run-tests]]))

Writing Tests

You can write a test making one assertion with deftest and is same as with clojure.test:

(deftest test-numbers
  (is (= 1 1))

Running Tests

You can run tests by writing run-tests. This may be done in your REPL or at the end of your file. If you have many test namespaces it's idiomatic to create a test runner namespace which imports all of your test namespaces and then invokes run-tests.

Fixtures

You can declare fixtures with the cljs.test/use-fixtures macro. You can declare either :once fixtures or :each fixtures. Unlike closure.test fixtures are split into two parts :before and :after. This is so that fixture will work correctly even when used asynchronously.

(use-fixtures :once
  {:before (fn [] ...)
   :after  (fn [] ...))

Async Testing

As client-side code tends to be highly asynchronous and JavaScript is single-threaded, it's important that cljs.test provided asynchronous testing support.

Clone this wiki locally