diff --git a/docs/api.md b/docs/api.md index 0e39154268a891..788b2e018f57ea 100644 --- a/docs/api.md +++ b/docs/api.md @@ -129,21 +129,22 @@ a resolve reaction that verifies the returned value. Note that in the promise chain constructed in `test_function` assertions don't need to wrapped in `step` or `step_func` calls. -Here's another example where the `test_function` uses the provided `test` -parameter to test a Promise that is expected to reject. Note that it's important -to handle all expected rejections since an unhandled rejection causes the test -to fail. +`promise_rejects` can be used to test Promises that need to reject: + + promise_rejects(test_object, code, promise) + +The `code` argument is equivalent to the same argument to the `assert_throws` +function. + +Here's an example where the `bar()` function returns a Promise that rejects +with a TypeError: function bar() { - return Promise.reject("bar"); + return Promise.reject(new TypeError()); } promise_test(function(t) { - return bar() - .then(t.unreached_func("bar() should not accept"), - function(result) { - assert_equals(result, "bar", "bar should return 'bar'"); - }); + return promise_rejects(t, new TypeError(), bar); }, "Another example"); ## Single Page Tests ## diff --git a/testharness.js b/testharness.js index fadd17123f7af8..a31ca7d41d69a6 100644 --- a/testharness.js +++ b/testharness.js @@ -464,6 +464,12 @@ policies and contribution forms [3]. })); } + function promise_rejects(test, expected, promise) { + return promise.then(test.unreached_func("Should have rejected.")).catch(function(e) { + assert_throws(expected, function() { throw e }); + }); + } + function setup(func_or_properties, maybe_properties) { var func = null; @@ -511,6 +517,7 @@ policies and contribution forms [3]. expose(test, 'test'); expose(async_test, 'async_test'); expose(promise_test, 'promise_test'); + expose(promise_rejects, 'promise_rejects'); expose(generate_tests, 'generate_tests'); expose(setup, 'setup'); expose(done, 'done'); @@ -1039,7 +1046,10 @@ policies and contribution forms [3]. var required_props = { code: name_code_map[name] }; if (required_props.code === 0 || - ("name" in e && e.name !== e.name.toUpperCase() && e.name !== "DOMException")) { + (typeof e == "object" && + "name" in e && + e.name !== e.name.toUpperCase() && + e.name !== "DOMException")) { // New style exception: also test the name property. required_props.name = name; } @@ -1181,8 +1191,8 @@ policies and contribution forms [3]. if (this.phase >= this.phases.HAS_RESULT) { return; } - var message = (typeof e === "object" && e !== null) ? e.message : e; - if (typeof e.stack != "undefined" && typeof e.message == "string") { + var message = String((typeof e === "object" && e !== null) ? e.message : e); + if (typeof e.stack != "undefined") { //Try to make it more informative for some exceptions, at least //in Gecko and WebKit. This results in a stack dump instead of //just errors like "Cannot read property 'parentNode' of null"