unexpected
Version:
Extensible BDD assertion toolkit
161 lines (137 loc) • 11.9 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.all</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>
</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="../fail">fail</a>
</li>
<li class="">
<a href="../installPlugin">installPlugin</a>
</li>
<li class="">
<a href="../promise">promise</a>
</li>
<li class="active">
<a href="">promise.all</a>
</li>
<li class="">
<a href="../promise-any">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="../withError">withError</a>
</li>
</ul>
</nav>
<div class="main" tabindex="-1">
<div class="content">
<h3 id="expect-promise-all-">expect.promise.all(...)</h3>
<p>This method will find all promises in the given structure and return a
promise that resolves when all of the promises in the structure have
resolved. If any of the promises are rejected the resulting promise
will be rejected with the same error.</p>
<p>This method is usually used in combination with
<a href="/api/promise-settle">expect.promise.settle</a>.</p>
<p>Let's make an asynchronous assertion that we can uses 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 resolves when all the
promises in the nested structure resolves.</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">all</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: #0086b3">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: #0086b3">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 is rejected when one
of the promises in the nested structure is 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">all</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: #0086b3">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">'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></div>
</div><div class="output">
<div><span style="color: red; font-weight: bold">failed expectation in</span> <div style="display: inline-block; vertical-align: top">
<div>{ <span style="color: #555">a</span>: <span style="color: #df5000">'0'</span>, <span style="color: #555">b</span>: <span style="color: #0086b3">1</span> }<span style="color: red; font-weight: bold">:</span></div>
</div></div>
<div> a: <div style="display: inline-block; vertical-align: top">
<div><span style="color: red; font-weight: bold">expected</span> <span style="color: #df5000">'0'</span> <span style="color: red; font-weight: bold">to be a number after a short delay</span></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>