Welcome to Unexpected
The extensible BDD assertion toolkit
Badges
Features
- Extensible
- Fast
- Provides really nice error messages
- Helps you if you misspells assertions
- Compatible with all test frameworks.
- Node.JS ready (
require('unexpected')
) - Supports asynchronous assertions using promises
- Single global with no prototype extensions or shims.
- Cross-browser: works on Chrome, Firefox, Safari, Opera, IE6+, (IE6-IE8 with es5-shim)
Node
Install it with NPM or add it to your package.json
:
Then:
Browser
Include unexpected.js
.
this will expose the expect function under the following namespace:
RequireJS
Include the library with RequireJS the following way:
Using Unexpected with a test framework
For example, if you create a test suite with mocha.
Let's say we wanted to test the following program:
math.js
Our test file would look like this:
If a certain expectation fails, an exception will be raised which gets captured and shown/processed by the test runner.
Source
The source for Unexpected can be found on Github.
Releases
8.0.0
- All errors originating from assertions are now instances of
UnexpectedError
, which can be manipulated before being serialized. - Error messages and diffs are now built lazily, improving performance.
- 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 Mocha and Jasmine.
- Deprecated error.output, please use error.getErrorMessage() instead.
- Deprecated error.label, please use error.getLabel() instead.
when decoded as
,when called with
,when passed as parameter to
,when passed as parameters to
: 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 functionAssertion.prototype.shift
to delegate to other assertions.- Nested error mode: Don't repeat the subject when it takes up multiple lines and is identical to the parent subject.
- Added a new
bubbleThrough
error mode that will make the error bubble all the way to the top, mainly useful internally. - Added
to error
assertion. - Minor bugfixes and output tweaks.
7.0.0
- Support for
asynchronous assertions using promises.
All built-in assertions that delegate to other assertions (such as
to satisfy
) have been rewritten to support this. The change is fully backwards compatible. - Removed support for the
to be an array of
andto be an array of (strings|numbers|...)
assertions. There are better and more flexible alternatives. - 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:
to be an array whose items satisfy
=>to have items satisfying
to be an (object|hash|map) whose keys satisfy
=>to have keys satisfying
to be an (object|hash|map) whose values satisfy
=>to have values satisfying
Also, these 3 assertions no longer pass for empty collections.
- New
when passed as parameter to constructor
andwhen passed as parameter to async
"adverbial" assertions. - New
when decoded as
"adverbial" assertion forBuffer
instances. - New
to have message
assertion defined forError
instances. - A lot of output improvements and minor tweaks.
6.0.0
- New documentation and corresponding site.
- Use
Object.is
/the SameValue algorithm when checking equality of primitive values (theto be
andto equal
assertions). - Tweaked the output of numerous assertions.
- Constrained
to be empty
andto have length
to only work with strings and array-like objects. - Renamed the
arrayLike
type toarray-like
. - Changed style names and added theming support (mostly internal).
- Removed grammatically incorrect assertions.
MIT License
Copyright (c) 2013 Sune Simonsen sune@we-knowhow.dk
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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.