Skip to content

Commit

Permalink
Merge pull request web-platform-tests#99 from Ms2ger/rejection
Browse files Browse the repository at this point in the history
Implement a promise_rejects function to match assert_throws.
  • Loading branch information
sideshowbarker committed Dec 29, 2014
2 parents 142b7cd + 83ab6e5 commit bdb36d2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
21 changes: 11 additions & 10 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ##
Expand Down
16 changes: 13 additions & 3 deletions testharness.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit bdb36d2

Please sign in to comment.