bensu / doo

doo is a library and lein plugin to run cljs.test on different js environments.
Eclipse Public License 1.0
324 stars 63 forks source link

Feature request: add a way to inject javascript references #25

Closed ricardojmendez closed 8 years ago

ricardojmendez commented 9 years ago

When running tests on doo, it'd be ideal to have a way to inject some Javascript libraries. For instance, Jayq expects you to have linked to the jQuery library from your page, so we'd need to inject it into the tests.

I'm using phantom, but expect this is something desirable for others as well.

bensu commented 9 years ago

Is it okay to add it as a <script src="path/to/jquery"> ?

ricardojmendez commented 9 years ago

As discussed on the chat: yes, adding a script link directly on the test html is file.

jori-ahvonen commented 9 years ago

+1

crisptrutski commented 9 years ago

As an interim measure - how about just including cljsjs/jquery as a test dependency (and requiring it in your runner)

xcthulhu commented 9 years ago

@crisptrutski +1 for CLJSJS :D

bensu commented 8 years ago

Hi @ricardojmendez,

After trying it a couple of alternatives in the REPL (tweaks on clojurescript.test command line builder) I realize that @crisptrutski is right. The ClojureScript compiler already has a method to inject dependencies! If your production build expects $ and you server it through a CDN, then you can always add the snippet in :foreign-libs for the test build.

To explore that solution I've put together this repo which shows how to set it up and run it. These are the important parts:

  :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/$))))

Does this solve your problem?

ricardojmendez commented 8 years ago

@bensu: Let me give it a shot and get back to you later this week - I'm currently tied up on something where doo isn't an option (testing inside a Chrome extension).

ricardojmendez commented 8 years ago

Yes, the dependencies solve exactly this issue.