Web Inspector: add InspectorTest.expectException() and use it
[WebKit-https.git] / LayoutTests / inspector / unit-tests / test-harness-expect-functions-async.html
1 <!doctype html>
2 <html>
3 <head>
4 <script src="../../http/tests/inspector/resources/inspector-test.js"></script>
5 <script>
6 function test()
7 {
8     let suite = InspectorTest.createAsyncSuite("InspectorTest.ExpectFunctions.Async");
9     suite.addTestCase({
10         name: "expectException.WorkIsNotAFunction",
11         async test() {
12             InspectorTest.expectException(async () => {
13                 await InspectorTest.expectException(42);
14             });
15         }
16     });
17
18     suite.addTestCase({
19         name: "expectException.SyncWorkThatThrowsException",
20         async test() {
21             let error;
22             await InspectorTest.expectException(() => {
23                 error = new Error("A fake exception");
24                 throw error;
25             }).then((e) => {
26                 InspectorTest.expectNotNull(error, "Returned promise should be resolved with the expected exception.");
27                 InspectorTest.expectEqual(error, e, "Exception should not be altered.");
28             }).catch(() => {
29                 InspectorTest.fail("The returned promise should not reject since an expected exception was raised.");
30             });
31         }
32     });
33
34     suite.addTestCase({
35         name: "expectException.SyncWorkThatDoesNotThrowException",
36         async test() {
37             InspectorTest.log("The following assertion is expected to fail.");
38
39             let returnValue = 42;
40             await InspectorTest.expectException(() => {
41                 return returnValue;
42             }).then((e) => {
43                 InspectorTest.fail("The returned promise should reject since an expected exception was not raised.");
44             }).catch((resolvedValue) => {
45                 InspectorTest.expectEqual(resolvedValue, returnValue, "Rejected value should be the returned value.");
46             });
47         }
48     });
49
50     suite.addTestCase({
51         name: "expectException.AsyncWorkThatRejects",
52         async test() {
53             let error;
54
55             await InspectorTest.expectException(() => {
56                 error = new Error("A fake rejection");
57                 return Promise.reject(error);
58             }).then((e) => {
59                 InspectorTest.expectNotNull(error, "Returned promise should be resolved with the expected exception.");
60                 InspectorTest.expectEqual(error, e, "Exception should not be altered.");
61             }).catch(() => {
62                 InspectorTest.fail("The returned promise should not reject since an expected exception was raised.");
63             });
64         }
65     });
66
67     suite.addTestCase({
68         name: "expectException.AsyncWorkThatResolves",
69         async test() {
70             InspectorTest.log("The following assertion is expected to fail.");
71
72             let returnValue = 42;
73             await InspectorTest.expectException(() => {
74                 return Promise.resolve(returnValue);
75             }).then(() => {
76                 InspectorTest.fail("The returned promise should not resolve since an expected exception was not raised.");
77             }).catch((resolvedValue) => {
78                 InspectorTest.expectEqual(resolvedValue, returnValue, "Rejected value should be the returned value.");
79             });
80         }
81     });
82
83     suite.runTestCasesAndFinish();
84 }
85 </script>
86 </head>
87 <body onload="runTest()">
88     <p>Testing asynchronous TestHarness.expect* functions.</p>
89 </body>
90 </html>