UNPKG

unexpected

Version:
368 lines (300 loc) 22.6 kB
<!DOCTYPE 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>object - to have properties</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="/assertions/any/to-be-a/">Assertions</a></li> <li class=""><a href="/api/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="assertions"> <nav id="assertion-menu" class="sidebar js-remember-scroll-position"> <ul> <li> <h3>any</h3> <ul> <li class=""> <a href="/assertions/any/to-be-a/">to be a</a> </li> <li class=""> <a href="/assertions/any/to-be-falsy/">to be falsy</a> </li> <li class=""> <a href="/assertions/any/to-be-null/">to be null</a> </li> <li class=""> <a href="/assertions/any/to-be-ok/">to be ok</a> </li> <li class=""> <a href="/assertions/any/to-be-truthy/">to be truthy</a> </li> <li class=""> <a href="/assertions/any/to-be-undefined/">to be undefined</a> </li> <li class=""> <a href="/assertions/any/to-be/">to be</a> </li> <li class=""> <a href="/assertions/any/to-equal/">to equal</a> </li> <li class=""> <a href="/assertions/any/to-satisfy/">to satisfy</a> </li> <li class=""> <a href="/assertions/any/to-be-defined/">to be defined</a> </li> </ul> </li> <li> <h3>array</h3> <ul> <li class=""> <a href="/assertions/array/to-be-an-array-whose-items-satisfy/">to be an array whose items satisfy</a> </li> <li class=""> <a href="/assertions/array/to-be-empty/">to be empty</a> </li> <li class=""> <a href="/assertions/array/to-be-non-empty/">to be non-empty</a> </li> <li class=""> <a href="/assertions/array/to-contain/">to contain</a> </li> <li class=""> <a href="/assertions/array/to-have-length/">to have length</a> </li> </ul> </li> <li> <h3>boolean</h3> <ul> <li class=""> <a href="/assertions/boolean/to-be-false/">to be false</a> </li> <li class=""> <a href="/assertions/boolean/to-be-true/">to be true</a> </li> </ul> </li> <li> <h3>function</h3> <ul> <li class=""> <a href="/assertions/function/to-have-arity/">to have arity</a> </li> <li class=""> <a href="/assertions/function/to-throw/">to throw</a> </li> </ul> </li> <li> <h3>number</h3> <ul> <li class=""> <a href="/assertions/number/to-be-greater-than/">to be greater than</a> </li> <li class=""> <a href="/assertions/number/to-be-infinite/">to be infinite</a> </li> <li class=""> <a href="/assertions/number/to-be-close-to/">to be close to</a> </li> <li class=""> <a href="/assertions/number/to-be-finite/">to be finite</a> </li> <li class=""> <a href="/assertions/number/to-be-greater-than-or-equal-to/">to be greater than or equal to</a> </li> <li class=""> <a href="/assertions/number/to-be-NaN/">to be NaN</a> </li> <li class=""> <a href="/assertions/number/to-be-less-than-or-equal-to/">to be less than or equal to</a> </li> <li class=""> <a href="/assertions/number/to-be-less-than/">to be less than</a> </li> <li class=""> <a href="/assertions/number/to-be-negative/">to be negative</a> </li> <li class=""> <a href="/assertions/number/to-be-positive/">to be positive</a> </li> <li class=""> <a href="/assertions/number/to-be-within/">to be within</a> </li> </ul> </li> <li> <h3>object</h3> <ul> <li class=""> <a href="/assertions/object/to-be-a-map-whose-keys-satisfy/">to be a map whose keys satisfy</a> </li> <li class=""> <a href="/assertions/object/to-be-a-map-whose-values-satisfy/">to be a map whose values satisfy</a> </li> <li class=""> <a href="/assertions/object/to-be-canonical/">to be canonical</a> </li> <li class=""> <a href="/assertions/object/to-have-key/">to have key</a> </li> <li class=""> <a href="/assertions/object/to-have-keys/">to have keys</a> </li> <li class="active"> <a href="/assertions/object/to-have-properties/">to have properties</a> </li> <li class=""> <a href="/assertions/object/to-have-property/">to have property</a> </li> </ul> </li> <li> <h3>string</h3> <ul> <li class=""> <a href="/assertions/string/to-be-non-empty/">to be non-empty</a> </li> <li class=""> <a href="/assertions/string/to-be-empty/">to be empty</a> </li> <li class=""> <a href="/assertions/string/to-be-greater-than/">to be greater than</a> </li> <li class=""> <a href="/assertions/string/to-be-less-than-or-equal-to/">to be less than or equal to</a> </li> <li class=""> <a href="/assertions/string/to-be-less-than/">to be less than</a> </li> <li class=""> <a href="/assertions/string/to-be-greater-than-or-equal-to/">to be greater than or equal to</a> </li> <li class=""> <a href="/assertions/string/to-be-within/">to be within</a> </li> <li class=""> <a href="/assertions/string/to-be/">to be</a> </li> <li class=""> <a href="/assertions/string/to-contain/">to contain</a> </li> <li class=""> <a href="/assertions/string/to-have-length/">to have length</a> </li> <li class=""> <a href="/assertions/string/to-match/">to match</a> </li> </ul> </li> </ul> </nav> <div class="main"> <div class="content"> <p>Assert presence of properties in an object.</p> <div class="code lang-javascript"> <div><span style="color: #000000">expect</span><span style="color: #000000">({</span>&nbsp;a<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'a'</span><span style="color: #000000">,</span>&nbsp;b<span style="color: #000000">:</span>&nbsp;<span style="color: #000000">{</span>&nbsp;c<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'c'</span>&nbsp;<span style="color: #000000">},</span>&nbsp;d<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'d'</span>&nbsp;<span style="color: #000000">},</span>&nbsp;<span style="color: #df5000">'to&nbsp;have&nbsp;properties'</span><span style="color: #000000">,</span>&nbsp;<span style="color: #000000">[</span><span style="color: #df5000">'a'</span><span style="color: #000000">,</span>&nbsp;<span style="color: #df5000">'b'</span><span style="color: #000000">]);</span></div> <div><span style="color: #000000">expect</span><span style="color: #000000">({</span>&nbsp;a<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'a'</span><span style="color: #000000">,</span>&nbsp;b<span style="color: #000000">:</span>&nbsp;<span style="color: #000000">{</span>&nbsp;c<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'c'</span>&nbsp;<span style="color: #000000">},</span>&nbsp;d<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'d'</span>&nbsp;<span style="color: #000000">},</span>&nbsp;<span style="color: #df5000">'to&nbsp;have&nbsp;properties'</span><span style="color: #000000">,</span>&nbsp;<span style="color: #000000">{</span></div> <div>&nbsp;&nbsp;&nbsp;&nbsp;a<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'a'</span><span style="color: #000000">,</span></div> <div>&nbsp;&nbsp;&nbsp;&nbsp;b<span style="color: #000000">:</span>&nbsp;<span style="color: #000000">{</span>&nbsp;c<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'c'</span>&nbsp;<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>&nbsp;<span style="color: #df5000">'a'</span><span style="color: #000000">,</span>&nbsp;<span style="color: #000000">{</span>&nbsp;c<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'c'</span>&nbsp;<span style="color: #000000">},</span>&nbsp;<span style="color: #df5000">'d'</span>&nbsp;<span style="color: #000000">],</span>&nbsp;<span style="color: #df5000">'to&nbsp;have&nbsp;properties'</span><span style="color: #000000">,</span>&nbsp;<span style="color: #000000">{</span></div> <div>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0086b3">1</span><span style="color: #000000">:</span>&nbsp;<span style="color: #000000">{</span>&nbsp;c<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'c'</span>&nbsp;<span style="color: #000000">},</span></div> <div>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0086b3">2</span><span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'d'</span></div> <div><span style="color: #000000">});</span></div> </div><p>Using the <code>own</code> flag, you can assert presence of an own properties.</p> <div class="code lang-javascript"> <div><span style="color: #000000">expect</span><span style="color: #000000">({</span>&nbsp;a<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'a'</span><span style="color: #000000">,</span>&nbsp;b<span style="color: #000000">:</span>&nbsp;<span style="color: #000000">{</span>&nbsp;c<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'c'</span>&nbsp;<span style="color: #000000">},</span>&nbsp;d<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'d'</span>&nbsp;<span style="color: #000000">},</span>&nbsp;<span style="color: #df5000">'to&nbsp;have&nbsp;own&nbsp;properties'</span><span style="color: #000000">,</span>&nbsp;<span style="color: #000000">[</span><span style="color: #df5000">'a'</span><span style="color: #000000">,</span>&nbsp;<span style="color: #df5000">'b'</span><span style="color: #000000">]);</span></div> <div><span style="color: #000000">expect</span><span style="color: #000000">({</span>&nbsp;a<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'a'</span><span style="color: #000000">,</span>&nbsp;b<span style="color: #000000">:</span>&nbsp;<span style="color: #000000">{</span>&nbsp;c<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'c'</span>&nbsp;<span style="color: #000000">},</span>&nbsp;d<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'d'</span>&nbsp;<span style="color: #000000">},</span>&nbsp;<span style="color: #df5000">'to&nbsp;have&nbsp;own&nbsp;properties'</span><span style="color: #000000">,</span>&nbsp;<span style="color: #000000">{</span></div> <div>&nbsp;&nbsp;&nbsp;&nbsp;a<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'a'</span><span style="color: #000000">,</span></div> <div>&nbsp;&nbsp;&nbsp;&nbsp;b<span style="color: #000000">:</span>&nbsp;<span style="color: #000000">{</span>&nbsp;c<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'c'</span>&nbsp;<span style="color: #000000">}</span></div> <div><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>&nbsp;a<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'f00'</span><span style="color: #000000">,</span>&nbsp;b<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'bar'</span>&nbsp;<span style="color: #000000">},</span>&nbsp;<span style="color: #df5000">'to&nbsp;have&nbsp;own&nbsp;properties'</span><span style="color: #000000">,</span>&nbsp;<span style="color: #000000">{</span></div> <div>&nbsp;&nbsp;a<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'foo'</span><span style="color: #000000">,</span></div> <div>&nbsp;&nbsp;c<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'baz'</span></div> <div><span style="color: #000000">});</span></div> </div><div class="output"> <div><span style="color: red; font-weight: bold">expected</span>&nbsp;{&nbsp;<span style="color: #555">a</span>:&nbsp;<span style="color: #df5000">'f00'</span>,&nbsp;<span style="color: #555">b</span>:&nbsp;<span style="color: #df5000">'bar'</span>&nbsp;}&nbsp;<span style="color: red; font-weight: bold">to&nbsp;have&nbsp;own&nbsp;properties</span>&nbsp;{&nbsp;<span style="color: #555">a</span>:&nbsp;<span style="color: #df5000">'foo'</span>,&nbsp;<span style="color: #555">c</span>:&nbsp;<span style="color: #df5000">'baz'</span>&nbsp;}</div> <div>&nbsp;</div> <div>{</div> <div>&nbsp;&nbsp;<div style="display: inline-block; vertical-align: top"> <div><span style="color: #555">a</span>:&nbsp;<div style="display: inline-block; vertical-align: top"> <div><span style="color: #df5000">'f00'</span>,</div> </div>&nbsp;<div style="display: inline-block; vertical-align: top"> <div><span style="color: red; font-weight: bold">//</span></div> <div><span style="color: red; font-weight: bold">//</span></div> <div><span style="color: red; font-weight: bold">//</span></div> </div>&nbsp;<div style="display: inline-block; vertical-align: top"> <div><span style="color: red; font-weight: bold">should&nbsp;equal</span>&nbsp;<div style="display: inline-block; vertical-align: top"> <div><span style="color: #df5000">'foo'</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="background-color: red; color: white">f00</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="background-color: green; color: white">foo</span></div> </div></div> </div></div> </div></div> <div>&nbsp;&nbsp;<div style="display: inline-block; vertical-align: top"> <div><span style="color: #555">b</span>:&nbsp;<span style="color: #df5000">'bar'</span>,</div> </div></div> <div>&nbsp;&nbsp;<div style="display: inline-block; vertical-align: top"> <div><span style="color: #555">c</span>:&nbsp;<div style="display: inline-block; vertical-align: top"> <div><span style="color: #0086b3">undefined</span></div> </div>&nbsp;<div style="display: inline-block; vertical-align: top"> <div><span style="color: red; font-weight: bold">//</span></div> </div>&nbsp;<div style="display: inline-block; vertical-align: top"> <div><span style="color: red; font-weight: bold">should&nbsp;equal</span>&nbsp;<div style="display: inline-block; vertical-align: top"> <div><span style="color: #df5000">'baz'</span></div> </div></div> </div></div> </div></div> <div>}</div> </div><p>This assertion can be negated using the <code>not</code> flag:</p> <div class="code lang-javascript"> <div><span style="color: #000000">expect</span><span style="color: #000000">({</span>&nbsp;a<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'a'</span><span style="color: #000000">,</span>&nbsp;b<span style="color: #000000">:</span>&nbsp;<span style="color: #000000">{</span>&nbsp;c<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'c'</span>&nbsp;<span style="color: #000000">},</span>&nbsp;d<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'d'</span>&nbsp;<span style="color: #000000">},</span>&nbsp;<span style="color: #df5000">'not&nbsp;to&nbsp;have&nbsp;properties'</span><span style="color: #000000">,</span>&nbsp;<span style="color: #000000">[</span><span style="color: #df5000">'k'</span><span style="color: #000000">,</span>&nbsp;<span style="color: #df5000">'l'</span><span style="color: #000000">]);</span></div> <div><span style="color: #000000">expect</span><span style="color: #000000">(</span>Object<span style="color: #000000">.</span><span style="color: #000000">create</span><span style="color: #000000">({</span>&nbsp;a<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'a'</span><span style="color: #000000">,</span>&nbsp;b<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'b'</span>&nbsp;<span style="color: #000000">}),</span>&nbsp;<span style="color: #df5000">'not&nbsp;to&nbsp;have&nbsp;own&nbsp;properties'</span><span style="color: #000000">,</span>&nbsp;<span style="color: #000000">[</span><span style="color: #df5000">'a'</span><span style="color: #000000">,</span>&nbsp;<span style="color: #df5000">'b'</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>&nbsp;a<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'a'</span><span style="color: #000000">,</span>&nbsp;b<span style="color: #000000">:</span>&nbsp;<span style="color: #000000">{</span>&nbsp;c<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'c'</span>&nbsp;<span style="color: #000000">},</span>&nbsp;d<span style="color: #000000">:</span>&nbsp;<span style="color: #df5000">'d'</span>&nbsp;<span style="color: #000000">},</span>&nbsp;<span style="color: #df5000">'not&nbsp;to&nbsp;have&nbsp;properties'</span><span style="color: #000000">,</span>&nbsp;<span style="color: #000000">[</span><span style="color: #df5000">'b'</span><span style="color: #000000">,</span>&nbsp;<span style="color: #df5000">'d'</span><span style="color: #000000">]);</span></div> </div><div class="output"> <div><span style="color: red; font-weight: bold">expected</span>&nbsp;{&nbsp;<span style="color: #555">a</span>:&nbsp;<span style="color: #df5000">'a'</span>,&nbsp;<span style="color: #555">b</span>:&nbsp;{&nbsp;<span style="color: #555">c</span>:&nbsp;<span style="color: #df5000">'c'</span>&nbsp;},&nbsp;<span style="color: #555">d</span>:&nbsp;<span style="color: #df5000">'d'</span>&nbsp;}&nbsp;<span style="color: red; font-weight: bold">not&nbsp;to&nbsp;have&nbsp;properties</span>&nbsp;[&nbsp;<span style="color: #df5000">'b'</span>,&nbsp;<span style="color: #df5000">'d'</span>&nbsp;]</div> </div> </div> </div> </section> <script type="text/javascript" src="/static/toggleSidebar.js"></script> <script type="text/javascript" src="/static/rememberScrollPosition.js"></script> <script type="text/javascript" src="/static/search.js"></script> </body> </html>