crisptrutski / boot-cljs-test

Boot task to run ClojureScript tests.
53 stars 18 forks source link

PhantomJS ReferenceError when using External Libraries (JayQ) #45

Closed dimitertodorov closed 8 years ago

dimitertodorov commented 8 years ago

When requiring jayq.core, PhantomJS complains about missing libraries. Is there a way to pass to PhantomJS external libraries it should load in addition to the CLJS Compiles outputs.

Writing output.cljs.edn...
Using torcaui/suite.cljs...
Compiling ClojureScript...
WARNING: Replacing ClojureScript compiler option :main with automatically set value.
• output.js
Running cljs tests...
ReferenceError: Can't find variable: jQuery

  phantomjs://code/phantom379506722135246806.js:81 in onError
Elapsed time: 2.595 sec

I know this might be a problem with the way JayQ does not package jQuery with itself but relies on you to require it.

bensu commented 8 years ago

Hi @dimitertodorov

Do you mind looking at https://github.com/bensu/doo/issues/25 ? Adding the jQuery file as a :foreign-lib in the compiler options might be what you need.

dimitertodorov commented 8 years ago

So, I couldn't get jayq working. But I did get plain jQuery working.

My test task now looks like this:

(deftask test-all []
         (comp (testing)
               (test-cljs :exit? true
                          :cljs-opts {:foreign-libs [{:file "jquery/dist/jquery.min.js" :provides ["js.jquery"]}]}
                          :suite-ns 'torcaui.suite)
               (exit!)))

A simple unit test to verify jQuery was loaded in properly would look like this:

(ns torcaui.core-test
  (:require-macros [cljs.test :refer [deftest testing is async]]
                   [dommy.core :refer [sel sel1]])
  (:require [cljs.test]
            [torcaui.core]
            [js.jquery]
            [torcaui.app :as app]
            [torcaui.routes :as routes]
            [torcaui.utils :as utils]
            [om.dom :as dom]
            [om.core :as om :include-macros true]
            [torcaui.components.aside :as aside]
            [dommy.core :refer [attr text] :refer-macros [sel sel1]]))

(defn insert-container! [container]
  (dommy.core/append! (sel1 js/document :body) container))

(defn new-container! []
  (let [id (str "container-" (gensym))
        n (.createElement js/document "DIV")]
    (set! (.-id n) id)
    (insert-container! n)
    (sel1 (str "#" id))))

(def jquery (js* "$"))

(defn x []
  (->
    (jquery "h1.class1")
    (jquery "span")
    (.text)))

(defn widget [data owner]
  (reify
    om/IRender
    (render [this]
      (dom/h1 #js {:id "classes" :className "class1 class2"} nil (:text data)))))

(deftest jquery-works
  (let [c (new-container!)]
    (om/root widget {:text "Hello world!" :other "Potato"} {:target c})
    (let [xv (x)]
      (is (= "Hello world!" xv)))))
crisptrutski commented 8 years ago

Closing, since you already came right (with caveats).

Injecting additional JS at the appropriate point is a (boot-)cljs concern, and should think still solvable without any changes to to this library. But if that's proven incorrect, please reopen.