unexpected
Version:
Extensible BDD assertion toolkit
231 lines (203 loc) • 20 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.any</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="active">
<a href="">promise.any</a>
</li>
<li class="">
<a href="../promise-settle">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-any-">expect.promise.any(...)</h1>
<p>This method will find all promises in the given structure and return a promise
that will be fulfilled when any of the promises in the structure have been
fulfilled. If all of the promises are rejected, the resulting promise will be
rejected with the errors.</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">0</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 will be fulfilled when any
of the promises in the nested structure has been fulfilled.</p>
<div class="code lang-javascript">
<div><span style="color: #a71d5d">return</span> expect<span style="color: #000000">.</span>promise<span style="color: #000000">.</span><span style="color: #000000">any</span><span style="color: #000000">({</span></div>
<div> foo<span style="color: #000000">:</span> <span style="color: #000000">[</span></div>
<div> <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> <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> </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><p>The following code snippet create a promise that will rejected when all
of the promises in the nested structure have been rejected:</p>
<div class="code lang-javascript">
<div><span style="color: #a71d5d">return</span> expect<span style="color: #000000">.</span>promise<span style="color: #000000">.</span><span style="color: #000000">any</span><span style="color: #000000">({</span></div>
<div> foo<span style="color: #000000">:</span> <span style="color: #000000">[</span></div>
<div> <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> <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: #df5000">'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> </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">'0'</span><span style="color: #000000">,</span> b<span style="color: #000000">:</span> <span style="color: #0086b3">1</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><span style="color: #000000">caught</span><span style="color: #000000">(</span><span style="color: #a71d5d">function</span> <span style="color: #000000">(</span>aggregateError<span style="color: #000000">)</span> <span style="color: #000000">{</span></div>
<div> <span style="color: #969896"> // Let's reformat the error a bit</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">error</span><span style="color: #000000">(</span>aggregateError<span style="color: #000000">.</span>message<span style="color: #000000">);</span></div>
<div> <span style="color: #a71d5d">var</span> errors <span style="color: #a71d5d">=</span> <span style="color: #000000">[];</span></div>
<div> <span style="color: #a71d5d">for</span> <span style="color: #000000">(</span><span style="color: #a71d5d">var</span> i <span style="color: #a71d5d">=</span> <span style="color: #0086b3">0</span><span style="color: #000000">;</span> i <span style="color: #a71d5d"><</span> aggregateError<span style="color: #000000">.</span>length<span style="color: #000000">;</span> i <span style="color: #a71d5d">+=</span> <span style="color: #0086b3">1</span><span style="color: #000000">)</span> <span style="color: #000000">{</span></div>
<div> errors<span style="color: #000000">.</span><span style="color: #000000">push</span><span style="color: #000000">(</span>aggregateError<span style="color: #000000">[</span>i<span style="color: #000000">]);</span></div>
<div> <span style="color: #000000">}</span></div>
<div> </div>
<div> errors<span style="color: #000000">.</span><span style="color: #000000">sort</span><span style="color: #000000">(</span><span style="color: #a71d5d">function</span> <span style="color: #000000">(</span>a<span style="color: #000000">,</span> b<span style="color: #000000">)</span> <span style="color: #000000">{</span><span style="color: #969896"> // Make the output stable</span></div>
<div> <span style="color: #a71d5d">if</span> <span style="color: #000000">(</span>a<span style="color: #000000">.</span>message <span style="color: #a71d5d"><</span> b<span style="color: #000000">.</span>message<span style="color: #000000">)</span> <span style="color: #a71d5d">return</span> <span style="color: #a71d5d">-</span><span style="color: #0086b3">1</span><span style="color: #000000">;</span></div>
<div> <span style="color: #a71d5d">if</span> <span style="color: #000000">(</span>a<span style="color: #000000">.</span>message <span style="color: #a71d5d">></span> b<span style="color: #000000">.</span>message<span style="color: #000000">)</span> <span style="color: #a71d5d">return</span> <span style="color: #0086b3">1</span><span style="color: #000000">;</span></div>
<div> <span style="color: #a71d5d">return</span> <span style="color: #0086b3">0</span><span style="color: #000000">;</span></div>
<div> <span style="color: #000000">});</span></div>
<div> </div>
<div> output<span style="color: #000000">.</span><span style="color: #000000">indentLines</span><span style="color: #000000">();</span></div>
<div> errors<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>e<span style="color: #000000">,</span> i<span style="color: #000000">)</span> <span style="color: #000000">{</span></div>
<div> output<span style="color: #000000">.</span><span style="color: #000000">nl</span><span style="color: #000000">().</span><span style="color: #000000">i</span><span style="color: #000000">().</span><span style="color: #000000">text</span><span style="color: #000000">(</span>i <span style="color: #a71d5d">+</span> <span style="color: #df5000">': '</span><span style="color: #000000">).</span><span style="color: #000000">block</span><span style="color: #000000">(</span>e<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> <span style="color: #000000">});</span></div>
<div><span style="color: #000000">});</span></div>
</div><div class="output">
<div><span style="color: red; font-weight: bold">aggregate error</span></div>
<div> 0: <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> 1: <div style="display: inline-block; vertical-align: top">
<div><span style="color: red; font-weight: bold">expected</span> [ <span style="color: #0086b3">0</span>, <span style="color: #df5000">'1'</span>, <span style="color: #0086b3">2</span> ]</div>
<div><span style="color: red; font-weight: bold">to have items satisfying</span> expect.it(<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: #0086b3">0</span>,</div>
</div></div>
<div> <div style="display: inline-block; vertical-align: top">
<div><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><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>
<div> <div style="display: inline-block; vertical-align: top">
<div><span style="color: #0086b3">2</span></div>
</div></div>
<div>]</div>
</div></div>
<div> 2: <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">'0'</span>, <span style="color: #555">b</span>: <span style="color: #0086b3">1</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">'0'</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">1</span></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>