Skip to content

Commit

Permalink
Implement a promise_rejects function to match assert_throws.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ms2ger committed Dec 26, 2014
1 parent 91db5ba commit 83ab6e5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 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
7 changes: 7 additions & 0 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

0 comments on commit 83ab6e5

Please sign in to comment.