Liking cljdoc? Tell your friends :D

cljs.test

A unit testing framework.

ASSERTIONS

The core of the library is the "is" macro, which lets you make assertions of any arbitrary expression:

(is (= 4 (+ 2 2))) (is (instance? Integer 256)) (is (.startsWith "abcde" "ab"))

You can type an "is" expression directly at the REPL, which will print a message if it fails.

user> (is (= 5 (+ 2 2)))

FAIL in  (:1)
expected: (= 5 (+ 2 2))
  actual: (not (= 5 4))
false

The "expected:" line shows you the original expression, and the "actual:" shows you what actually happened. In this case, it shows that (+ 2 2) returned 4, which is not = to 5. Finally, the "false" on the last line is the value returned from the expression. The "is" macro always returns the result of the inner expression.

There are two special assertions for testing exceptions. The "(is (thrown? c ...))" form tests if an exception of class c is thrown:

(is (thrown? ArithmeticException (/ 1 0)))

"(is (thrown-with-msg? c re ...))" does the same thing and also tests that the message on the exception matches the regular expression re:

(is (thrown-with-msg? ArithmeticException #"Divide by zero" (/ 1 0)))

DOCUMENTING TESTS

"is" takes an optional second argument, a string describing the assertion. This message will be included in the error report.

(is (= 5 (+ 2 2)) "Crazy arithmetic")

In addition, you can document groups of assertions with the "testing" macro, which takes a string followed by any number of assertions. The string will be included in failure reports. Calls to "testing" may be nested, and all of the strings will be joined together with spaces in the final report, in a style similar to RSpec http://rspec.info/

(testing "Arithmetic" (testing "with positive integers" (is (= 4 (+ 2 2))) (is (= 7 (+ 3 4)))) (testing "with negative integers" (is (= -4 (+ -2 -2))) (is (= -1 (+ 3 -4)))))

Note that, unlike RSpec, the "testing" macro may only be used INSIDE a "deftest" form (see below).

DEFINING TESTS

(deftest addition (is (= 4 (+ 2 2))) (is (= 7 (+ 3 4))))

(deftest subtraction (is (= 1 (- 4 3))) (is (= 3 (- 7 4))))

This creates functions named "addition" and "subtraction", which can be called like any other function. Therefore, tests can be grouped and composed, in a style similar to the test framework in Peter Seibel's "Practical Common Lisp" http://www.gigamonkeys.com/book/practical-building-a-unit-test-framework.html

(deftest arithmetic (addition) (subtraction))

The names of the nested tests will be joined in a list, like "(arithmetic addition)", in failure reports. You can use nested tests to set up a context shared by several tests.

DEFINING ASYNC TESTS

(deftest addition (async done (is (= 4 (+ 2 2))) (is (= 7 (+ 3 4))) (done)))

Async tests are constructed with the async macro. The first argument to the macro is the test completion callback. The body of the async macro may be any series of expressions. The completion callback must be invoked when all assertions have run. There is no support for asynchronous coordination - core.async is recommended for this. Note the body of the async test must be truly asynchronous to avoid stack overflow.

RUNNING TESTS

Run tests with the function "(run-tests namespaces...)":

(run-tests 'your.namespace 'some.other.namespace)

If you don't specify any namespaces, the current namespace is used. To run all tests in all namespaces, use "(run-all-tests)".

By default, these functions will search for all tests defined in a namespace and run them in an undefined order. However, if you are composing tests, as in the "arithmetic" example above, you probably do not want the "addition" and "subtraction" tests run separately. In that case, you must define a special function named "test-ns-hook" that runs your tests in the correct order:

(defn test-ns-hook [] (arithmetic))

"run-tests" also optionally takes a testing enviroment. A default one is supplied for you by invoking "empty-env". The test environment contains everything needed to run tests including the report results map. Fixtures must be present here if you want them to run. Note that code that relies on "test-ns" will automatically be supplied the appropriate defined fixtures. For example, this is done for you if you use "run-tests".

Note: test-ns-hook prevents execution of fixtures (see below).

OMITTING TESTS FROM PRODUCTION CODE

You can set the ClojureScript compiler build option ":load-tests" to false when loading or compiling code in production. This will prevent any tests from being created by or "deftest".

FIXTURES

Fixtures allow you to run code before and after tests, to set up the context in which tests should be run.

A fixture is a map of one or two functions that run code before and after tests. It looks like this:

{:before (fn [] Perform setup, establish bindings, whatever. ) :after (fn [] Tear-down / clean-up code here. )}

Both are optional and can be left out.

Fixtures are attached to namespaces in one of two ways. "each" fixtures are run repeatedly, once for each test function created with "deftest". "each" fixtures are useful for establishing a consistent before/after state for each test, like clearing out database tables.

"each" fixtures can be attached to the current namespace like this: (use-fixtures :each fixture1 fixture2 ...) The fixture1, fixture2 are just maps like the example above. They can also be passed directly, like this: (use-fixtures :each {:before (fn [] setup...), :after (fn [] cleanup...)})

The other kind of fixture, a "once" fixture, is only run once, around ALL the tests in the namespace. "once" fixtures are useful for tasks that only need to be performed once, like establishing database connections, or for time-consuming tasks.

Attach "once" fixtures to the current namespace like this: (use-fixtures :once fixture1 fixture2 ...)

Note: Fixtures and test-ns-hook are mutually incompatible. If you are using test-ns-hook, fixture functions will never be run.

WRAPPING FIXTURES

Instead of a map, a fixture can be specified like this:

(defn my-fixture [f] Perform setup, establish bindings, whatever. (f) Then call the function we were passed. Tear-down / clean-up code here. )

This style is incompatible with async tests. If an async test is encountered, testing will be aborted. It can't be mixed with fixtures specified as maps.

EXTENDING TEST-IS (ADVANCED)

You can extend the behavior of the "is" macro by defining new methods for the "assert-expr" multimethod. These methods are called during expansion of the "is" macro, so they should return quoted forms to be evaluated.

You can plug in your own test-reporting framework by specifying a :reporter key in the test environment. It is normally set to :cljs.test/default. Set this to the desired key and supply custom implementations of the "report" multimethod.

The 'event' argument is a map. It will always have a :type key, whose value will be a keyword signaling the type of event being reported. Standard events with :type value of :pass, :fail, and :error are called when an assertion passes, fails, and throws an exception, respectively. In that case, the event will also have the following keys:

:expected The form that was expected to be true :actual A form representing what actually occurred :message The string message given as an argument to 'is'

The "testing" strings will be a list in the :testing-contexts property of the test environment, and the vars being tested will be a list in the :testing-vars property of the test environment.

For additional event types, see the examples in the code.

A unit testing framework.

ASSERTIONS

The core of the library is the "is" macro, which lets you make
assertions of any arbitrary expression:

(is (= 4 (+ 2 2)))
(is (instance? Integer 256))
(is (.startsWith "abcde" "ab"))

You can type an "is" expression directly at the REPL, which will
print a message if it fails.

    user> (is (= 5 (+ 2 2)))

    FAIL in  (:1)
    expected: (= 5 (+ 2 2))
      actual: (not (= 5 4))
    false

The "expected:" line shows you the original expression, and the
"actual:" shows you what actually happened.  In this case, it
shows that (+ 2 2) returned 4, which is not = to 5.  Finally, the
"false" on the last line is the value returned from the
expression.  The "is" macro always returns the result of the
inner expression.

There are two special assertions for testing exceptions.  The
"(is (thrown? c ...))" form tests if an exception of class c is
thrown:

(is (thrown? ArithmeticException (/ 1 0))) 

"(is (thrown-with-msg? c re ...))" does the same thing and also
tests that the message on the exception matches the regular
expression re:

(is (thrown-with-msg? ArithmeticException #"Divide by zero"
                      (/ 1 0)))

DOCUMENTING TESTS

"is" takes an optional second argument, a string describing the
assertion.  This message will be included in the error report.

(is (= 5 (+ 2 2)) "Crazy arithmetic")

In addition, you can document groups of assertions with the
"testing" macro, which takes a string followed by any number of
assertions.  The string will be included in failure reports.
Calls to "testing" may be nested, and all of the strings will be
joined together with spaces in the final report, in a style
similar to RSpec <http://rspec.info/>

(testing "Arithmetic"
  (testing "with positive integers"
    (is (= 4 (+ 2 2)))
    (is (= 7 (+ 3 4))))
  (testing "with negative integers"
    (is (= -4 (+ -2 -2)))
    (is (= -1 (+ 3 -4)))))

Note that, unlike RSpec, the "testing" macro may only be used
INSIDE a "deftest" form (see below).


DEFINING TESTS

(deftest addition
  (is (= 4 (+ 2 2)))
  (is (= 7 (+ 3 4))))

(deftest subtraction
  (is (= 1 (- 4 3)))
  (is (= 3 (- 7 4))))

This creates functions named "addition" and "subtraction", which
can be called like any other function.  Therefore, tests can be
grouped and composed, in a style similar to the test framework in
Peter Seibel's "Practical Common Lisp"
<http://www.gigamonkeys.com/book/practical-building-a-unit-test-framework.html>

(deftest arithmetic
  (addition)
  (subtraction))

The names of the nested tests will be joined in a list, like
"(arithmetic addition)", in failure reports.  You can use nested
tests to set up a context shared by several tests.

DEFINING ASYNC TESTS

(deftest addition
  (async done
    (is (= 4 (+ 2 2)))
    (is (= 7 (+ 3 4)))
    (done)))

Async tests are constructed with the async macro. The first argument to
the macro is the test completion callback. The body of the async macro may
be any series of expressions. The completion callback must be invoked when
all assertions have run. There is no support for asynchronous coordination -
core.async is recommended for this. Note the body of the async test must be
truly asynchronous to avoid stack overflow.

RUNNING TESTS

Run tests with the function "(run-tests namespaces...)":

(run-tests 'your.namespace 'some.other.namespace)

If you don't specify any namespaces, the current namespace is
used.  To run all tests in all namespaces, use "(run-all-tests)".

By default, these functions will search for all tests defined in
a namespace and run them in an undefined order.  However, if you
are composing tests, as in the "arithmetic" example above, you
probably do not want the "addition" and "subtraction" tests run
separately.  In that case, you must define a special function
named "test-ns-hook" that runs your tests in the correct order:

(defn test-ns-hook []
  (arithmetic))

"run-tests" also optionally takes a testing enviroment. A default
one is supplied for you by invoking "empty-env".  The test
environment contains everything needed to run tests including the
report results map. Fixtures must be present here if you want them
to run. Note that code that relies on "test-ns" will
automatically be supplied the appropriate defined fixtures.  For
example, this is done for you if you use "run-tests".

Note: test-ns-hook prevents execution of fixtures (see below).


OMITTING TESTS FROM PRODUCTION CODE

You can set the ClojureScript compiler build option
":load-tests" to false when loading or compiling code in
production.  This will prevent any tests from being created by
or "deftest".


FIXTURES

Fixtures allow you to run code before and after tests, to set up
the context in which tests should be run.

A fixture is a map of one or two functions that run code before and
after tests.  It looks like this:

{:before (fn []
           Perform setup, establish bindings, whatever.
           )
 :after (fn []
          Tear-down / clean-up code here.
          )}

Both are optional and can be left out.

Fixtures are attached to namespaces in one of two ways.  "each"
fixtures are run repeatedly, once for each test function created
with "deftest".  "each" fixtures are useful for
establishing a consistent before/after state for each test, like
clearing out database tables.

"each" fixtures can be attached to the current namespace like this:
(use-fixtures :each fixture1 fixture2 ...)
The fixture1, fixture2 are just maps like the example above.
They can also be passed directly, like this:
(use-fixtures :each
  {:before (fn [] setup...), :after (fn [] cleanup...)})

The other kind of fixture, a "once" fixture, is only run once,
around ALL the tests in the namespace.  "once" fixtures are useful
for tasks that only need to be performed once, like establishing
database connections, or for time-consuming tasks.

Attach "once" fixtures to the current namespace like this:
(use-fixtures :once fixture1 fixture2 ...)

Note: Fixtures and test-ns-hook are mutually incompatible.  If you
are using test-ns-hook, fixture functions will *never* be run.


WRAPPING FIXTURES

Instead of a map, a fixture can be specified like this:

(defn my-fixture [f]
   Perform setup, establish bindings, whatever.
  (f)  Then call the function we were passed.
   Tear-down / clean-up code here.
 )

This style is incompatible with async tests. If an async test is
encountered, testing will be aborted. It can't be mixed with
fixtures specified as maps.


EXTENDING TEST-IS (ADVANCED)

You can extend the behavior of the "is" macro by defining new
methods for the "assert-expr" multimethod.  These methods are
called during expansion of the "is" macro, so they should return
quoted forms to be evaluated.

You can plug in your own test-reporting framework by specifying a
:reporter key in the test environment. It is normally set to
:cljs.test/default. Set this to the desired key and supply custom
implementations of the "report" multimethod.

The 'event' argument is a map.  It will always have a :type key,
whose value will be a keyword signaling the type of event being
reported.  Standard events with :type value of :pass, :fail, and
:error are called when an assertion passes, fails, and throws an
exception, respectively.  In that case, the event will also have
the following keys:

  :expected   The form that was expected to be true
  :actual     A form representing what actually occurred
  :message    The string message given as an argument to 'is'

The "testing" strings will be a list in the :testing-contexts
property of the test environment, and the vars being tested will be
a list in the :testing-vars property of the test environment.

For additional event types, see the examples in the code.
raw docstring

*current-env*cljs

source

arecljsmacro

(are argv expr & args)

Checks multiple assertions with a template expression. See clojure.template/do-template for an explanation of templates.

Example: (are [x y] (= x y)
2 (+ 1 1) 4 (* 2 2)) Expands to: (do (is (= 2 (+ 1 1))) (is (= 4 (* 2 2))))

Note: This breaks some reporting features, such as line numbers.

Checks multiple assertions with a template expression.
See clojure.template/do-template for an explanation of
templates.

Example: (are [x y] (= x y)  
              2 (+ 1 1)
              4 (* 2 2))
Expands to: 
         (do (is (= 2 (+ 1 1)))
             (is (= 4 (* 2 2))))

Note: This breaks some reporting features, such as line numbers.
sourceraw docstring

asynccljsmacro

(async done & body)

Wraps body as a CPS function that can be returned from a test to continue asynchronously. Binds done to a function that must be invoked once and from an async context after any assertions.

(deftest example-with-timeout (async done (js/setTimeout (fn [] ;; make assertions in async context... (done) ;; ...then call done ) 0)))

Wraps body as a CPS function that can be returned from a test to
continue asynchronously.  Binds done to a function that must be
invoked once and from an async context after any assertions.

(deftest example-with-timeout
  (async done
    (js/setTimeout (fn []
                     ;; make assertions in async context...
                     (done) ;; ...then call done
                     )
                   0)))
sourceraw docstring

async?cljs

(async? x)

Returns whether x implements IAsyncTest.

Returns whether x implements IAsyncTest.
sourceraw docstring

blockcljs

(block fns)

Tag a seq of fns to be picked up by run-block as injected continuation. See run-block.

Tag a seq of fns to be picked up by run-block as injected
continuation.  See run-block.
sourceraw docstring

clear-env!cljs

(clear-env!)
source

compose-fixturescljs

(compose-fixtures f1 f2)

Composes two fixture functions, creating a new fixture function that combines their behavior.

NOTE: Incompatible with map fixtures.

Composes two fixture functions, creating a new fixture function
that combines their behavior.

NOTE: Incompatible with map fixtures.
sourceraw docstring

deftestcljsmacro

(deftest name & body)

Defines a test function with no arguments. Test functions may call other tests, so tests may be composed. If you compose tests, you should also define a function named test-ns-hook; run-tests will call test-ns-hook instead of testing all vars.

Note: Actually, the test body goes in the :test metadata on the var, and the real function (the value of the var) calls test-var on itself.

When cljs.analyzer/load-tests is false, deftest is ignored.

Defines a test function with no arguments.  Test functions may call
other tests, so tests may be composed.  If you compose tests, you
should also define a function named test-ns-hook; run-tests will
call test-ns-hook instead of testing all vars.

Note: Actually, the test body goes in the :test metadata on the var,
and the real function (the value of the var) calls test-var on
itself.

When cljs.analyzer/*load-tests* is false, deftest is ignored.
sourceraw docstring

do-reportcljs

(do-report m)
source

empty-envcljs

(empty-env)
(empty-env reporter)

Generates a testing environment with a reporter. (empty-env) - uses the :cljs.test/default reporter. (empty-env :cljs.test/pprint) - pretty prints all data structures. (empty-env reporter) - uses a reporter of your choosing.

To create your own reporter see cljs.test/report

Generates a testing environment with a reporter.
(empty-env) - uses the :cljs.test/default reporter.
(empty-env :cljs.test/pprint) - pretty prints all data structures. 
(empty-env reporter) - uses a reporter of your choosing.

To create your own reporter see cljs.test/report
sourceraw docstring

file-and-linecljs

(file-and-line exception depth)
source

get-and-clear-env!cljs

(get-and-clear-env!)
source

get-current-envcljs

(get-current-env)
source

IAsyncTestcljsprotocol

Marker protocol denoting CPS function to begin asynchronous testing.

Marker protocol denoting CPS function to begin asynchronous
testing.
sourceraw docstring

inc-report-counter!cljs

(inc-report-counter! name)

Increments the named counter in report-counters, a ref to a map. Does nothing if report-counters is nil.

Increments the named counter in *report-counters*, a ref to a map.
Does nothing if *report-counters* is nil.
sourceraw docstring

iscljsmacro

(is form)
(is form msg)

Generic assertion macro. 'form' is any predicate test. 'msg' is an optional message to attach to the assertion.

Example: (is (= 4 (+ 2 2)) "Two plus two should be 4")

Special forms:

(is (thrown? c body)) checks that an instance of c is thrown from body, fails if not; then returns the thing thrown.

(is (thrown-with-msg? c re body)) checks that an instance of c is thrown AND that the message on the exception matches (with re-find) the regular expression re.

Generic assertion macro.  'form' is any predicate test.
'msg' is an optional message to attach to the assertion.

Example: (is (= 4 (+ 2 2)) "Two plus two should be 4")

Special forms:

(is (thrown? c body)) checks that an instance of c is thrown from
body, fails if not; then returns the thing thrown.

(is (thrown-with-msg? c re body)) checks that an instance of c is
thrown AND that the message on the exception matches (with
re-find) the regular expression re.
sourceraw docstring

join-fixturescljs

(join-fixtures fixtures)

Composes a collection of fixtures, in order. Always returns a valid fixture function, even if the collection is empty.

NOTE: Incompatible with map fixtures.

Composes a collection of fixtures, in order.  Always returns a valid
fixture function, even if the collection is empty.

NOTE: Incompatible with map fixtures.
sourceraw docstring

js-filenamecljs

(js-filename stack-element)
source

js-line-and-columncljs

(js-line-and-column stack-element)
source

mapped-line-and-columncljs

(mapped-line-and-column filename line column)
source

reportcljsmultimethod

Generic reporting function, may be overridden to plug in different report formats (e.g., TAP, JUnit). Assertions such as 'is' call 'report' to indicate results. The argument given to 'report' will be a map with a :type key.

Generic reporting function, may be overridden to plug in
different report formats (e.g., TAP, JUnit).  Assertions such as
'is' call 'report' to indicate results.  The argument given to
'report' will be a map with a :type key.
sourceraw docstring

run-all-testscljsmacro

(run-all-tests)
(run-all-tests re)
(run-all-tests re env)

Runs all tests in all namespaces; prints results. Optional argument is a regular expression; only namespaces with names matching the regular expression (with re-matches) will be tested.

Runs all tests in all namespaces; prints results.
Optional argument is a regular expression; only namespaces with
names matching the regular expression (with re-matches) will be
tested.
sourceraw docstring

run-blockcljs

(run-block fns)

Invoke all functions in fns with no arguments. A fn can optionally return

an async test - is invoked with a continuation running left fns

a seq of fns tagged per block - are invoked immediately after fn

Invoke all functions in fns with no arguments. A fn can optionally
return

an async test - is invoked with a continuation running left fns

a seq of fns tagged per block - are invoked immediately after fn
sourceraw docstring

run-testscljsmacro

(run-tests)
(run-tests env-or-ns)
(run-tests env-or-ns & namespaces)

Runs all tests in the given namespaces; prints results. Defaults to current namespace if none given. Does not return a meaningful value due to the possiblity of asynchronous execution. To detect test completion add a :end-run-tests method case to the cljs.test/report multimethod.

Runs all tests in the given namespaces; prints results.
Defaults to current namespace if none given. Does not return a meaningful
value due to the possiblity of asynchronous execution. To detect test
completion add a :end-run-tests method case to the cljs.test/report
multimethod.
sourceraw docstring

run-tests-blockcljsmacro

(run-tests-block env-or-ns & namespaces)

Like test-vars, but returns a block for further composition and later execution.

Like test-vars, but returns a block for further composition and
later execution.
sourceraw docstring

set-env!cljs

(set-env! new-env)
source

successful?cljs

(successful? summary)

Returns true if the given test summary indicates all tests were successful, false otherwise.

Returns true if the given test summary indicates all tests
were successful, false otherwise.
sourceraw docstring

test-all-varscljsmacro

(test-all-vars [quote ns :as form])

Calls test-vars on every var with :test metadata interned in the namespace, with fixtures.

Calls test-vars on every var with :test metadata interned in the
namespace, with fixtures.
sourceraw docstring

test-all-vars-blockcljsmacro

(test-all-vars-block [quote ns])
source

test-nscljsmacro

(test-ns ns)
(test-ns env [quote ns :as form])

If the namespace defines a function named test-ns-hook, calls that. Otherwise, calls test-all-vars on the namespace. 'ns' is a namespace object or a symbol.

Internally binds report-counters to a ref initialized to initial-report-counters.

If the namespace defines a function named test-ns-hook, calls that.
Otherwise, calls test-all-vars on the namespace.  'ns' is a
namespace object or a symbol.

Internally binds *report-counters* to a ref initialized to
*initial-report-counters*.  
sourceraw docstring

test-ns-blockcljsmacro

(test-ns-block env [quote ns :as form])

Like test-ns, but returns a block for further composition and later execution. Does not clear the current env.

Like test-ns, but returns a block for further composition and
later execution.  Does not clear the current env.
sourceraw docstring

test-varcljs

(test-var v)

If v has a function in its :test metadata, calls that function, add v to :testing-vars property of env.

If v has a function in its :test metadata, calls that function,
add v to :testing-vars property of env.
sourceraw docstring

test-var-blockcljs

(test-var-block v)

Like test-var, but returns a block for further composition and later execution.

Like test-var, but returns a block for further composition and
later execution.
sourceraw docstring

test-varscljs

(test-vars vars)

Groups vars by their namespace and runs test-vars on them with appropriate fixtures assuming they are present in the current testing environment.

Groups vars by their namespace and runs test-vars on them with
appropriate fixtures assuming they are present in the current
testing environment.
sourceraw docstring

test-vars-blockcljs

(test-vars-block vars)

Like test-vars, but returns a block for further composition and later execution.

Like test-vars, but returns a block for further composition and
later execution.
sourceraw docstring

testingcljsmacro

(testing string & body)

Adds a new string to the list of testing contexts. May be nested, but must occur inside a test function (deftest).

Adds a new string to the list of testing contexts.  May be nested,
but must occur inside a test function (deftest).
sourceraw docstring

testing-contexts-strcljs

(testing-contexts-str)

Returns a string representation of the current test context. Joins strings in testing-contexts with spaces.

Returns a string representation of the current test context. Joins
strings in *testing-contexts* with spaces.
sourceraw docstring

testing-vars-strcljs

(testing-vars-str m)

Returns a string representation of the current test. Renders names in testing-vars as a list, then the source file and line of current assertion.

Returns a string representation of the current test.  Renders names
in *testing-vars* as a list, then the source file and line of
current assertion.
sourceraw docstring

try-exprcljsmacro

(try-expr msg form)

Used by the 'is' macro to catch unexpected exceptions. You don't call this.

Used by the 'is' macro to catch unexpected exceptions.
You don't call this.
sourceraw docstring

update-current-env!cljs

(update-current-env! ks f & args)
source

use-fixturescljsmacro

(use-fixtures type & fns)
source

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close