ClojureScript can Test Itself

April 21, 2016

About nine months ago, bootstrap ClojureScript was born, with David Nolen's popular and apt characterization: ClojureScript can compile itself.

Now, this being a new capability, it started off with its own share of bugs that needed to be eradicated. But—surprising to me at least—the known bug count was brought to zero within a time span of only half a year or so.

Now, we are at the point where people are starting to put bootstrap ClojureScript to good use. But, one lingering issue has been that you can't easily write unit tests for code that you are targeting to this environment. Until today: support has now landed in the compiler which makes it possible to load the cljs.test namespace in bootstrap mode.

What was the holdup? The main issue (as is often the case when porting existing ClojureScript code for use in bootstrap) was macros and Java interop. In other words, some of the cljs.test macro code was making use of some facilities that only worked in Clojure.

These issues had previously been addressed in a cljs.test port for Planck. (In fact, we've subsequently learned that it is possible to simplify some aspects of such bootstrap ports.) Given that this made it possible to use Planck to execute the ClojureScript compiler's unit tests, it made sense to get this capability upstream in the ClojureScript compiler itself where it is much more broadly applicable.

The immediate benefit that we get from this is that the ClojureScript compiler now has a script that automates running its own test suite in bootstrap mode. This is important because, while regular JVM ClojureScript and bootstrap ClojureScript are extremely similar, there are subtle differences that can lead to regressions as the ClojureScript compiler codebase evolves. With this script in place, the ever-growing suite of more than 17,000 unit tests can be easily executed, catching bootstrap regressions before they ship.

If you are a library or application developer targeting the bootstrap environment, you can use this new capability to build and run your ClojureScript code and associated cljs.test tests completely within a bootstrapped environment, ensuring your code works there. A sample project that has code with tests that simultaneously targets Clojure, regular JVM-based ClojureScript, and bootstrapped ClojureScript is Titrate.

Cool! This will go a long way towards ensuring the quality of the bootstrap ClojureScript implementation along with libraries and other codebases that target bootstrap ClojureScript!

Tags: ClojureScript Bootstrap