Skip to content

Test & Include JavaScript Code

Sebastian Bensusan edited this page Jun 29, 2016 · 1 revision

Wether you want to test a JavaScript function/module or include it in your tests as a dependency, you can include it the same way you would in ClojureScript: by using externs and :foreign-libs.

Including Dependencies

For example, to include jQuery you would need the following config:

  :cljsbuild {
    :builds [{:id "test"
              :source-paths ["src"]
              :compiler {:main doo-with-deps.core
                         :output-to "out/testable.js"
                         :source-map-timestamp true
                         :foreign-libs [{:file "resources/vendor/jquery.js"
                                         :provides ["jquery.core"]}]}}]}

and in your test file:

(ns doo-with-deps.core
  (:require [cljs.test :refer-macros [deftest is testing]]
            [jquery.core]))

(deftest external-deps 
  (testing "When starting the application, jQuery is present"
    (is (exists? js/$))))

doo-with-deps is an example repo with this config.

Testing JavaScript code from ClojureScript

If you want to test a JavaScript function/module from ClojureScript you need the following config:

 :cljsbuild {:builds
              {:test
               {:source-paths ["src"]
                :compiler {:main "js-test-in-cljs.test.runner"
                           :foreign-libs [{:file "main.js"
                                          :provides ["js.main"]}]
                           :optimizations :none
                           :output-to "resources/public/test/js/testable.js"
                           :output-dir "resources/public/test/js"}}}}

and in your test file:

(ns js-test-in-cljs.test.core-test
  (:require js.main
            [cljs.test :refer-macros [deftest testing is are]]))


(deftest first-test
  (testing "trival"
    (is (= 9 (js/foo 3)))))

Here is a wonderful template with a complete setup showing to use test.check and ClojureScript to test a JavaScript function.

Clone this wiki locally