Skip to content

test(promise): test for error object names instead of specific messages #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 48 additions & 67 deletions test/PromiseTest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -55,35 +55,21 @@ function testThen(param) {
}

function testInvalidThen(param) {
return Core__Promise.$$catch(Promise.resolve(1).then(function (first) {
return Promise.resolve(Promise.resolve(first + 1 | 0));
}).then(function (p) {
p.then(function (value) {
Test.run([
[
"PromiseTest.res",
57,
28,
41
],
"Should be 2"
], value, equal, 2);
return Promise.resolve(undefined);
});
return Promise.resolve(undefined);
}), (function (e) {
var ret = e.RE_EXN_ID === Js_exn.$$Error ? e._1.message === "p.then is not a function" : false;
Test.run([
[
"PromiseTest.res",
68,
26,
60
],
"then should have thrown an error"
], ret, equal, true);
return Promise.resolve(undefined);
}));
return Promise.resolve(1).then(function (first) {
return Promise.resolve(Promise.resolve(first + 1 | 0));
}).then(function (p) {
var isPromise = typeof p === "object";
Test.run([
[
"PromiseTest.res",
56,
26,
51
],
"Should not be a promise"
], isPromise, equal, false);
return Promise.resolve(undefined);
});
}

function testThenResolve(param) {
Expand All @@ -93,7 +79,7 @@ function testThenResolve(param) {
Test.run([
[
"PromiseTest.res",
81,
69,
26,
39
],
Expand All @@ -103,26 +89,21 @@ function testThenResolve(param) {
}

function testInvalidThenResolve(param) {
return Core__Promise.$$catch(Promise.resolve(1).then(function (num) {
return Promise.resolve(num);
}).then(function (p) {
p.then(function (num) {
return num + 1 | 0;
});
return Promise.resolve(undefined);
}), (function (e) {
var ret = e.RE_EXN_ID === Js_exn.$$Error ? e._1.message === "p.then is not a function" : false;
Test.run([
[
"PromiseTest.res",
107,
26,
60
],
"then should have thrown an error"
], ret, equal, true);
return Promise.resolve(undefined);
}));
return Promise.resolve(1).then(function (num) {
return Promise.resolve(num);
}).then(function (p) {
var isPromise = typeof p === "object";
Test.run([
[
"PromiseTest.res",
83,
26,
51
],
"Should not be a promise"
], isPromise, equal, false);
return Promise.resolve(undefined);
});
}

function runTests$1(param) {
Expand All @@ -148,7 +129,7 @@ function testExnRejection(param) {
Test.run([
[
"PromiseTest.res",
129,
105,
26,
30
],
Expand Down Expand Up @@ -181,11 +162,11 @@ function testExternalPromiseThrow(param) {
return Core__Promise.$$catch(Curry._1(asyncParseFail, undefined).then(function (param) {
return Promise.resolve(undefined);
}), (function (e) {
var success = e.RE_EXN_ID === Js_exn.$$Error ? Caml_obj.equal(e._1.message, "Unexpected token . in JSON at position 1") : false;
var success = e.RE_EXN_ID === Js_exn.$$Error ? Caml_obj.equal(e._1.name, "SyntaxError") : false;
Test.run([
[
"PromiseTest.res",
163,
139,
26,
76
],
Expand All @@ -207,7 +188,7 @@ function testExnThrow(param) {
Test.run([
[
"PromiseTest.res",
182,
158,
26,
49
],
Expand All @@ -225,7 +206,7 @@ function testRaiseErrorThrow(param) {
Test.run([
[
"PromiseTest.res",
205,
181,
26,
51
],
Expand All @@ -249,7 +230,7 @@ function thenAfterCatch(param) {
Test.run([
[
"PromiseTest.res",
228,
204,
26,
45
],
Expand Down Expand Up @@ -278,7 +259,7 @@ function testCatchFinally(param) {
Test.run([
[
"PromiseTest.res",
250,
226,
26,
48
],
Expand All @@ -287,7 +268,7 @@ function testCatchFinally(param) {
Test.run([
[
"PromiseTest.res",
251,
227,
26,
59
],
Expand All @@ -309,7 +290,7 @@ function testResolveFinally(param) {
Test.run([
[
"PromiseTest.res",
268,
244,
26,
45
],
Expand All @@ -318,7 +299,7 @@ function testResolveFinally(param) {
Test.run([
[
"PromiseTest.res",
269,
245,
26,
59
],
Expand Down Expand Up @@ -388,7 +369,7 @@ function testParallel(param) {
Test.run([
[
"PromiseTest.res",
306,
282,
26,
55
],
Expand All @@ -415,7 +396,7 @@ function testRace(param) {
Test.run([
[
"PromiseTest.res",
325,
301,
26,
44
],
Expand Down Expand Up @@ -449,7 +430,7 @@ function testParallel2(param) {
Test.run([
[
"PromiseTest.res",
349,
325,
26,
55
],
Expand Down Expand Up @@ -494,7 +475,7 @@ function testParallel3(param) {
Test.run([
[
"PromiseTest.res",
374,
350,
26,
55
],
Expand Down Expand Up @@ -545,7 +526,7 @@ function testParallel4(param) {
Test.run([
[
"PromiseTest.res",
400,
376,
26,
55
],
Expand Down Expand Up @@ -602,7 +583,7 @@ function testParallel5(param) {
Test.run([
[
"PromiseTest.res",
427,
403,
26,
55
],
Expand Down Expand Up @@ -665,7 +646,7 @@ function testParallel6(param) {
Test.run([
[
"PromiseTest.res",
455,
431,
26,
55
],
Expand Down
34 changes: 5 additions & 29 deletions test/PromiseTest.res
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,8 @@ module ThenChaining = {
resolve(resolve(first + 1))
})
->then(p => {
p
->then(value => {
Test.run(__POS_OF__("Should be 2"), value, equal, 2)
resolve()
})
->ignore
resolve()
})
->catch(e => {
let ret = switch e {
| Exn.Error(m) => Exn.message(m) === Some("p.then is not a function")
| _ => false
}
Test.run(__POS_OF__("then should have thrown an error"), ret, equal, true)
let isPromise = Type.typeof((p: promise<_>)) == #object
Test.run(__POS_OF__("Should not be a promise"), isPromise, equal, false)
resolve()
})
}
Expand All @@ -91,20 +79,8 @@ module ThenChaining = {
resolve(num)
})
->then(p => {
// This will throw because of the auto-collapsing of promises
p
->thenResolve(num => {
num + 1
})
->ignore
resolve()
})
->catch(e => {
let ret = switch e {
| Exn.Error(m) => Exn.message(m) === Some("p.then is not a function")
| _ => false
}
Test.run(__POS_OF__("then should have thrown an error"), ret, equal, true)
let isPromise = Type.typeof((p: promise<_>)) == #object
Test.run(__POS_OF__("Should not be a promise"), isPromise, equal, false)
resolve()
})
}
Expand Down Expand Up @@ -156,7 +132,7 @@ module Catching = {
->then(_ => resolve()) // Since our asyncParse will fail anyways, we convert to promise<unit> for our catch later
->catch(e => {
let success = switch e {
| Exn.Error(err) => Exn.message(err) == Some("Unexpected token . in JSON at position 1")
| Exn.Error(err) => Exn.name(err) == Some("SyntaxError")
| _ => false
}

Expand Down