├── .gitignore ├── README.md ├── images └── clj-test-for-js.jpg ├── project.clj ├── resources └── public │ └── figwheel.html └── src ├── js_test_in_cljs └── test │ ├── core_test.cljs │ └── runner.cljs └── main.js /.gitignore: -------------------------------------------------------------------------------- 1 | pom.xml 2 | pom.xml.asc 3 | *jar 4 | /lib/ 5 | /classes/ 6 | /target/ 7 | /checkouts/ 8 | .lein-deps-sum 9 | .lein-repl-history 10 | .lein-plugins/ 11 | .lein-failures 12 | .nrepl-port 13 | figwheel_server.log 14 | resources/public/fig/* 15 | resources/public/test/* 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rationale 2 | 3 | `Clojurescript` provides powerful tools and semantics for testing code like `deftest` and `test.check` (generative testing). 4 | This repository is a template for testing `javascript` code with `clojurescript` code. 5 | 6 | 7 | # Structure 8 | 9 | There is a `javascript` file `main.js` that defines a `javascript` function `foo`. 10 | 11 | There is a `clojurescript` file `core_test.cljs` that tests `foo`. 12 | 13 | ![core_test.cljs](https://raw.githubusercontent.com/viebel/template-test-js-with-clj/master/images/clj-test-for-js.jpg) 14 | 15 | In `project.clj`, `main.js` is defined as a `:foreign-libs`, so we can `:require` it from `clojurescript`. 16 | 17 | The tests are run with [lein doo](https://github.com/bensu/doo). 18 | 19 | Simple, no? 20 | 21 | # How to run 22 | ```bash 23 | lein doo phantom test 24 | ``` 25 | 26 | Modify the code of the test inside `src/js_test_in_cljs/test/core_test.cljs` and the tests re-run automatically in under 1 second. 27 | 28 | 29 | -------------------------------------------------------------------------------- /images/clj-test-for-js.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/viebel/template-test-js-with-clj/a20c7e856f2bfa6218eef6d5c8c5c252a2ef98b3/images/clj-test-for-js.jpg -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject js-test-in-cljs "0.0.1" 2 | :description "Test Javascript code in clojurescript" 3 | :dependencies [[org.clojure/clojure "1.8.0"] 4 | [org.clojure/clojurescript "1.9.36"] 5 | [org.clojure/test.check "0.9.0"]] 6 | :clean-targets ^{:protect false} ["resources/public/test/js"] 7 | :plugins [[lein-doo "0.1.6"]] 8 | :source-paths ["src"] 9 | :cljsbuild {:builds 10 | {:test 11 | {:source-paths ["src"] 12 | :compiler 13 | {:main "js-test-in-cljs.test.runner" 14 | :foreign-libs [{:file "main.js" 15 | :provides ["js.main"]}] 16 | :optimizations :none 17 | :output-to "resources/public/test/js/testable.js" 18 | :output-dir "resources/public/test/js"}}}}) 19 | -------------------------------------------------------------------------------- /resources/public/figwheel.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Figwheel 6 | 7 | 8 |

Figwheel

9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/js_test_in_cljs/test/core_test.cljs: -------------------------------------------------------------------------------- 1 | (ns js-test-in-cljs.test.core-test 2 | (:require 3 | js.main 4 | [clojure.test.check :as tc] 5 | [clojure.test.check.generators :as gen] 6 | [clojure.test.check.properties :as prop :include-macros true] 7 | [clojure.test.check.clojure-test :as ct :refer-macros [defspec]] 8 | [cljs.test :refer-macros [deftest testing is are]])) 9 | 10 | 11 | (deftest first-test 12 | (testing "trival" 13 | (is (= 9 (js/foo 3))))) 14 | 15 | 16 | (defspec check-foo 200 17 | (prop/for-all [n gen/int] 18 | (= (* n n) (js/foo n)))) 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/js_test_in_cljs/test/runner.cljs: -------------------------------------------------------------------------------- 1 | (ns js-test-in-cljs.test.runner 2 | (:require [doo.runner :refer-macros [doo-tests]] 3 | [js-test-in-cljs.test.core-test])) 4 | 5 | (doo-tests 'js-test-in-cljs.test.core-test) 6 | -------------------------------------------------------------------------------- /src/main.js: -------------------------------------------------------------------------------- 1 | function foo(x) { 2 | return x * x; 3 | } 4 | --------------------------------------------------------------------------------