Skip to content

Commit 4bbd304

Browse files
authored
test(promise): test for error object names instead of specific messages (#66)
* test(promise): test for error object names instead of specific messages * test(promise): test if it is a promise directly rather than a side-effect
1 parent b27fc5f commit 4bbd304

File tree

2 files changed

+53
-96
lines changed

2 files changed

+53
-96
lines changed

test/PromiseTest.mjs

Lines changed: 48 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -55,35 +55,21 @@ function testThen(param) {
5555
}
5656

5757
function testInvalidThen(param) {
58-
return Core__Promise.$$catch(Promise.resolve(1).then(function (first) {
59-
return Promise.resolve(Promise.resolve(first + 1 | 0));
60-
}).then(function (p) {
61-
p.then(function (value) {
62-
Test.run([
63-
[
64-
"PromiseTest.res",
65-
57,
66-
28,
67-
41
68-
],
69-
"Should be 2"
70-
], value, equal, 2);
71-
return Promise.resolve(undefined);
72-
});
73-
return Promise.resolve(undefined);
74-
}), (function (e) {
75-
var ret = e.RE_EXN_ID === Js_exn.$$Error ? e._1.message === "p.then is not a function" : false;
76-
Test.run([
77-
[
78-
"PromiseTest.res",
79-
68,
80-
26,
81-
60
82-
],
83-
"then should have thrown an error"
84-
], ret, equal, true);
85-
return Promise.resolve(undefined);
86-
}));
58+
return Promise.resolve(1).then(function (first) {
59+
return Promise.resolve(Promise.resolve(first + 1 | 0));
60+
}).then(function (p) {
61+
var isPromise = typeof p === "object";
62+
Test.run([
63+
[
64+
"PromiseTest.res",
65+
56,
66+
26,
67+
51
68+
],
69+
"Should not be a promise"
70+
], isPromise, equal, false);
71+
return Promise.resolve(undefined);
72+
});
8773
}
8874

8975
function testThenResolve(param) {
@@ -93,7 +79,7 @@ function testThenResolve(param) {
9379
Test.run([
9480
[
9581
"PromiseTest.res",
96-
81,
82+
69,
9783
26,
9884
39
9985
],
@@ -103,26 +89,21 @@ function testThenResolve(param) {
10389
}
10490

10591
function testInvalidThenResolve(param) {
106-
return Core__Promise.$$catch(Promise.resolve(1).then(function (num) {
107-
return Promise.resolve(num);
108-
}).then(function (p) {
109-
p.then(function (num) {
110-
return num + 1 | 0;
111-
});
112-
return Promise.resolve(undefined);
113-
}), (function (e) {
114-
var ret = e.RE_EXN_ID === Js_exn.$$Error ? e._1.message === "p.then is not a function" : false;
115-
Test.run([
116-
[
117-
"PromiseTest.res",
118-
107,
119-
26,
120-
60
121-
],
122-
"then should have thrown an error"
123-
], ret, equal, true);
124-
return Promise.resolve(undefined);
125-
}));
92+
return Promise.resolve(1).then(function (num) {
93+
return Promise.resolve(num);
94+
}).then(function (p) {
95+
var isPromise = typeof p === "object";
96+
Test.run([
97+
[
98+
"PromiseTest.res",
99+
83,
100+
26,
101+
51
102+
],
103+
"Should not be a promise"
104+
], isPromise, equal, false);
105+
return Promise.resolve(undefined);
106+
});
126107
}
127108

128109
function runTests$1(param) {
@@ -148,7 +129,7 @@ function testExnRejection(param) {
148129
Test.run([
149130
[
150131
"PromiseTest.res",
151-
129,
132+
105,
152133
26,
153134
30
154135
],
@@ -181,11 +162,11 @@ function testExternalPromiseThrow(param) {
181162
return Core__Promise.$$catch(Curry._1(asyncParseFail, undefined).then(function (param) {
182163
return Promise.resolve(undefined);
183164
}), (function (e) {
184-
var success = e.RE_EXN_ID === Js_exn.$$Error ? Caml_obj.equal(e._1.message, "Unexpected token . in JSON at position 1") : false;
165+
var success = e.RE_EXN_ID === Js_exn.$$Error ? Caml_obj.equal(e._1.name, "SyntaxError") : false;
185166
Test.run([
186167
[
187168
"PromiseTest.res",
188-
163,
169+
139,
189170
26,
190171
76
191172
],
@@ -207,7 +188,7 @@ function testExnThrow(param) {
207188
Test.run([
208189
[
209190
"PromiseTest.res",
210-
182,
191+
158,
211192
26,
212193
49
213194
],
@@ -225,7 +206,7 @@ function testRaiseErrorThrow(param) {
225206
Test.run([
226207
[
227208
"PromiseTest.res",
228-
205,
209+
181,
229210
26,
230211
51
231212
],
@@ -249,7 +230,7 @@ function thenAfterCatch(param) {
249230
Test.run([
250231
[
251232
"PromiseTest.res",
252-
228,
233+
204,
253234
26,
254235
45
255236
],
@@ -278,7 +259,7 @@ function testCatchFinally(param) {
278259
Test.run([
279260
[
280261
"PromiseTest.res",
281-
250,
262+
226,
282263
26,
283264
48
284265
],
@@ -287,7 +268,7 @@ function testCatchFinally(param) {
287268
Test.run([
288269
[
289270
"PromiseTest.res",
290-
251,
271+
227,
291272
26,
292273
59
293274
],
@@ -309,7 +290,7 @@ function testResolveFinally(param) {
309290
Test.run([
310291
[
311292
"PromiseTest.res",
312-
268,
293+
244,
313294
26,
314295
45
315296
],
@@ -318,7 +299,7 @@ function testResolveFinally(param) {
318299
Test.run([
319300
[
320301
"PromiseTest.res",
321-
269,
302+
245,
322303
26,
323304
59
324305
],
@@ -388,7 +369,7 @@ function testParallel(param) {
388369
Test.run([
389370
[
390371
"PromiseTest.res",
391-
306,
372+
282,
392373
26,
393374
55
394375
],
@@ -415,7 +396,7 @@ function testRace(param) {
415396
Test.run([
416397
[
417398
"PromiseTest.res",
418-
325,
399+
301,
419400
26,
420401
44
421402
],
@@ -449,7 +430,7 @@ function testParallel2(param) {
449430
Test.run([
450431
[
451432
"PromiseTest.res",
452-
349,
433+
325,
453434
26,
454435
55
455436
],
@@ -494,7 +475,7 @@ function testParallel3(param) {
494475
Test.run([
495476
[
496477
"PromiseTest.res",
497-
374,
478+
350,
498479
26,
499480
55
500481
],
@@ -545,7 +526,7 @@ function testParallel4(param) {
545526
Test.run([
546527
[
547528
"PromiseTest.res",
548-
400,
529+
376,
549530
26,
550531
55
551532
],
@@ -602,7 +583,7 @@ function testParallel5(param) {
602583
Test.run([
603584
[
604585
"PromiseTest.res",
605-
427,
586+
403,
606587
26,
607588
55
608589
],
@@ -665,7 +646,7 @@ function testParallel6(param) {
665646
Test.run([
666647
[
667648
"PromiseTest.res",
668-
455,
649+
431,
669650
26,
670651
55
671652
],

test/PromiseTest.res

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,8 @@ module ThenChaining = {
5252
resolve(resolve(first + 1))
5353
})
5454
->then(p => {
55-
p
56-
->then(value => {
57-
Test.run(__POS_OF__("Should be 2"), value, equal, 2)
58-
resolve()
59-
})
60-
->ignore
61-
resolve()
62-
})
63-
->catch(e => {
64-
let ret = switch e {
65-
| Exn.Error(m) => Exn.message(m) === Some("p.then is not a function")
66-
| _ => false
67-
}
68-
Test.run(__POS_OF__("then should have thrown an error"), ret, equal, true)
55+
let isPromise = Type.typeof((p: promise<_>)) == #object
56+
Test.run(__POS_OF__("Should not be a promise"), isPromise, equal, false)
6957
resolve()
7058
})
7159
}
@@ -91,20 +79,8 @@ module ThenChaining = {
9179
resolve(num)
9280
})
9381
->then(p => {
94-
// This will throw because of the auto-collapsing of promises
95-
p
96-
->thenResolve(num => {
97-
num + 1
98-
})
99-
->ignore
100-
resolve()
101-
})
102-
->catch(e => {
103-
let ret = switch e {
104-
| Exn.Error(m) => Exn.message(m) === Some("p.then is not a function")
105-
| _ => false
106-
}
107-
Test.run(__POS_OF__("then should have thrown an error"), ret, equal, true)
82+
let isPromise = Type.typeof((p: promise<_>)) == #object
83+
Test.run(__POS_OF__("Should not be a promise"), isPromise, equal, false)
10884
resolve()
10985
})
11086
}
@@ -156,7 +132,7 @@ module Catching = {
156132
->then(_ => resolve()) // Since our asyncParse will fail anyways, we convert to promise<unit> for our catch later
157133
->catch(e => {
158134
let success = switch e {
159-
| Exn.Error(err) => Exn.message(err) == Some("Unexpected token . in JSON at position 1")
135+
| Exn.Error(err) => Exn.name(err) == Some("SyntaxError")
160136
| _ => false
161137
}
162138

0 commit comments

Comments
 (0)