unexpected
Version:
Extensible BDD assertion toolkit
510 lines (414 loc) • 32.2 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>function - to error</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="active"><a href="../../any/to-be">Assertions</a></li>
<li class=""><a href="../../../api/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="assertions">
<nav id="assertion-menu" class="sidebar js-remember-scroll-position">
<ul>
<li>
<h3>any</h3>
<ul>
<li class="">
<a href="../../any/to-be">to be</a>
</li>
<li class="">
<a href="../../any/to-be-a">to be a</a>
</li>
<li class="">
<a href="../../any/to-be-defined">to be defined</a>
</li>
<li class="">
<a href="../../any/to-be-falsy">to be falsy</a>
</li>
<li class="">
<a href="../../any/to-be-null">to be null</a>
</li>
<li class="">
<a href="../../any/to-be-ok">to be ok</a>
</li>
<li class="">
<a href="../../any/to-be-truthy">to be truthy</a>
</li>
<li class="">
<a href="../../any/to-be-undefined">to be undefined</a>
</li>
<li class="">
<a href="../../any/to-equal">to equal</a>
</li>
<li class="">
<a href="../../any/to-satisfy">to satisfy</a>
</li>
<li class="">
<a href="../../any/when-passed-as-parameter-to">when passed as parameter to</a>
</li>
</ul>
</li>
<li>
<h3>string</h3>
<ul>
<li class="">
<a href="../../string/to-be">to be</a>
</li>
<li class="">
<a href="../../string/to-be-empty">to be empty</a>
</li>
<li class="">
<a href="../../string/to-be-greater-than">to be greater than</a>
</li>
<li class="">
<a href="../../string/to-be-greater-than-or-equal-to">to be greater than or equal to</a>
</li>
<li class="">
<a href="../../string/to-be-less-than">to be less than</a>
</li>
<li class="">
<a href="../../string/to-be-less-than-or-equal-to">to be less than or equal to</a>
</li>
<li class="">
<a href="../../string/to-be-non-empty">to be non-empty</a>
</li>
<li class="">
<a href="../../string/to-be-within">to be within</a>
</li>
<li class="">
<a href="../../string/to-begin-with">to begin with</a>
</li>
<li class="">
<a href="../../string/to-contain">to contain</a>
</li>
<li class="">
<a href="../../string/to-end-with">to end with</a>
</li>
<li class="">
<a href="../../string/to-have-length">to have length</a>
</li>
<li class="">
<a href="../../string/to-match">to match</a>
</li>
</ul>
</li>
<li>
<h3>number</h3>
<ul>
<li class="">
<a href="../../number/to-be-close-to">to be close to</a>
</li>
<li class="">
<a href="../../number/to-be-finite">to be finite</a>
</li>
<li class="">
<a href="../../number/to-be-greater-than">to be greater than</a>
</li>
<li class="">
<a href="../../number/to-be-greater-than-or-equal-to">to be greater than or equal to</a>
</li>
<li class="">
<a href="../../number/to-be-infinite">to be infinite</a>
</li>
<li class="">
<a href="../../number/to-be-less-than">to be less than</a>
</li>
<li class="">
<a href="../../number/to-be-less-than-or-equal-to">to be less than or equal to</a>
</li>
<li class="">
<a href="../../number/to-be-NaN">to be NaN</a>
</li>
<li class="">
<a href="../../number/to-be-negative">to be negative</a>
</li>
<li class="">
<a href="../../number/to-be-positive">to be positive</a>
</li>
<li class="">
<a href="../../number/to-be-within">to be within</a>
</li>
</ul>
</li>
<li>
<h3>boolean</h3>
<ul>
<li class="">
<a href="../../boolean/to-be-false">to be false</a>
</li>
<li class="">
<a href="../../boolean/to-be-true">to be true</a>
</li>
</ul>
</li>
<li>
<h3>object</h3>
<ul>
<li class="">
<a href="../../object/to-be-canonical">to be canonical</a>
</li>
<li class="">
<a href="../../object/to-have-key">to have key</a>
</li>
<li class="">
<a href="../../object/to-have-keys">to have keys</a>
</li>
<li class="">
<a href="../../object/to-have-keys-satisfying">to have keys satisfying</a>
</li>
<li class="">
<a href="../../object/to-have-properties">to have properties</a>
</li>
<li class="">
<a href="../../object/to-have-property">to have property</a>
</li>
<li class="">
<a href="../../object/to-have-values-satisfying">to have values satisfying</a>
</li>
</ul>
</li>
<li>
<h3>function</h3>
<ul>
<li class="">
<a href="../to-call-the-callback">to call the callback</a>
</li>
<li class="">
<a href="../to-call-the-callback-with-error">to call the callback with error</a>
</li>
<li class="">
<a href="../to-call-the-callback-without-error">to call the callback without error</a>
</li>
<li class="active">
<a href="">to error</a>
</li>
<li class="">
<a href="../to-have-arity">to have arity</a>
</li>
<li class="">
<a href="../to-throw">to throw</a>
</li>
</ul>
</li>
<li>
<h3>Promise</h3>
<ul>
<li class="">
<a href="../../Promise/to-be-fulfilled">to be fulfilled</a>
</li>
<li class="">
<a href="../../Promise/to-be-rejected">to be rejected</a>
</li>
<li class="">
<a href="../../Promise/when-fulfilled">when fulfilled</a>
</li>
<li class="">
<a href="../../Promise/when-rejected">when rejected</a>
</li>
</ul>
</li>
<li>
<h3>Error</h3>
<ul>
<li class="">
<a href="../../Error/to-have-message">to have message</a>
</li>
</ul>
</li>
<li>
<h3>array-like</h3>
<ul>
<li class="">
<a href="../../array-like/to-be-empty">to be empty</a>
</li>
<li class="">
<a href="../../array-like/to-be-non-empty">to be non-empty</a>
</li>
<li class="">
<a href="../../array-like/to-contain">to contain</a>
</li>
<li class="">
<a href="../../array-like/to-have-items-satisfying">to have items satisfying</a>
</li>
<li class="">
<a href="../../array-like/to-have-length">to have length</a>
</li>
<li class="">
<a href="../../array-like/when-passed-as-parameters-to">when passed as parameters to</a>
</li>
</ul>
</li>
<li>
<h3>Buffer</h3>
<ul>
<li class="">
<a href="../../Buffer/when-decoded-as">when decoded as</a>
</li>
</ul>
</li>
</ul>
</nav>
<div class="main" tabindex="-1">
<div class="content">
<h1>to error</h1>
<p>Asserts that the function throws an error, or returns a promise that
is rejected.</p>
<div class="code lang-javascript">
<div><span style="color: #a71d5d">function</span> <span style="color: #000000">willBeRejected</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><span style="color: #000000">promise</span><span style="color: #000000">(</span><span style="color: #a71d5d">function</span> <span style="color: #000000">(</span>resolve<span style="color: #000000">,</span> reject<span style="color: #000000">)</span> <span style="color: #000000">{</span></div>
<div> <span style="color: #000000">reject</span><span style="color: #000000">(</span><span style="color: #a71d5d">new</span> Error<span style="color: #000000">(</span><span style="color: #df5000">'The reject message'</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: #a71d5d">function</span> <span style="color: #000000">willThrow</span><span style="color: #000000">()</span> <span style="color: #000000">{</span></div>
<div> <span style="color: #a71d5d">throw</span> <span style="color: #a71d5d">new</span> Error<span style="color: #000000">(</span><span style="color: #df5000">'The error message'</span><span style="color: #000000">);</span></div>
<div><span style="color: #000000">}</span></div>
<div><span style="color: #000000">expect</span><span style="color: #000000">(</span>willBeRejected<span style="color: #000000">,</span> <span style="color: #df5000">'to error'</span><span style="color: #000000">);</span></div>
<div><span style="color: #000000">expect</span><span style="color: #000000">(</span>willThrow<span style="color: #000000">,</span> <span style="color: #df5000">'to error'</span><span style="color: #000000">);</span></div>
</div><p>In case of a failing expectation you get the following output:</p>
<div class="code lang-javascript">
<div><span style="color: #a71d5d">function</span> <span style="color: #000000">willNotBeRejected</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><span style="color: #000000">promise</span><span style="color: #000000">(</span><span style="color: #a71d5d">function</span> <span style="color: #000000">(</span>resolve<span style="color: #000000">,</span> reject<span style="color: #000000">)</span> <span style="color: #000000">{</span></div>
<div> <span style="color: #000000">resolve</span><span style="color: #000000">(</span><span style="color: #df5000">'Hello world'</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">expect</span><span style="color: #000000">(</span>willNotBeRejected<span style="color: #000000">,</span> <span style="color: #df5000">'to error'</span><span style="color: #000000">);</span></div>
</div><div class="output">
<div><span style="color: red; font-weight: bold">expected</span></div>
<div><span style="color: #a71d5d">function</span> <span style="color: #000000">willNotBeRejected</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><span style="color: #000000">promise</span><span style="color: #000000">(</span><span style="color: #a71d5d">function</span> <span style="color: #000000">(</span>resolve<span style="color: #000000">,</span> reject<span style="color: #000000">)</span> <span style="color: #000000">{</span></div>
<div> <span style="color: #000000">resolve</span><span style="color: #000000">(</span><span style="color: #df5000">'Hello world'</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: red; font-weight: bold">to error</span></div>
</div><p>You can assert the error message is a given string if you provide a
string as the second parameter.</p>
<div class="code lang-javascript">
<div><span style="color: #000000">expect</span><span style="color: #000000">(</span>willBeRejected<span style="color: #000000">,</span> <span style="color: #df5000">'to error'</span><span style="color: #000000">,</span> <span style="color: #df5000">'The reject message'</span><span style="color: #000000">);</span></div>
</div><div class="code lang-javascript">
<div><span style="color: #000000">expect</span><span style="color: #000000">(</span>willBeRejected<span style="color: #000000">,</span> <span style="color: #df5000">'to error'</span><span style="color: #000000">,</span> <span style="color: #df5000">'The error message'</span><span style="color: #000000">);</span></div>
</div><div class="output">
<div><span style="color: red; font-weight: bold">expected</span></div>
<div><span style="color: #a71d5d">function</span> <span style="color: #000000">willBeRejected</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><span style="color: #000000">promise</span><span style="color: #000000">(</span><span style="color: #a71d5d">function</span> <span style="color: #000000">(</span>resolve<span style="color: #000000">,</span> reject<span style="color: #000000">)</span> <span style="color: #000000">{</span></div>
<div> <span style="color: #000000">reject</span><span style="color: #000000">(</span><span style="color: #a71d5d">new</span> Error<span style="color: #000000">(</span><span style="color: #df5000">'The reject message'</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: red; font-weight: bold">to error</span> <span style="color: #df5000">'The error message'</span></div>
<div> <div style="display: inline-block; vertical-align: top">
<div><span style="color: red; font-weight: bold">expected</span> Error(<span style="color: #df5000">'The reject message'</span>) <span style="color: red; font-weight: bold">to satisfy</span> <span style="color: #df5000">'The error message'</span></div>
<div> </div>
<div><div style="display: inline-block; vertical-align: top">
<div><span style="color: red">-</span></div>
</div><div style="display: inline-block; vertical-align: top">
<div><span style="color: red">The </span><span style="background-color: red; color: white">reject</span><span style="color: red"> message</span></div>
</div></div>
<div><div style="display: inline-block; vertical-align: top">
<div><span style="color: green">+</span></div>
</div><div style="display: inline-block; vertical-align: top">
<div><span style="color: green">The </span><span style="background-color: green; color: white">error</span><span style="color: green"> message</span></div>
</div></div>
</div></div>
</div><p>By providing a regular expression as the second parameter you can
assert the error message matches the given regular expression.</p>
<div class="code lang-javascript">
<div><span style="color: #000000">expect</span><span style="color: #000000">(</span>willBeRejected<span style="color: #000000">,</span> <span style="color: #df5000">'to error'</span><span style="color: #000000">,</span> <span style="color: #183691">/reject message/</span><span style="color: #000000">);</span></div>
</div><p>In case of a failing expectation you get the following output:</p>
<div class="code lang-javascript">
<div><span style="color: #000000">expect</span><span style="color: #000000">(</span>willBeRejected<span style="color: #000000">,</span> <span style="color: #df5000">'to error'</span><span style="color: #000000">,</span> <span style="color: #183691">/error message/</span><span style="color: #000000">);</span></div>
</div><div class="output">
<div><span style="color: red; font-weight: bold">expected</span></div>
<div><span style="color: #a71d5d">function</span> <span style="color: #000000">willBeRejected</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><span style="color: #000000">promise</span><span style="color: #000000">(</span><span style="color: #a71d5d">function</span> <span style="color: #000000">(</span>resolve<span style="color: #000000">,</span> reject<span style="color: #000000">)</span> <span style="color: #000000">{</span></div>
<div> <span style="color: #000000">reject</span><span style="color: #000000">(</span><span style="color: #a71d5d">new</span> Error<span style="color: #000000">(</span><span style="color: #df5000">'The reject message'</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: red; font-weight: bold">to error</span> <span style="color: #183691">/error message/</span></div>
<div> <div style="display: inline-block; vertical-align: top">
<div><span style="color: red; font-weight: bold">expected</span> Error(<span style="color: #df5000">'The reject message'</span>) <span style="color: red; font-weight: bold">to satisfy</span> <span style="color: #183691">/error message/</span></div>
</div></div>
</div><p>You can also negate the check, and verify that the function will not
error out. When negating the assertion, you cannot provide a message.</p>
<div class="code lang-javascript">
<div><span style="color: #000000">expect</span><span style="color: #000000">(</span>willNotBeRejected<span style="color: #000000">,</span> <span style="color: #df5000">'not to error'</span><span style="color: #000000">);</span></div>
</div><p>In case of a failing expectation you get the following output:</p>
<div class="code lang-javascript">
<div><span style="color: #000000">expect</span><span style="color: #000000">(</span>willBeRejected<span style="color: #000000">,</span> <span style="color: #df5000">'not to error'</span><span style="color: #000000">);</span></div>
</div><div class="output">
<div><span style="color: red; font-weight: bold">expected</span></div>
<div><span style="color: #a71d5d">function</span> <span style="color: #000000">willBeRejected</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><span style="color: #000000">promise</span><span style="color: #000000">(</span><span style="color: #a71d5d">function</span> <span style="color: #000000">(</span>resolve<span style="color: #000000">,</span> reject<span style="color: #000000">)</span> <span style="color: #000000">{</span></div>
<div> <span style="color: #000000">reject</span><span style="color: #000000">(</span><span style="color: #a71d5d">new</span> Error<span style="color: #000000">(</span><span style="color: #df5000">'The reject message'</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: red; font-weight: bold">not to error</span></div>
<div> <div style="display: inline-block; vertical-align: top">
<div><span style="color: red; font-weight: bold">returned promise rejected with: </span>Error(<span style="color: #df5000">'The reject message'</span>)</div>
</div></div>
</div><p>You can pass in a function instead of the error message, and do more
assertions on the error.</p>
<div class="code lang-javascript">
<div><span style="color: #a71d5d">function</span> <span style="color: #000000">willBeRejectedAsync</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><span style="color: #000000">promise</span><span style="color: #000000">(</span><span style="color: #a71d5d">function</span> <span style="color: #000000">(</span>resolve<span style="color: #000000">,</span> reject<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: #a71d5d">function</span> <span style="color: #000000">()</span> <span style="color: #000000">{</span></div>
<div> <span style="color: #000000">reject</span><span style="color: #000000">(</span><span style="color: #a71d5d">new</span> Error<span style="color: #000000">(</span><span style="color: #df5000">'async error'</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> </div>
<div><span style="color: #a71d5d">return</span> <span style="color: #000000">expect</span><span style="color: #000000">(</span>willBeRejectedAsync<span style="color: #000000">,</span> <span style="color: #df5000">'to error'</span><span style="color: #000000">,</span> <span style="color: #a71d5d">function</span> <span style="color: #000000">(</span>e<span style="color: #000000">)</span> <span style="color: #000000">{</span></div>
<div> <span style="color: #a71d5d">return</span> <span style="color: #000000">expect</span><span style="color: #000000">(</span>e<span style="color: #000000">.</span>message<span style="color: #000000">,</span> <span style="color: #df5000">'to equal'</span><span style="color: #000000">,</span> <span style="color: #df5000">'async error'</span><span style="color: #000000">);</span></div>
<div><span style="color: #000000">});</span></div>
</div><p>You can even do async assertions in the function that you pass in.</p>
<div class="code lang-javascript">
<div><span style="color: #a71d5d">var</span> errorCount <span style="color: #a71d5d">=</span> <span style="color: #0086b3">0</span><span style="color: #000000">;</span></div>
<div><span style="color: #a71d5d">function</span> <span style="color: #000000">willBeRejectedAsync</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><span style="color: #000000">promise</span><span style="color: #000000">(</span><span style="color: #a71d5d">function</span> <span style="color: #000000">(</span>resolve<span style="color: #000000">,</span> reject<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: #a71d5d">function</span> <span style="color: #000000">()</span> <span style="color: #000000">{</span></div>
<div> <span style="color: #a71d5d">var</span> error <span style="color: #a71d5d">=</span> <span style="color: #a71d5d">new</span> Error<span style="color: #000000">(</span><span style="color: #df5000">'async error'</span><span style="color: #000000">);</span></div>
<div> errorCount <span style="color: #a71d5d">+=</span> <span style="color: #0086b3">1</span><span style="color: #000000">;</span></div>
<div> error<span style="color: #000000">.</span>errorCount <span style="color: #a71d5d">=</span> errorCount<span style="color: #000000">;</span></div>
<div> <span style="color: #000000">reject</span><span style="color: #000000">(</span>error<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> </div>
<div><span style="color: #a71d5d">return</span> <span style="color: #000000">expect</span><span style="color: #000000">(</span>willBeRejectedAsync<span style="color: #000000">,</span> <span style="color: #df5000">'to error'</span><span style="color: #000000">,</span> <span style="color: #a71d5d">function</span> <span style="color: #000000">(</span>e<span style="color: #000000">)</span> <span style="color: #000000">{</span></div>
<div> <span style="color: #a71d5d">return</span> <span style="color: #000000">expect</span><span style="color: #000000">(</span>willBeRejectedAsync<span style="color: #000000">,</span> <span style="color: #df5000">'to error'</span><span style="color: #000000">,</span> <span style="color: #a71d5d">function</span> <span style="color: #000000">(</span>e2<span style="color: #000000">)</span> <span style="color: #000000">{</span></div>
<div> <span style="color: #a71d5d">return</span> <span style="color: #000000">expect</span><span style="color: #000000">(</span>e2<span style="color: #000000">.</span>errorCount<span style="color: #000000">,</span> <span style="color: #df5000">'to be greater than'</span><span style="color: #000000">,</span> e<span style="color: #000000">.</span>errorCount<span style="color: #000000">);</span></div>
<div> <span style="color: #000000">});</span></div>
<div><span style="color: #000000">});</span></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>