unexpected
Version:
Extensible BDD assertion toolkit
195 lines (167 loc) • 16.1 kB
HTML
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=0">
<link rel="stylesheet" href="../../static/normalize.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../static/main.css" type="text/css" media="screen" />
<link rel="shortcut icon" type="image/vnd.microsoft.icon" href="../../static/bug-icon-black.ico">
<title>promise.settle</title>
</head>
<body class="sidebar-hidden has-sidebar">
<header>
<div class="logo-icon"></div>
<nav>
<ul>
<li class="menu-toggle-item">
<button class="menu-toggle" onclick="toggleSidebar()"></button>
</li>
<li class=""><a href="../..">Unexpected</a></li>
<li class=""><a href="../../assertions/any/to-be">Assertions</a></li>
<li class="active"><a href="../addAssertion">API</a></li>
<li class=""><a href="../../plugins">Plugins</a></li>
</ul>
</nav>
<div class="search" style="visibility: hidden">
<input id="search" placeholder="Search..." value="">
<div id="searchDropDown" class="dropDown">
<ul id="searchResults"></ul>
</div>
</div>
</header>
<section id="api">
<nav id="api-menu" class="sidebar js-remember-scroll-position">
<ul>
<li class="">
<a href="../addAssertion">addAssertion</a>
</li>
<li class="">
<a href="../addType">addType</a>
</li>
<li class="">
<a href="../async">async</a>
</li>
<li class="">
<a href="../clone">clone</a>
</li>
<li class="">
<a href="../expect">expect</a>
</li>
<li class="">
<a href="../fail">fail</a>
</li>
<li class="">
<a href="../promise">promise</a>
</li>
<li class="">
<a href="../promise-all">promise.all</a>
</li>
<li class="">
<a href="../promise-any">promise.any</a>
</li>
<li class="active">
<a href="">promise.settle</a>
</li>
<li class="">
<a href="../toString">toString</a>
</li>
<li class="">
<a href="../UnexpectedError">UnexpectedError</a>
</li>
<li class="">
<a href="../use">use</a>
</li>
<li class="">
<a href="../withError">withError</a>
</li>
</ul>
</nav>
<div class="main" tabindex="-1">
<div class="content">
<h1 id="expect-promise-settle-">expect.promise.settle(...)</h1>
<p>This method will find all promises in the given structure and return a
promise that will be fulfilled when all of the promises have been rejected or
fulfilled. When the returned promise is fulfilled, you can use
<a href="https://github.com/petkaantonov/bluebird/blob/master/API.md#synchronous-inspection">synchronous inspection</a>
on each of the promises to read their values.</p>
<p>Let's make an asynchronous assertion that we can use for the examples:</p>
<div class="code lang-javascript">
<div>expect<span style="color: #000000">.</span><span style="color: #000000">addAssertion</span><span style="color: #000000">(</span><span style="color: #df5000">'to be a number after a short delay'</span><span style="color: #000000">,</span> <span style="color: #a71d5d">function</span> <span style="color: #000000">(</span>expect<span style="color: #000000">,</span> subject<span style="color: #000000">)</span> <span style="color: #000000">{</span></div>
<div> <span style="color: #a71d5d">return</span> expect<span style="color: #000000">.</span><span style="color: #000000">promise</span><span style="color: #000000">(</span><span style="color: #a71d5d">function</span> <span style="color: #000000">(</span>run<span style="color: #000000">)</span> <span style="color: #000000">{</span></div>
<div> <span style="color: #000000">setTimeout</span><span style="color: #000000">(</span><span style="color: #000000">run</span><span style="color: #000000">(</span><span style="color: #a71d5d">function</span> <span style="color: #000000">()</span> <span style="color: #000000">{</span></div>
<div> <span style="color: #000000">expect</span><span style="color: #000000">(</span>subject<span style="color: #000000">,</span> <span style="color: #df5000">'to be a number'</span><span style="color: #000000">);</span></div>
<div> <span style="color: #000000">}),</span> <span style="color: #0086b3">1</span><span style="color: #000000">);</span></div>
<div> <span style="color: #000000">});</span></div>
<div><span style="color: #000000">});</span></div>
</div><p>See the <a href="/api/promise">promise</a> documentation for more details on how
expect.promise works.</p>
<p>The following code snippet creates a promise that is rejected when any
promise in the nested structure is rejected. When the returned promise
is rejected it create an error report with the details.</p>
<div class="code lang-javascript">
<div><span style="color: #a71d5d">var</span> promises <span style="color: #a71d5d">=</span> <span style="color: #000000">{</span></div>
<div> foo<span style="color: #000000">:</span> <span style="color: #000000">expect</span><span style="color: #000000">(</span><span style="color: #df5000">'42'</span><span style="color: #000000">,</span> <span style="color: #df5000">'to be a number after a short delay'</span><span style="color: #000000">),</span></div>
<div> bar<span style="color: #000000">:</span> <span style="color: #000000">expect</span><span style="color: #000000">([</span><span style="color: #0086b3">0</span><span style="color: #000000">,</span> <span style="color: #0086b3">1</span><span style="color: #000000">,</span> <span style="color: #0086b3">2</span><span style="color: #000000">],</span> <span style="color: #df5000">'to have items satisfying'</span><span style="color: #000000">,</span></div>
<div> expect<span style="color: #000000">.</span><span style="color: #000000">it</span><span style="color: #000000">(</span><span style="color: #df5000">'to be a number after a short delay'</span><span style="color: #000000">)),</span></div>
<div> baz<span style="color: #000000">:</span> <span style="color: #000000">expect</span><span style="color: #000000">({</span> a<span style="color: #000000">:</span> <span style="color: #df5000">'1'</span><span style="color: #000000">,</span> b<span style="color: #000000">:</span> <span style="color: #0086b3">2</span> <span style="color: #000000">},</span> <span style="color: #df5000">'to have values satisfying'</span><span style="color: #000000">,</span></div>
<div> <span style="color: #df5000">'to be a number after a short delay'</span><span style="color: #000000">)</span></div>
<div><span style="color: #000000">};</span></div>
<div> </div>
<div><span style="color: #a71d5d">return</span> expect<span style="color: #000000">.</span>promise<span style="color: #000000">.</span><span style="color: #000000">all</span><span style="color: #000000">(</span>promises<span style="color: #000000">).</span><span style="color: #000000">caught</span><span style="color: #000000">(</span><span style="color: #a71d5d">function</span> <span style="color: #000000">()</span> <span style="color: #000000">{</span></div>
<div> <span style="color: #a71d5d">return</span> expect<span style="color: #000000">.</span>promise<span style="color: #000000">.</span><span style="color: #000000">settle</span><span style="color: #000000">(</span>promises<span style="color: #000000">).</span><span style="color: #000000">then</span><span style="color: #000000">(</span><span style="color: #a71d5d">function</span> <span style="color: #000000">()</span> <span style="color: #000000">{</span></div>
<div> expect<span style="color: #000000">.</span><span style="color: #000000">fail</span><span style="color: #000000">(</span><span style="color: #a71d5d">function</span> <span style="color: #000000">(</span>output<span style="color: #000000">)</span> <span style="color: #000000">{</span></div>
<div> output<span style="color: #000000">.</span><span style="color: #000000">text</span><span style="color: #000000">(</span><span style="color: #df5000">'{'</span><span style="color: #000000">).</span><span style="color: #000000">nl</span><span style="color: #000000">();</span></div>
<div> output<span style="color: #000000">.</span><span style="color: #000000">indentLines</span><span style="color: #000000">();</span></div>
<div> Object<span style="color: #000000">.</span><span style="color: #000000">keys</span><span style="color: #000000">(</span>promises<span style="color: #000000">).</span><span style="color: #000000">forEach</span><span style="color: #000000">(</span><span style="color: #a71d5d">function</span> <span style="color: #000000">(</span>key<span style="color: #000000">,</span> index<span style="color: #000000">)</span> <span style="color: #000000">{</span></div>
<div> output<span style="color: #000000">.</span><span style="color: #000000">i</span><span style="color: #000000">().</span><span style="color: #000000">jsKey</span><span style="color: #000000">(</span>key<span style="color: #000000">).</span><span style="color: #000000">text</span><span style="color: #000000">(</span><span style="color: #df5000">':'</span><span style="color: #000000">).</span><span style="color: #000000">sp</span><span style="color: #000000">();</span></div>
<div> <span style="color: #a71d5d">if</span> <span style="color: #000000">(</span>promises<span style="color: #000000">[</span>key<span style="color: #000000">].</span><span style="color: #000000">isFulfilled</span><span style="color: #000000">())</span> <span style="color: #000000">{</span></div>
<div> output<span style="color: #000000">.</span><span style="color: #000000">success</span><span style="color: #000000">(</span><span style="color: #df5000">'✓'</span><span style="color: #000000">);</span></div>
<div> <span style="color: #000000">}</span> <span style="color: #a71d5d">else</span> <span style="color: #000000">{</span></div>
<div> output<span style="color: #000000">.</span><span style="color: #000000">error</span><span style="color: #000000">(</span><span style="color: #df5000">'⨯ '</span><span style="color: #000000">).</span><span style="color: #000000">block</span><span style="color: #000000">(</span>promises<span style="color: #000000">[</span>key<span style="color: #000000">].</span><span style="color: #000000">reason</span><span style="color: #000000">().</span><span style="color: #000000">getErrorMessage</span><span style="color: #000000">({</span> output<span style="color: #000000">:</span> output <span style="color: #000000">}));</span></div>
<div> <span style="color: #000000">}</span></div>
<div> output<span style="color: #000000">.</span><span style="color: #000000">nl</span><span style="color: #000000">();</span></div>
<div> <span style="color: #000000">});</span></div>
<div> output<span style="color: #000000">.</span><span style="color: #000000">outdentLines</span><span style="color: #000000">();</span></div>
<div> output<span style="color: #000000">.</span><span style="color: #000000">text</span><span style="color: #000000">(</span><span style="color: #df5000">'}'</span><span style="color: #000000">);</span></div>
<div> <span style="color: #000000">});</span></div>
<div> <span style="color: #000000">});</span></div>
<div><span style="color: #000000">});</span></div>
</div><div class="output">
<div>{</div>
<div> <span style="color: #555">foo</span>: <span style="color: red; font-weight: bold">⨯ </span><div style="display: inline-block; vertical-align: top">
<div><span style="color: red; font-weight: bold">expected</span> <span style="color: #df5000">'42'</span> <span style="color: red; font-weight: bold">to be a number after a short delay</span></div>
</div></div>
<div> <span style="color: #555">bar</span>: <span style="color: green; font-weight: bold">✓</span></div>
<div> <span style="color: #555">baz</span>: <span style="color: red; font-weight: bold">⨯ </span><div style="display: inline-block; vertical-align: top">
<div><span style="color: red; font-weight: bold">expected</span> { <span style="color: #555">a</span>: <span style="color: #df5000">'1'</span>, <span style="color: #555">b</span>: <span style="color: #0086b3">2</span> }</div>
<div><span style="color: red; font-weight: bold">to have values satisfying</span> <span style="color: #df5000">'to be a number after a short delay'</span></div>
<div> </div>
<div>{</div>
<div> <div style="display: inline-block; vertical-align: top">
<div><span style="color: #555">a</span>: <div style="display: inline-block; vertical-align: top">
<div><span style="color: #df5000">'1'</span>,</div>
</div> <div style="display: inline-block; vertical-align: top">
<div><span style="color: red; font-weight: bold">//</span></div>
</div> <div style="display: inline-block; vertical-align: top">
<div><span style="color: red; font-weight: bold">should be a number after a short delay</span></div>
</div></div>
</div></div>
<div> <div style="display: inline-block; vertical-align: top">
<div><span style="color: #555">b</span>: <span style="color: #0086b3">2</span></div>
</div></div>
<div>}</div>
</div></div>
<div>}</div>
</div>
</div>
</div>
</section>
<script type="text/javascript">
baseUrl = '../..';
</script>
<script type="text/javascript" src="../../static/toggleSidebar.js"></script>
<script type="text/javascript" src="../../static/rememberScrollPosition.js"></script>
<script type="text/javascript" src="../../static/focusMain.js"></script>
<script type="text/javascript" src="../../static/search.js"></script>
</body>
</html>