Skip to content

Commit 565b786

Browse files
borkdudeswannodette
authored andcommitted
CLJS-3483: async tests should report uncaught exception
1 parent 84479f6 commit 565b786

2 files changed

Lines changed: 158 additions & 147 deletions

File tree

src/main/cljs/cljs/test.cljc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,12 @@
243243
(let [done-sym (gensym "done")]
244244
[`(cljs.test/async ~done-sym
245245
(try ~@body
246+
(catch :default e#
247+
(cljs.test/do-report
248+
{:type :error
249+
:message "Uncaught exception, not in assertion."
250+
:expected nil
251+
:actual e#}))
246252
(finally (~done-sym))))])
247253
body)]
248254
(when ana/*load-tests*
Lines changed: 152 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(ns cljs.async-await-test
22
(:refer-global :only [Date Promise])
3-
(:require [clojure.test :refer [deftest is async]]
3+
(:require [clojure.test :refer [deftest is] :as t]
44
[cljs.core :as cc :refer [await] :rename {await aw}]
55
[goog.object :as gobj])
66
(:require-macros [cljs.macro-test.macros :refer [await!] :as macros]))
@@ -14,12 +14,10 @@
1414
(+ n x y (f))))
1515

1616
(deftest ^:async defn-test
17-
(try
18-
(let [v (await (foo 10))]
19-
(is (= 61 v)))
20-
(let [v (await (apply foo [10]))]
21-
(is (= 61 v)))
22-
(catch :default _ (is false))))
17+
(let [v (await (foo 10))]
18+
(is (= 61 v)))
19+
(let [v (await (apply foo [10]))]
20+
(is (= 61 v))))
2321

2422
(defn ^:async variadic-foo [n & ns]
2523
(let [x (await (js/Promise.resolve n))
@@ -30,179 +28,155 @@
3028
(+ n x y (f))))
3129

3230
(deftest ^:async variadic-defn-test
33-
(try
34-
(let [v (await (variadic-foo 10))]
35-
(is (= 41 v)))
36-
(let [v (await (variadic-foo 10 1 2 3))]
37-
(is (= 47 v)))
38-
(let [v (await (apply variadic-foo [10 1 2 3]))]
39-
(is (= 47 v)))
40-
(catch :default _ (is false))))
31+
(let [v (await (variadic-foo 10))]
32+
(is (= 41 v)))
33+
(let [v (await (variadic-foo 10 1 2 3))]
34+
(is (= 47 v)))
35+
(let [v (await (apply variadic-foo [10 1 2 3]))]
36+
(is (= 47 v))))
4137

4238
(defn ^:async multi-arity-foo
4339
([n] (await n))
4440
([n x] (+ (await n) x)))
4541

4642
(deftest ^:async multi-arity-defn-test
47-
(try
48-
(let [v (await (multi-arity-foo 10))]
49-
(is (= 10 v)))
50-
(let [v (await (multi-arity-foo 10 20))]
51-
(is (= 30 v)))
52-
(let [v (await (apply multi-arity-foo [10]))]
53-
(is (= 10 v)))
54-
(let [v (await (apply multi-arity-foo [10 20]))]
55-
(is (= 30 v)))
56-
(catch :default _ (is false))))
43+
(let [v (await (multi-arity-foo 10))]
44+
(is (= 10 v)))
45+
(let [v (await (multi-arity-foo 10 20))]
46+
(is (= 30 v)))
47+
(let [v (await (apply multi-arity-foo [10]))]
48+
(is (= 10 v)))
49+
(let [v (await (apply multi-arity-foo [10 20]))]
50+
(is (= 30 v))))
5751

5852
(defn ^:async multi-arity-variadic-foo
5953
([n] (await n))
6054
([n & xs] (apply + (await n) xs)))
6155

6256
(deftest ^:async multi-arity-variadic-test
63-
(try
64-
(let [v (await (multi-arity-variadic-foo 10))]
65-
(is (= 10 v)))
66-
(let [v (await (multi-arity-variadic-foo 10 20))]
67-
(is (= 30 v)))
68-
(let [v (await (apply multi-arity-variadic-foo [10]))]
69-
(is (= 10 v)))
70-
(let [v (await (apply multi-arity-variadic-foo [10 20]))]
71-
(is (= 30 v)))
72-
(catch :default _ (is false))))
57+
(let [v (await (multi-arity-variadic-foo 10))]
58+
(is (= 10 v)))
59+
(let [v (await (multi-arity-variadic-foo 10 20))]
60+
(is (= 30 v)))
61+
(let [v (await (apply multi-arity-variadic-foo [10]))]
62+
(is (= 10 v)))
63+
(let [v (await (apply multi-arity-variadic-foo [10 20]))]
64+
(is (= 30 v))))
7365

7466
(deftest ^:async fn-test
75-
(try
76-
(let [f (^:async fn [x] (+ x (await (js/Promise.resolve 20))))
77-
v (await (f 10))
78-
v2 (await (apply f [10]))]
79-
(is (= 30 v v2)))
80-
(catch :default _ (is false))))
67+
(let [f (^:async fn [x] (+ x (await (js/Promise.resolve 20))))
68+
v (await (f 10))
69+
v2 (await (apply f [10]))]
70+
(is (= 30 v v2))))
8171

8272
(deftest ^:async varargs-fn-test
83-
(try
84-
(let [f (^:async fn [x & xs] (apply + x (await (js/Promise.resolve 20)) xs))
85-
v (await (f 10))
86-
v2 (await (apply f [10]))
87-
v3 (await (f 5 5))
88-
v4 (await (apply f [5 5]))]
89-
(is (= 30 v v2 v3 v4)))
90-
(catch :default _ (is false))))
73+
(let [f (^:async fn [x & xs] (apply + x (await (js/Promise.resolve 20)) xs))
74+
v (await (f 10))
75+
v2 (await (apply f [10]))
76+
v3 (await (f 5 5))
77+
v4 (await (apply f [5 5]))]
78+
(is (= 30 v v2 v3 v4))))
9179

9280
(deftest ^:async variadic-fn-test
93-
(try (let [f (^:async fn
94-
([x] (await (js/Promise.resolve x)))
95-
([x y] (cons (await (js/Promise.resolve x)) [y])))]
96-
(is (= [1 1 [1 2] [1 2]]
97-
[(await (f 1))
98-
(await (apply f [1]))
99-
(await (f 1 2))
100-
(await (apply f [1 2]))])))
101-
(catch :default _ (is false))))
81+
(let [f (^:async fn
82+
([x] (await (js/Promise.resolve x)))
83+
([x y] (cons (await (js/Promise.resolve x)) [y])))]
84+
(is (= [1 1 [1 2] [1 2]]
85+
[(await (f 1))
86+
(await (apply f [1]))
87+
(await (f 1 2))
88+
(await (apply f [1 2]))]))))
10289

10390
(deftest ^:async variadic-varargs-fn-test
104-
(try (let [f (^:async fn
105-
([x] (await (js/Promise.resolve x)))
106-
([x & xs] (cons (await (js/Promise.resolve x)) xs)))]
107-
(is (= [1 1 [1 2 3] [1 2 3]]
108-
[(await (f 1))
109-
(await (apply f [1]))
110-
(await (f 1 2 3))
111-
(await (apply f [1 2 3]))])))
112-
(catch :default _ (is false))))
91+
(let [f (^:async fn
92+
([x] (await (js/Promise.resolve x)))
93+
([x & xs] (cons (await (js/Promise.resolve x)) xs)))]
94+
(is (= [1 1 [1 2 3] [1 2 3]]
95+
[(await (f 1))
96+
(await (apply f [1]))
97+
(await (f 1 2 3))
98+
(await (apply f [1 2 3]))]))))
11399

114100
(deftest ^:async await-in-throw-test
115101
(let [f (^:async fn [x] (inc (if (odd? x) (throw (await (js/Promise.resolve "dude"))) x)))]
116-
(try
117-
(let [x (await (f 2))]
118-
(is (= 3 x)))
119-
(let [x (try (await (f 1))
120-
(catch :default e e))]
121-
(is (= "dude" x)))
122-
(catch :default _ (is false)))))
102+
(let [x (await (f 2))]
103+
(is (= 3 x)))
104+
(let [x (try (await (f 1))
105+
(catch :default e e))]
106+
(is (= "dude" x)))))
123107

124108
(deftest ^:async await-in-do-test
125-
(try
126-
(let [a (atom 0)
127-
f (^:async fn [] (let [_ (do (swap! a inc)
128-
(swap! a + (await (js/Promise.resolve 2))))]
129-
@a))
130-
v (await (f))]
131-
(is (= 3 v)))
132-
(catch :default _ (is false))))
109+
(let [a (atom 0)
110+
f (^:async fn [] (let [_ (do (swap! a inc)
111+
(swap! a + (await (js/Promise.resolve 2))))]
112+
@a))
113+
v (await (f))]
114+
(is (= 3 v))))
133115

134116
(deftest ^:async await-let-fn-test
135-
(try
136-
(let [f (^:async fn [] (let [v
137-
;; force letfn in expr position
138-
(letfn [(^:async f [] (inc (await (js/Promise.resolve 10))))]
139-
(inc (await (f))))]
140-
(identity v)))
141-
v (await (f))]
142-
(is (= 12 v)))
143-
(catch :default _ (is false))))
117+
(let [f (^:async fn [] (let [v
118+
;; force letfn in expr position
119+
(letfn [(^:async f [] (inc (await (js/Promise.resolve 10))))]
120+
(inc (await (f))))]
121+
(identity v)))
122+
v (await (f))]
123+
(is (= 12 v))))
144124

145125
(deftest ^:async await-in-loop-test
146-
(try
147-
(let [f (^:async fn [] (let [x
148-
;; force loop in expr position
149-
(loop [xs (map #(js/Promise.resolve %) [1 2 3])
150-
ys []]
151-
(if (seq xs)
152-
(let [x (first xs)
153-
v (await x)]
154-
(recur (rest xs) (conj ys v)))
155-
ys))]
156-
(identity x)))
157-
v (await (f))]
158-
(is (= [1 2 3] v)))
159-
(catch :default _ (is false))))
126+
(let [f (^:async fn [] (let [x
127+
;; force loop in expr position
128+
(loop [xs (map #(js/Promise.resolve %) [1 2 3])
129+
ys []]
130+
(if (seq xs)
131+
(let [x (first xs)
132+
v (await x)]
133+
(recur (rest xs) (conj ys v)))
134+
ys))]
135+
(identity x)))
136+
v (await (f))]
137+
(is (= [1 2 3] v))))
160138

161139
(deftest ^:async await-in-nested
162-
(try
163-
(let [f (^:async fn []
164-
(let [b1 1
165-
b2 (let [x 2]
166-
(+ x
167-
;; outer let doesn't have awaits
168-
;; but inner let does, so outer let should become async
169-
(let [x (await (js/Promise.resolve 1))] x)))
170-
b3 (case :foo :foo (case :foo :foo (await (js/Promise.resolve 1))))
171-
b4 (int ;; wrapped in int to avoid false positive warning:
172-
;; all arguments must be numbers, got [number
173-
;; ignore] instead
174-
(try (throw (throw (await (js/Promise.resolve 1)))) (catch :default _ 1 )))
175-
a (atom 0)
176-
b5 (do (swap! a inc) (swap! a inc)
177-
;; do with single expr, wrapped in identity to avoid merging with upper do
178-
(identity (do (swap! a (await (js/Promise.resolve inc)))))
179-
;; do with multiple exprs, wrapped identity to avoid merging with upper do
180-
(identity (do (swap! a inc) (swap! a (await (js/Promise.resolve inc)))))
181-
@a)
182-
b6 (try (identity (try 1 (finally (await nil))))
183-
(finally nil))
184-
b7 (letfn [(f [x] x)]
185-
(f (letfn [(f [x] x)]
186-
(f (await 1)))))]
187-
(await (+ b1 b2 b3 b4 b5 b6 b7))))]
188-
(is (= 13 (await (f)))))
189-
(catch :default _ (is false))))
140+
(let [f (^:async fn []
141+
(let [b1 1
142+
b2 (let [x 2]
143+
(+ x
144+
;; outer let doesn't have awaits
145+
;; but inner let does, so outer let should become async
146+
(let [x (await (js/Promise.resolve 1))] x)))
147+
b3 (case :foo :foo (case :foo :foo (await (js/Promise.resolve 1))))
148+
b4 (int ;; wrapped in int to avoid false positive warning:
149+
;; all arguments must be numbers, got [number
150+
;; ignore] instead
151+
(try (throw (throw (await (js/Promise.resolve 1)))) (catch :default _ 1 )))
152+
a (atom 0)
153+
b5 (do (swap! a inc) (swap! a inc)
154+
;; do with single expr, wrapped in identity to avoid merging with upper do
155+
(identity (do (swap! a (await (js/Promise.resolve inc)))))
156+
;; do with multiple exprs, wrapped identity to avoid merging with upper do
157+
(identity (do (swap! a inc) (swap! a (await (js/Promise.resolve inc)))))
158+
@a)
159+
b6 (try (identity (try 1 (finally (await nil))))
160+
(finally nil))
161+
b7 (letfn [(f [x] x)]
162+
(f (letfn [(f [x] x)]
163+
(f (await 1)))))]
164+
(await (+ b1 b2 b3 b4 b5 b6 b7))))]
165+
(is (= 13 (await (f))))))
190166

191167
(deftest ^:async await-with-aliases-or-renamed-and-via-macros-test
192-
(try
193-
(let [a (await! (js/Promise.resolve 1))
194-
b (macros/await! (js/Promise.resolve 1))
195-
c (cc/await (js/Promise.resolve 1))
196-
d (aw (js/Promise.resolve 1))
197-
e (cljs.core/await (js/Promise.resolve 1))
198-
f (clojure.core/await (js/Promise.resolve 1))]
199-
(is (= 1 a))
200-
(is (= 1 b))
201-
(is (= 1 c))
202-
(is (= 1 d))
203-
(is (= 1 e))
204-
(is (= 1 f)))
205-
(catch :default _ (is false))))
168+
(let [a (await! (js/Promise.resolve 1))
169+
b (macros/await! (js/Promise.resolve 1))
170+
c (cc/await (js/Promise.resolve 1))
171+
d (aw (js/Promise.resolve 1))
172+
e (cljs.core/await (js/Promise.resolve 1))
173+
f (clojure.core/await (js/Promise.resolve 1))]
174+
(is (= 1 a))
175+
(is (= 1 b))
176+
(is (= 1 c))
177+
(is (= 1 d))
178+
(is (= 1 e))
179+
(is (= 1 f))))
206180

207181
(deftest ^:async await-with-ctor
208182
(let [f (^:async fn [] (Date. (await (Promise/resolve 0))))]
@@ -235,6 +209,37 @@
235209
(let [res (await (async-destructure {:foo 1}))]
236210
(is (= [1 "hello!"] res))))
237211

212+
(def throw-in-async-error-test (volatile! false))
213+
214+
(deftest ^:async simulated-async-error-test
215+
(when @throw-in-async-error-test
216+
(throw (js/Error. "simulated")))
217+
(is (not @throw-in-async-error-test)))
218+
219+
(def async-error-reports (atom []))
220+
221+
(defmethod t/report [::error-capture :error] [m]
222+
(swap! async-error-reports conj m))
223+
224+
(deftest ^:async async-test-error-reporting-test
225+
(reset! async-error-reports [])
226+
(let [outer-env (t/get-current-env)
227+
reports (try
228+
(t/set-env! (t/empty-env ::error-capture))
229+
(vreset! throw-in-async-error-test true)
230+
(await (js/Promise.
231+
(fn [resolve _]
232+
(t/run-block
233+
(concat (t/test-var-block (var simulated-async-error-test))
234+
[(fn [] (resolve @async-error-reports))])))))
235+
(finally
236+
(vreset! throw-in-async-error-test false)
237+
(t/set-env! outer-env)))]
238+
(is (= 1 (count reports)))
239+
(let [{:keys [message actual]} (first reports)]
240+
(is (= "Uncaught exception, not in assertion." message))
241+
(is (= "simulated" (ex-message actual))))))
242+
238243
(comment
239244
(clojure.test/run-tests)
240245
)

0 commit comments

Comments
 (0)