This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-01-14
Channels
- # announcements (2)
- # aws (1)
- # babashka (18)
- # babashka-sci-dev (103)
- # beginners (165)
- # calva (51)
- # cider (8)
- # circleci (1)
- # clj-kondo (22)
- # clj-on-windows (2)
- # cljdoc (1)
- # cljfx (31)
- # cljs-dev (16)
- # clojure (81)
- # clojure-europe (71)
- # clojure-nl (7)
- # clojure-uk (11)
- # clojurescript (20)
- # code-reviews (26)
- # conjure (1)
- # contributions-welcome (1)
- # core-async (15)
- # cursive (8)
- # datomic (8)
- # defnpodcast (2)
- # eastwood (24)
- # emacs (10)
- # events (1)
- # fulcro (4)
- # funcool (31)
- # graalvm (43)
- # graphql (8)
- # honeysql (9)
- # introduce-yourself (1)
- # jobs (12)
- # kaocha (3)
- # lsp (28)
- # malli (4)
- # meander (4)
- # membrane (7)
- # off-topic (64)
- # other-languages (3)
- # pedestal (1)
- # polylith (31)
- # portal (5)
- # re-frame (4)
- # reitit (1)
- # releases (5)
- # rum (2)
- # schema (2)
- # sci (34)
- # shadow-cljs (21)
- # vscode (1)
hi all ... is there a straightforward way to mock out re-frame's globals so that I can use kaocha and node to test some non-DOM-related code in a namespace that requires re-frame? My kaocha-cljs suite breaks the instant I add a test that requires a re-frame containing namespace, based on not being able to find `xmlhttprequest` and `window`. Looks like just adding the xmlhttprequest and window npms isn't helping. I'm thinking including a simple JS file that has `window = {}` (no `var` or `let` thus global) would do it here. I'm going with node because mostly we're testing non-DOM-related logic and I find it hard to justify adding browser flakiness to the units when we have a separate browser-based suite that does hit real re-frame.
Depends on your build tooling. I use shadow-cljs and have this line in my :test
build (which is a :node-test
target)
:devtools {:preloads [my.test-preloads]}
Inside the my.test-preloads
namespace is this code
(defn global-stubs!
[]
(let [make-element (fn [_]
(clj->js
{:setAttribute (fn [_] [])
:insertBefore (fn [_] [])}))]
(set! (.-document js/global)
(clj->js {:getElementById make-element
:getElementsByClassName make-element
:createStyle make-element
:createElement make-element
:querySelector make-element
:head (make-element nil)})))
(set! (.-window js/global)
(clj->js {:location {:href ""}}))
(set! (.-navigator js/global) (clj->js {:userAgent ""}))
(set! (.-crypto js/global) (clj->js {}))
(set! (.-history js/global)
(clj->js {:back (fn [])
:forward (fn [])
:go (fn [])})))
(global-stubs!)
This mocks out all the browser functionality that are tests can encounter.oh thanks ... didn't even consider a preload. Kaocha-cljs lets me set up compiler options so I think I can wedge the setting in there https://clojurescript.org/reference/compiler-options#preloads