unexpected
Version:
Extensible BDD assertion toolkit
254 lines (242 loc) • 18.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>Unexpected</title>
</head>
<body class="sidebar-hidden">
<header>
<div class="logo-icon"></div>
<nav>
<ul>
<li class="menu-toggle-item">
<button class="menu-toggle" onclick="toggleSidebar()"></button>
</li>
<li class="active"><a href="">Unexpected</a></li>
<li class=""><a href="assertions/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>
<div class="main theme-dark" tabindex="-1">
<div class="content">
<h1 id="welcome-to-unexpected">Welcome to Unexpected</h1>
<h1 id="the-extensible-bdd-assertion-toolkit">The extensible BDD assertion toolkit</h1>
<div class="code lang-javascript">
<div>expect<span style="color: #999">({</span> text<span style="color: #999">:</span> <span style="color: #3CDAFF">'f00!'</span> <span style="color: #999">},</span> <span style="color: #3CDAFF">'to equal'</span><span style="color: #999">,</span> <span style="color: #999">{</span> text<span style="color: #999">:</span> <span style="color: #3CDAFF">'foo!'</span> <span style="color: #999">});</span></div>
</div><div class="output">
<div><span style="color: red; font-weight: bold">expected</span> { <span style="color: #939393">text</span>: <span style="color: #3CDAFF">'f00!'</span> } <span style="color: red; font-weight: bold">to equal</span> { <span style="color: #939393">text</span>: <span style="color: #3CDAFF">'foo!'</span> }</div>
<div> </div>
<div>{</div>
<div> <div style="display: inline-block; vertical-align: top">
<div><span style="color: #939393">text</span>: <div style="display: inline-block; vertical-align: top">
<div><span style="color: #3CDAFF">'f00!'</span></div>
</div> <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> <div style="display: inline-block; vertical-align: top">
<div><span style="color: red; font-weight: bold">should equal</span> <div style="display: inline-block; vertical-align: top">
<div><span style="color: #3CDAFF">'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: black">f00</span><span style="color: red">!</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: black">foo</span><span style="color: green">!</span></div>
</div></div>
</div></div>
</div></div>
<div>}</div>
</div><h2 id="badges">Badges</h2>
<p><a href="http://badge.fury.io/js/unexpected"><img src="https://badge.fury.io/js/unexpected.svg" alt="NPM version"></a>
<a href="https://travis-ci.org/unexpectedjs/unexpected"><img src="https://travis-ci.org/unexpectedjs/unexpected.svg?branch=master" alt="Build Status"></a>
<a href="https://coveralls.io/r/unexpectedjs/unexpected"><img src="https://coveralls.io/repos/unexpectedjs/unexpected/badge.svg" alt="Coverage Status"></a>
<a href="https://david-dm.org/unexpectedjs/unexpected"><img src="https://david-dm.org/unexpectedjs/unexpected.svg" alt="Dependency Status"></a>
<a href="https://gitter.im/unexpectedjs/unexpected?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge"><img src="https://badges.gitter.im/Join%20Chat.svg" alt="Gitter"></a></p>
<h2 id="features">Features</h2>
<ul>
<li>Extensible</li>
<li>Fast</li>
<li>Provides really nice error messages</li>
<li>Helps you if you misspells assertions</li>
<li>Compatible with all test frameworks.</li>
<li>Node.JS ready (<code>require('unexpected')</code>)</li>
<li>Supports <a href="/api/addAssertion/#asynchronous-assertions">asynchronous assertions using promises</a></li>
<li>Single global with no prototype extensions or shims.</li>
<li>Cross-browser: works on Chrome, Firefox, Safari, Opera, IE6+,
(IE6-IE8 with <a href="https://github.com/es-shims/es5-shim">es5-shim</a>)</li>
</ul>
<h3 id="node">Node</h3>
<p>Install it with NPM or add it to your <code>package.json</code>:</p>
<div class="code lang-">
<div>$ npm install unexpected</div>
</div><p>Then:</p>
<div class="code lang-javascript">
<div><span style="color: #FFAA27">var</span> expect <span style="color: #3CDAFF">=</span> require<span style="color: #999">(</span><span style="color: #3CDAFF">'unexpected'</span><span style="color: #999">);</span></div>
</div><h3 id="browser">Browser</h3>
<p>Include <code>unexpected.js</code>.</p>
<div class="code lang-html">
<div><span style="color: #999"><</span><span style="color: white">script </span><span style="color: #3CDAFF">src</span><span style="color: #999">="</span><span style="color: #FFAA27">unexpected.js</span><span style="color: #999">"></</span><span style="color: white">script</span><span style="color: #999">></span></div>
</div><p>this will expose the expect function under the following namespace:</p>
<div class="code lang-javascript">
<div><span style="color: #FFAA27">var</span> expect <span style="color: #3CDAFF">=</span> weknowhow<span style="color: #999">.</span>expect<span style="color: #999">;</span></div>
</div><h3 id="requirejs">RequireJS</h3>
<p>Include the library with RequireJS the following way:</p>
<div class="code lang-javascript">
<div>require<span style="color: #999">.</span>config<span style="color: #999">({</span></div>
<div> paths<span style="color: #999">:</span> <span style="color: #999">{</span></div>
<div> unexpected<span style="color: #999">:</span> <span style="color: #3CDAFF">'path/to/unexpected'</span></div>
<div> <span style="color: #999">}</span></div>
<div><span style="color: #999">});</span></div>
<div> </div>
<div>define<span style="color: #999">([</span><span style="color: #3CDAFF">'unexpected'</span><span style="color: #999">],</span> <span style="color: #FFAA27">function</span> <span style="color: #999">(</span>expect<span style="color: #999">)</span> <span style="color: #999">{</span></div>
<div> <span style="color: gray"> // Your code</span></div>
<div><span style="color: #999">});</span></div>
</div><h2 id="using-unexpected-with-a-test-framework">Using Unexpected with a test framework</h2>
<p>For example, if you create a test suite with
<a href="http://github.com/visionmedia/mocha">mocha</a>.</p>
<p>Let's say we wanted to test the following program:</p>
<p><strong>math.js</strong></p>
<div class="code lang-javascript">
<div><span style="color: #FFAA27">function</span> add <span style="color: #999">(</span>a<span style="color: #999">,</span> b<span style="color: #999">)</span> <span style="color: #999">{</span> <span style="color: #FFAA27">return</span> a <span style="color: #3CDAFF">+</span> b<span style="color: #999">;</span> <span style="color: #999">};</span></div>
</div><p>Our test file would look like this:</p>
<div class="code lang-javascript">
<div>describe<span style="color: #999">(</span><span style="color: #3CDAFF">'math.js'</span><span style="color: #999">,</span> <span style="color: #FFAA27">function</span> <span style="color: #999">()</span> <span style="color: #999">{</span></div>
<div> describe<span style="color: #999">(</span><span style="color: #3CDAFF">'add'</span><span style="color: #999">,</span> <span style="color: #FFAA27">function</span> <span style="color: #999">()</span> <span style="color: #999">{</span></div>
<div> it<span style="color: #999">(</span><span style="color: #3CDAFF">'is a function'</span><span style="color: #999">,</span> <span style="color: #FFAA27">function</span> <span style="color: #999">()</span> <span style="color: #999">{</span></div>
<div> expect<span style="color: #999">(</span>add<span style="color: #999">,</span> <span style="color: #3CDAFF">'to be a'</span><span style="color: #999">,</span> <span style="color: #3CDAFF">'function'</span><span style="color: #999">);</span></div>
<div> <span style="color: #999">});</span></div>
<div> </div>
<div> it<span style="color: #999">(</span><span style="color: #3CDAFF">'does addition on numbers'</span><span style="color: #999">,</span> <span style="color: #FFAA27">function</span> <span style="color: #999">()</span> <span style="color: #999">{</span></div>
<div> expect<span style="color: #999">(</span>add<span style="color: #999">(</span><span style="color: white">1</span><span style="color: #999">,</span> <span style="color: white">3</span><span style="color: #999">),</span> <span style="color: #3CDAFF">'to be'</span><span style="color: #999">,</span> <span style="color: white">4</span><span style="color: #999">);</span></div>
<div> <span style="color: #999">});</span></div>
<div> <span style="color: #999">});</span></div>
<div><span style="color: #999">});</span></div>
</div><p>If a certain expectation fails, an exception will be raised which gets captured
and shown/processed by the test runner.</p>
<h2 id="source">Source</h2>
<p>The source for Unexpected can be found on
<a href="https://github.com/unexpectedjs/unexpected">Github</a>.</p>
<h2 id="releases">Releases</h2>
<h3 id="9-0-0">9.0.0</h3>
<ul>
<li>Build all error messages lazily. This is an internal refactoring
that makes it possible to generate very different output in the
text, Ansi, and HTML modes using the magicpen
<a href="https://github.com/sunesimonsen/magicpen#raw">raw</a> feature. This
change mostly affects plugins, and we have updated all the official
plugins accordingly, so please upgrade those to the latest version
when you upgrade to Unexpected 9.</li>
<li>Made it possible to tweak the default error message when creating
assertions. See <a href="./api/addAssertion/">addAssertion</a> for more
information.</li>
<li>Expanded the <code>to have message</code> assertion defined for <code>Error</code>
instances to allow matching a serialization other than plain text:
<code>to have ansi message</code>, <code>to have html message</code>.</li>
<li>The <code>to contain</code> assertion defined for strings: When the assertion fails,
display a "diff" where partial matches are highlighted.</li>
</ul>
<h3 id="8-0-0">8.0.0</h3>
<ul>
<li>All errors originating from assertions are now instances of
<a href="/api/UnexpectedError/"><code>UnexpectedError</code></a>, which can be manipulated before being
serialized.</li>
<li>Error messages and diffs are now built lazily, improving
performance.</li>
<li>Unexpected now detects created promises that were never returned and
fails synchronously. This will uncover some extremely nasty bugs
where the test suite succeeds when it should actually fail. This
feature only works in <a href="http://mochajs.org/">Mocha</a> and <a href="http://jasmine.github.io/">Jasmine</a>.</li>
<li>Deprecated error.output, please use error.getErrorMessage() instead.</li>
<li>Deprecated error.label, please use error.getLabel() instead.</li>
<li><code>when decoded as</code>, <code>when called with</code>, <code>when passed as parameter
to</code>, <code>when passed as parameters to</code>: Require the 4th argument to be
a string specifying an assertion. Previously a function was also
allowed, which turned out to be error prone. This also affects all
plugins that use the internal function <code>Assertion.prototype.shift</code>
to delegate to other assertions.</li>
<li>Nested error mode: Don't repeat the subject when it takes up
multiple lines and is identical to the parent subject.</li>
<li>Added a new <code>bubbleThrough</code> error mode that will make the error
bubble all the way to the top, mainly useful internally.</li>
<li>Added <a href="/assertions/function/to-error/"><code>to error</code></a> assertion.</li>
<li>Minor bugfixes and output tweaks.</li>
</ul>
<h3 id="7-0-0">7.0.0</h3>
<ul>
<li>Support for
<a href="/api/addAssertion/#asynchronous-assertions">asynchronous assertions using promises</a>.
All built-in assertions that delegate to other assertions (such as <code>to satisfy</code>)
have been rewritten to support this. The change is fully backwards compatible.</li>
<li>Removed support for the <code>to be an array of</code> and
<code>to be an array of (strings|numbers|...)</code> assertions. There are better and
more flexible alternatives.</li>
<li>Renamed assertions so that the subject type isn't mentioned in the assertion name.
The old names are kept around as aliases for now. These assertions are affected:<ul>
<li><code>to be an array whose items satisfy</code> => <code>to have items satisfying</code></li>
<li><code>to be an (object|hash|map) whose keys satisfy</code> => <code>to have keys satisfying</code></li>
<li><code>to be an (object|hash|map) whose values satisfy</code> => <code>to have values satisfying</code>
Also, these 3 assertions no longer pass for empty collections.</li>
</ul>
</li>
<li>New <code>when passed as parameter to constructor</code> and <code>when passed as parameter to async</code> "adverbial" assertions.</li>
<li>New <code>when decoded as</code> "adverbial" assertion for <code>Buffer</code> instances.</li>
<li>New <code>to have message</code> assertion defined for <code>Error</code> instances.</li>
<li>A lot of output improvements and minor tweaks.</li>
</ul>
<h3 id="6-0-0">6.0.0</h3>
<ul>
<li>New documentation and <a href="https://unexpectedjs.github.io/">corresponding site</a>.</li>
<li>Use <code>Object.is</code>/the <a href="http://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevalue">SameValue algorithm</a> when checking equality of primitive values (the <code>to be</code> and <code>to equal</code> assertions).</li>
<li>Tweaked the output of numerous assertions.</li>
<li>Constrained <code>to be empty</code> and <code>to have length</code> to only work with strings and array-like objects.</li>
<li>Renamed the <code>arrayLike</code> type to <code>array-like</code>.</li>
<li>Changed style names and added theming support (mostly internal).</li>
<li>Removed grammatically incorrect assertions.</li>
</ul>
<h2 id="mit-license">MIT License</h2>
<p>Copyright (c) 2013 Sune Simonsen <a href="mailto:sune@we-knowhow.dk">sune@we-knowhow.dk</a></p>
<p>Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:</p>
<p>The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.</p>
<p>THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</p>
</div>
<a class="github-ribbon" href="https://github.com/unexpectedjs/unexpected"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/a6677b08c955af8400f44c6298f40e7d19cc5b2d/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f677261795f3664366436642e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png"></a>
</div>
<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>