kaocha-cljs icon indicating copy to clipboard operation
kaocha-cljs copied to clipboard

cljs repl "document is not defined" error

Open practicalli-johnny opened this issue 3 years ago • 2 comments

Description :octocat:

Running kaocha-cljs on a project generated from the fighwheel-main template fails, with cljs repl - document is not defined error.

"Execution error (ReferenceError) at (<cljs repl>:1).\ndocument is not defined\n"

The figwheel-main test runner succuessfully runs runs the 2 unit tests that are generated from the figwheel-main template, using the command clojure -M:fig:test. Both tests pass.

Reproduction guide :beetle:

an example project can be found at: practicalli-john/clojurescript-figwheel-main-hello-world

  • Generate a new project using the fighwheel-main template with the --reagent option (:project/new is a user-level alias for clj-new)
clojure -M:project/new figwheel-main practicalli/hello-world -- --reagent
  • Add koacha-cljs following the instructions in the README (commit on example repo)
    • add an alias called :kaocha-cljs to deps.edn
  :kaocha-cljs
  {:extra-paths ["test"]
   :extra-deps  {com.lambdaisland/kaocha-cljs {:mvn/version "1.2.123"}}} 
  • create a tests.edn file
#kaocha/v1
{:source-paths ["src"]
 :tests [{:id :unit-cljs
          :type :kaocha.type/cljs
          ;; :test-paths ["test"]
          ;; :cljs/timeout 10000                        ; 10 seconds, the default
          ;; :cljs/repl-env cljs.repl.node/repl-env     ; node is the default
          ;; :cljs/repl-env cljs.repl.browser/repl-env
          }]}

The same error occurs with our without the :source-paths value

  • create a bin/kaocha file
#!/usr/bin/env sh
[ -d "node_modules/ws" ] || npm install ws
clojure -M:kaocha-cljs -m kaocha.runner "$@"
  • run tests using bin/kaocha unit-cljs

Observed behaviour: :eyes: :broken_heart: The command line shows the test run has an error and prints out a long stack trace

A summary of the error:

[{:type clojure.lang.ExceptionInfo,
   :message "Execution error (ReferenceError) at (<cljs repl>:1).\ndocument is not defined\n",
   :data {:type :js-eval-exception,
          :error {:status :exception,
                  :value "Execution error (ReferenceError) at (<cljs repl>:1).\ndocument is not defined\n"},
          :repl-env #cljs.repl.node.NodeEnv{:host "localhost", :port 51605, :path nil,
                                            :socket #object[clojure.lang.Atom 0x7169d057
                                                            {:status :ready, :val
                                                             {:socket #object[java.net.Socket 0x3ec75472 "Socket[addr=localhost/127.0.0.1,port=51605,localport=41216]"],
                                                              :in #object[java.io.BufferedReader 0x1953197a "java.io.BufferedReader@1953197a"],
                                                              :out #object[java.io.BufferedWriter 0x30f1487d "java.io.BufferedWriter@30f1487d"]}}],
                                            :proc #object[clojure.lang.Atom 0x15ff6460
                                                          {:status :ready, :val #object[java.lang.ProcessImpl 0x4798ff99 "Process[pid=46644, exitValue=137]"]}],
                                            :state #object[clojure.lang.Atom 0x12cdc214
                                                           {:status :ready, :val {:listeners 0}}],
                                            :debug-port nil},
          :form (require (quote practicalli.hello-world-test)),
          :js "goog.require('practicalli.hello_world_test');\n'nil';\n"},
   :at [cljs.repl$evaluate_form invokeStatic "repl.cljc" 577]}]
Full error report
{:via
 [{:type clojure.lang.ExceptionInfo,
   :message "Execution error (ReferenceError) at (<cljs repl>:1).\ndocument is not defined\n",
   :data {:type :js-eval-exception,
          :error {:status :exception,
                  :value "Execution error (ReferenceError) at (<cljs repl>:1).\ndocument is not defined\n"},
          :repl-env #cljs.repl.node.NodeEnv{:host "localhost", :port 51605, :path nil,
                                            :socket #object[clojure.lang.Atom 0x7169d057
                                                            {:status :ready, :val
                                                             {:socket #object[java.net.Socket 0x3ec75472 "Socket[addr=localhost/127.0.0.1,port=51605,localport=41216]"],
                                                              :in #object[java.io.BufferedReader 0x1953197a "java.io.BufferedReader@1953197a"],
                                                              :out #object[java.io.BufferedWriter 0x30f1487d "java.io.BufferedWriter@30f1487d"]}}],
                                            :proc #object[clojure.lang.Atom 0x15ff6460
                                                          {:status :ready, :val #object[java.lang.ProcessImpl 0x4798ff99 "Process[pid=46644, exitValue=137]"]}],
                                            :state #object[clojure.lang.Atom 0x12cdc214
                                                           {:status :ready, :val {:listeners 0}}],
                                            :debug-port nil},
          :form (require (quote practicalli.hello-world-test)),
          :js "goog.require('practicalli.hello_world_test');\n'nil';\n"},
   :at [cljs.repl$evaluate_form invokeStatic "repl.cljc" 577]}],
 :trace [[cljs.repl$evaluate_form invokeStatic "repl.cljc" 577]
         [cljs.repl$evaluate_form invoke "repl.cljc" 498]
         [cljs.repl$eval_cljs invokeStatic "repl.cljc" 692]
         [cljs.repl$eval_cljs invoke "repl.cljc" 685]
         [kaocha.cljs.queue_eval_loop$start_BANG_$fn__3832$fn__3837 invoke "queue_eval_loop.clj" 78]
         [kaocha.cljs.queue_eval_loop$start_BANG_$fn__3832 invoke "queue_eval_loop.clj" 69]
         [cljs.compiler$with_core_cljs invokeStatic "compiler.cljc" 1474]
         [cljs.compiler$with_core_cljs invoke "compiler.cljc" 1463]
         [kaocha.cljs.queue_eval_loop$start_BANG_ invokeStatic "queue_eval_loop.clj" 55]
         [kaocha.cljs.queue_eval_loop$start_BANG_ doInvoke "queue_eval_loop.clj" 40]
         [clojure.lang.RestFn invoke "RestFn.java" 470]
         [kaocha.cljs.prepl$prepl$fn__3853$fn__3854 invoke "prepl.clj" 14]
         [kaocha.cljs.prepl$prepl$fn__3853 invoke "prepl.clj" 13]
         [clojure.core$binding_conveyor_fn$fn__5739 invoke "core.clj" 2030]
         [clojure.lang.AFn call "AFn.java" 18]
         [java.util.concurrent.FutureTask run "FutureTask.java" 264]
         [java.util.concurrent.ThreadPoolExecutor runWorker "ThreadPoolExecutor.java" 1136]
         [java.util.concurrent.ThreadPoolExecutor$Worker run "ThreadPoolExecutor.java" 635]
         [java.lang.Thread run "Thread.java" 833]],
 :cause "Execution error (ReferenceError) at (<cljs repl>:1).\ndocument is not defined\n",
 :data {:type :js-eval-exception,
        :error {:status :exception,
                :value "Execution error (ReferenceError) at (<cljs repl>:1).\ndocument is not defined\n"},
        :repl-env #cljs.repl.node.NodeEnv{:host "localhost", :port 51605, :path nil,
                                          :socket #object[clojure.lang.Atom 0x7169d057
                                                          {:status :ready, :val
                                                           {:socket #object[java.net.Socket 0x3ec75472 "Socket[addr=localhost/127.0.0.1,port=51605,localport=41216]"],
                                                            :in #object[java.io.BufferedReader 0x1953197a "java.io.BufferedReader@1953197a"],
                                                            :out #object[java.io.BufferedWriter 0x30f1487d "java.io.BufferedWriter@30f1487d"]}}],
                                          :proc #object[clojure.lang.Atom 0x15ff6460
                                                        {:status :ready,
                                                         :val #object[java.lang.ProcessImpl 0x4798ff99 "Process[pid=46644, exitValue=137]"]}],
                                          :state #object[clojure.lang.Atom 0x12cdc214
                                                         {:status :ready, :val {:listeners 0}}], :debug-port nil},
        :form (require (quote practicalli.hello-world-test)),
        :js "goog.require('practicalli.hello_world_test');\n'nil';\n"}}
Complete terminal output
❯ bin/kaocha unit-cljs
[(E]
Randomized with --seed 1633805738

ERROR in unit-cljs (main.java:37)
Unexpected error executing kaocha-cljs test suite.
Exception: clojure.lang.ExceptionInfo: ClojureScript Exception
{:via [{:type clojure.lang.ExceptionInfo, :message "Execution error (ReferenceError) at (<cljs repl>:1).\ndocument is not defined\n", :data {:type :js-eval-exception, :error {:status :exception, :value "Execution error (ReferenceError) at (<cljs repl>:1).\ndocument is not defined\n"}, :repl-env #cljs.repl.node.NodeEnv{:host "localhost", :port 51605, :path nil, :socket #object[clojure.lang.Atom 0x7169d057 {:status :ready, :val {:socket #object[java.net.Socket 0x3ec75472 "Socket[addr=localhost/127.0.0.1,port=51605,localport=41216]"], :in #object[java.io.BufferedReader 0x1953197a "java.io.BufferedReader@1953197a"], :out #object[java.io.BufferedWriter 0x30f1487d "java.io.BufferedWriter@30f1487d"]}}], :proc #object[clojure.lang.Atom 0x15ff6460 {:status :ready, :val #object[java.lang.ProcessImpl 0x4798ff99 "Process[pid=46644, exitValue=137]"]}], :state #object[clojure.lang.Atom 0x12cdc214 {:status :ready, :val {:listeners 0}}], :debug-port nil}, :form (require (quote practicalli.hello-world-test)), :js "goog.require('practicalli.hello_world_test');\n'nil';\n"}, :at [cljs.repl$evaluate_form invokeStatic "repl.cljc" 577]}], :trace [[cljs.repl$evaluate_form invokeStatic "repl.cljc" 577] [cljs.repl$evaluate_form invoke "repl.cljc" 498] [cljs.repl$eval_cljs invokeStatic "repl.cljc" 692] [cljs.repl$eval_cljs invoke "repl.cljc" 685] [kaocha.cljs.queue_eval_loop$start_BANG_$fn__3832$fn__3837 invoke "queue_eval_loop.clj" 78] [kaocha.cljs.queue_eval_loop$start_BANG_$fn__3832 invoke "queue_eval_loop.clj" 69] [cljs.compiler$with_core_cljs invokeStatic "compiler.cljc" 1474] [cljs.compiler$with_core_cljs invoke "compiler.cljc" 1463] [kaocha.cljs.queue_eval_loop$start_BANG_ invokeStatic "queue_eval_loop.clj" 55] [kaocha.cljs.queue_eval_loop$start_BANG_ doInvoke "queue_eval_loop.clj" 40] [clojure.lang.RestFn invoke "RestFn.java" 470] [kaocha.cljs.prepl$prepl$fn__3853$fn__3854 invoke "prepl.clj" 14] [kaocha.cljs.prepl$prepl$fn__3853 invoke "prepl.clj" 13] [clojure.core$binding_conveyor_fn$fn__5739 invoke "core.clj" 2030] [clojure.lang.AFn call "AFn.java" 18] [java.util.concurrent.FutureTask run "FutureTask.java" 264] [java.util.concurrent.ThreadPoolExecutor runWorker "ThreadPoolExecutor.java" 1136] [java.util.concurrent.ThreadPoolExecutor$Worker run "ThreadPoolExecutor.java" 635] [java.lang.Thread run "Thread.java" 833]], :cause "Execution error (ReferenceError) at (<cljs repl>:1).\ndocument is not defined\n", :data {:type :js-eval-exception, :error {:status :exception, :value "Execution error (ReferenceError) at (<cljs repl>:1).\ndocument is not defined\n"}, :repl-env #cljs.repl.node.NodeEnv{:host "localhost", :port 51605, :path nil, :socket #object[clojure.lang.Atom 0x7169d057 {:status :ready, :val {:socket #object[java.net.Socket 0x3ec75472 "Socket[addr=localhost/127.0.0.1,port=51605,localport=41216]"], :in #object[java.io.BufferedReader 0x1953197a "java.io.BufferedReader@1953197a"], :out #object[java.io.BufferedWriter 0x30f1487d "java.io.BufferedWriter@30f1487d"]}}], :proc #object[clojure.lang.Atom 0x15ff6460 {:status :ready, :val #object[java.lang.ProcessImpl 0x4798ff99 "Process[pid=46644, exitValue=137]"]}], :state #object[clojure.lang.Atom 0x12cdc214 {:status :ready, :val {:listeners 0}}], :debug-port nil}, :form (require (quote practicalli.hello-world-test)), :js "goog.require('practicalli.hello_world_test');\n'nil';\n"}}
 at kaocha.type.cljs$fn__6985.invokeStatic (cljs.clj:152)
    kaocha.type.cljs/fn (cljs.clj:150)
    kaocha.type.cljs$queue_consumer.invokeStatic (cljs.clj:197)
    kaocha.type.cljs$queue_consumer.invoke (cljs.clj:182)
    kaocha.type.cljs$eval7081$fn__7083.invoke (cljs.clj:359)
    ...
    kaocha.plugin.capture_output$capture_output_wrap_run_hook$fn__2669$fn__2670.invoke (capture_output.clj:83)
    ...
    kaocha.plugin.capture_output$capture_output_wrap_run_hook$fn__2669.doInvoke (capture_output.clj:83)
    ...
    kaocha.testable$run.invokeStatic (testable.clj:129)
    kaocha.testable$run.invoke (testable.clj:120)
    kaocha.testable$run_testable.invokeStatic (testable.clj:211)
    kaocha.testable$run_testable.invoke (testable.clj:158)
    kaocha.testable$run_testables.invokeStatic (testable.clj:224)
    kaocha.testable$run_testables.invoke (testable.clj:214)
    kaocha.type.cljs$eval7023$fn__7025.invoke (cljs.clj:292)
    ...
    kaocha.plugin.capture_output$capture_output_wrap_run_hook$fn__2669$fn__2670.invoke (capture_output.clj:83)
    ...
    kaocha.plugin.capture_output$capture_output_wrap_run_hook$fn__2669.doInvoke (capture_output.clj:83)
    ...
    kaocha.testable$run.invokeStatic (testable.clj:129)
    kaocha.testable$run.invoke (testable.clj:120)
    kaocha.testable$run_testable.invokeStatic (testable.clj:211)
    kaocha.testable$run_testable.invoke (testable.clj:158)
    kaocha.testable$run_testables.invokeStatic (testable.clj:224)
    kaocha.testable$run_testables.invoke (testable.clj:214)
    kaocha.api$run$fn__3176$fn__3180$fn__3181.invoke (api.clj:141)
    ...
    kaocha.api$run$fn__3176$fn__3180.invoke (api.clj:115)
    ...
    kaocha.api$run$fn__3176.invoke (api.clj:114)
    ...
    kaocha.api$run.invokeStatic (api.clj:99)
    kaocha.api$run.invoke (api.clj:86)
    kaocha.runner$run$fn__3239.invoke (runner.clj:132)
    ...
    kaocha.runner$run.invokeStatic (runner.clj:130)
    kaocha.runner$run.invoke (runner.clj:73)
    kaocha.runner$_main_STAR_.invokeStatic (runner.clj:176)
    kaocha.runner$_main_STAR_.doInvoke (runner.clj:144)
    ...
    kaocha.runner$_main.invokeStatic (runner.clj:187)
    kaocha.runner$_main.doInvoke (runner.clj:185)
    ...
0 tests, 1 assertions, 1 errors, 0 failures.
❯ 

Expected behaviour: :heart: :smile: kaocha-cljs should discover and run two unit tests and both tests should pass

image

System Info :computer:

practicalli-johnny avatar Sep 07 '22 12:09 practicalli-johnny

document is not defined

This usually means you're using libraries that expect a DOM (i.e. they expect to find js/document), but you are using them in an environment where there is no DOM, typically Node. That's also what is happening here. You are including goog.dom and reagent.dom, so it's clear you expect to find a DOM, and kaocha-cljs's default behavior is to use the Node REPL to execute tests.

If I instead instruct it to use a browser REPL then all is well.

#kaocha/v1
{:source-paths ["src"]
 :tests [{:id :unit-cljs
          :type :kaocha.type/cljs
          :cljs/repl-env cljs.repl.browser/repl-env}]}

~/github/clojurescript-figwheel-main-hello-world on  main 
➜ bin/kaocha                         
[(..)]
2 tests, 2 assertions, 0 failures.

plexus avatar Sep 20 '22 08:09 plexus

If there's anything actionable here it's probably to add a FAQ about this particular error to the README. A similar thing can happen with js/fetch

plexus avatar Sep 20 '22 08:09 plexus