clojure.test on ClojureScript?

357 views
Skip to first unread message

Shantanu Kumar

unread,
Feb 25, 2012, 11:34:29 PM2/25/12
to Clojure
Hi,

Has anybody got clojure.test working with ClojureScript? Is it planned
for clojure.test to eventually work on ClojureScript?

Shantanu

Stuart Sierra

unread,
Feb 26, 2012, 10:33:36 AM2/26/12
to clo...@googlegroups.com
Brenton Ashworth has been doing some excellent work with testing in ClojureScript One, integrating clojure.test with a browser running compiled ClojureScript.

https://github.com/brentonashworth/one/blob/master/src/lib/clj/one/test.clj

-S

Brenton

unread,
Feb 26, 2012, 1:58:26 PM2/26/12
to Clojure
Shantanu,

I have been experimenting with this in ClojureScript One. The latest
version is in the M003 branch.

The example that Stuart links to is a complex integration test. Here
is an example of some unit tests which test ClojureScript code.

https://github.com/brentonashworth/one/blob/M003/test/one/test/dispatch.clj

The idea is to keep writing tests in Clojure using clojure.test but
add the ability to easily evaluate some forms in a JavaScript
environment. This allows you to have one test suite for a Clojure/
ClojureScript project with integrated test results. The same technique
could be used with other test frameworks.

In the above example, you set up the test namespace in the same way
you would with Clojure tests.

You then have the `in-javascript` macro which sets up the
ClojureScript namespace and allows you to define functions that you
can call from ClojureScript in the tests below. All ClojureScript in
the tests below will be evaluated in the namespace that you set up
here.

In your tests, when you want to evaluate something in the JavaScript
environment, you wrap that form in the `js` macro.

If you checkout this branch you can run tests in three ways:

1) `lein test` will run all the tests as it normally would. The
current setup will launch a browser which can be used as the
evaluation environment.

2) You can run tests from a Clojure REPL:

(dev-server)
(require 'one.test)
(require 'clojure.test)
(require 'one.test.dispatch)

(def ee (one.test/browser-eval-env))

;; go to the development or fresh page

(binding [one.test/*eval-env* ee]
(clojure.test/run-tests 'one.test.dispatch))

3) If you are in a ClojureScript REPL, you can run the tests with:

(run-tests 'one.test.dispatch)

In each case you need to have a JavaScript environment in which to
evaluation things. In case 1 this is created for you. In case 2 you
have to manually set it up. In case 3 it uses the active ClojureScript
REPL's environment.

This is all very experimental. I hope it gives you some good ideas for
where to start.

Thanks,
Brenton

Shantanu Kumar

unread,
Mar 8, 2012, 2:58:46 PM3/8/12
to Clojure
Thanks Brenton and Stuart, that was very insightful.

Shantanu

On Feb 26, 11:58 pm, Brenton <bashw...@gmail.com> wrote:
> Shantanu,
>
> I have been experimenting with this in ClojureScript One. The latest
> version is in the M003 branch.
>
> The example that Stuart links to is a complex integration test. Here
> is an example of some unit tests which test ClojureScript code.
>
> https://github.com/brentonashworth/one/blob/M003/test/one/test/dispat...
Reply all
Reply to author
Forward
0 new messages