OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * testharness-helpers contains various useful extensions to testharness.js to |
| 3 * allow them to be used across multiple tests before they have been |
| 4 * upstreamed. This file is intended to be usable from both document and worker |
| 5 * environments, so code should for example not rely on the DOM. |
| 6 */ |
| 7 |
| 8 // 'promise_test' is a new kind of testharness test that handles some |
| 9 // boilerplate for testing with promises. |
| 10 function promise_test(func, name, properties) { |
| 11 properties = properties || {}; |
| 12 var test = async_test(name, properties); |
| 13 Promise.resolve(test.step(func, test, test)) |
| 14 .then(function() { test.done(); }) |
| 15 .catch(test.step_func(function(value) { |
| 16 throw value; |
| 17 })); |
| 18 } |
| 19 |
| 20 // Returns a promise that fulfills after the provided |promise| is fulfilled. |
| 21 // The |test| succeeds only if |promise| rejects with an exception matching |
| 22 // |code|. Accepted values for |code| follow those accepted for assert_throws(). |
| 23 // The optional |description| describes the test being performed. |
| 24 // E.g.: |
| 25 // assert_promise_rejects( |
| 26 // new Promise(...), // something that should throw an exception. |
| 27 // 'NotFoundError', |
| 28 // 'Should throw NotFoundError.'); |
| 29 // |
| 30 // assert_promise_rejects( |
| 31 // new Promise(...), |
| 32 // new TypeError(), |
| 33 // 'Should throw TypeError'); |
| 34 function assert_promise_rejects(promise, code, description) { |
| 35 return promise.then( |
| 36 function() { |
| 37 throw 'assert_promise_rejects: ' + description + ' Promise did not reject.
'; |
| 38 }, |
| 39 function(e) { |
| 40 if (code !== undefined) { |
| 41 assert_throws(code, function() { throw e; }, description); |
| 42 } |
| 43 }); |
| 44 } |
OLD | NEW |