mirror of https://github.com/status-im/reagent.git
Catch all React errors during tests
This commit is contained in:
parent
7d074b55ed
commit
36509f79ff
|
@ -901,6 +901,29 @@
|
||||||
(defn foo []
|
(defn foo []
|
||||||
[:div])
|
[:div])
|
||||||
|
|
||||||
|
(defn log-error [& f]
|
||||||
|
(debug/error (apply str f)))
|
||||||
|
|
||||||
|
(defn wrap-capture-window-error [f]
|
||||||
|
(fn []
|
||||||
|
(let [org js/console.onerror]
|
||||||
|
(set! js/window.onerror (fn [e]
|
||||||
|
(log-error e)
|
||||||
|
true))
|
||||||
|
(try
|
||||||
|
(f)
|
||||||
|
(finally
|
||||||
|
(set! js/window.onerror org))))))
|
||||||
|
|
||||||
|
(defn wrap-capture-console-error [f]
|
||||||
|
(fn []
|
||||||
|
(let [org js/console.error]
|
||||||
|
(set! js/console.error log-error)
|
||||||
|
(try
|
||||||
|
(f)
|
||||||
|
(finally
|
||||||
|
(set! js/console.error org))))))
|
||||||
|
|
||||||
(deftest test-err-messages
|
(deftest test-err-messages
|
||||||
(when (dev?)
|
(when (dev?)
|
||||||
(is (thrown-with-msg?
|
(is (thrown-with-msg?
|
||||||
|
@ -934,24 +957,27 @@
|
||||||
pkg "reagenttest.testreagent."
|
pkg "reagenttest.testreagent."
|
||||||
stack1 (str "in " pkg "comp1")
|
stack1 (str "in " pkg "comp1")
|
||||||
stack2 (str "in " pkg "comp2 > " pkg "comp1")
|
stack2 (str "in " pkg "comp2 > " pkg "comp1")
|
||||||
lstr (fn [& s] (list (apply str s)))
|
|
||||||
re (fn [& s]
|
re (fn [& s]
|
||||||
(re-pattern (apply str s)))
|
(re-pattern (apply str s)))
|
||||||
rend (fn [x]
|
rend (fn [x]
|
||||||
(with-mounted-component x identity))]
|
(with-mounted-component x identity))]
|
||||||
(let [e (debug/track-warnings
|
(let [e (debug/track-warnings
|
||||||
|
(wrap-capture-window-error
|
||||||
|
(wrap-capture-console-error
|
||||||
#(is (thrown-with-msg?
|
#(is (thrown-with-msg?
|
||||||
:default (re "Invalid tag: 'div.' \\(" stack2 "\\)")
|
:default (re "Invalid tag: 'div.' \\(" stack2 "\\)")
|
||||||
(rend [comp2 [:div. "foo"]]))))]
|
(rend [comp2 [:div. "foo"]]))))))]
|
||||||
(is (= e
|
(is (= (last (:error e))
|
||||||
{:error (lstr "Error rendering component (" stack2 ")")})))
|
(str "Error rendering component (" stack2 ")"))))
|
||||||
|
|
||||||
(let [e (debug/track-warnings
|
(let [e (debug/track-warnings
|
||||||
|
(wrap-capture-window-error
|
||||||
|
(wrap-capture-console-error
|
||||||
#(is (thrown-with-msg?
|
#(is (thrown-with-msg?
|
||||||
:default (re "Invalid tag: 'div.' \\(" stack1 "\\)")
|
:default (re "Invalid tag: 'div.' \\(" stack1 "\\)")
|
||||||
(rend [comp1 [:div. "foo"]]))))]
|
(rend [comp1 [:div. "foo"]]))))))]
|
||||||
(is (= e
|
(is (= (last (:error e))
|
||||||
{:error (lstr "Error rendering component (" stack1 ")")})))
|
(str "Error rendering component (" stack1 ")"))))
|
||||||
|
|
||||||
(let [e (debug/track-warnings #(r/as-element [nat]))]
|
(let [e (debug/track-warnings #(r/as-element [nat]))]
|
||||||
(is (re-find #"Using native React classes directly"
|
(is (re-find #"Using native React classes directly"
|
||||||
|
@ -981,11 +1007,14 @@
|
||||||
comp1 (fn comp1 []
|
comp1 (fn comp1 []
|
||||||
($ nil :foo)
|
($ nil :foo)
|
||||||
[:div "foo"])]
|
[:div "foo"])]
|
||||||
(with-mounted-component [error-boundary [comp1]]
|
(debug/track-warnings
|
||||||
|
(wrap-capture-window-error
|
||||||
|
(wrap-capture-console-error
|
||||||
|
#(with-mounted-component [error-boundary [comp1]]
|
||||||
(fn [c div]
|
(fn [c div]
|
||||||
(r/flush)
|
(r/flush)
|
||||||
(is (= "Cannot read property 'foo' of null" (.-message @error)))
|
(is (= "Cannot read property 'foo' of null" (.-message @error)))
|
||||||
(is (found-in #"Something went wrong\." div)))))))
|
(is (found-in #"Something went wrong\." div))))))))))
|
||||||
|
|
||||||
(deftest test-dom-node
|
(deftest test-dom-node
|
||||||
(let [node (atom nil)
|
(let [node (atom nil)
|
||||||
|
|
Loading…
Reference in New Issue