Welcome to Unexpected

The extensible BDD assertion toolkit

const library = {
  name: 'un3xp3c73d',
  created: 2013,
  site: 'https://unexpected.js.org',
};
 
expect(library, 'to satisfy', {
  name: 'Unexpected',
  created: 2013,
});
expected { name'un3xp3c73d'created2013site'https://unexpected.js.org' }
to satisfy { name'Unexpected'created2013 }
 
{
  
name
'un3xp3c73d'
//
//
//
//
 
should equal 
'Unexpected'
 
un3xp3c73d
Unexpected
  
created2013,
  
site'https://unexpected.js.org'
}

Badges

NPM version Build Status Coverage Status Gitter Github BrowserStack Status

Features

Node

Install it with NPM or add it to your package.json:

$ npm install --save-dev unexpected

Then:

const expect = require('unexpected');

Browser

Include unexpected.js.

<script src="unexpected.js"></script>

This will expose the expect function under the following namespace:

const expect = weknowhow.expect;

RequireJS

Include the library with RequireJS the following way:

require.config({
  paths: {
    unexpected: 'path/to/unexpected',
  },
});
 
define(['unexpected'], function (expect) {
  // Your code
});

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

function add(a, b) {
  return a + b;
}

Our test file would look like this:

describe('math.js', function () {
  describe('add', function () {
    it('is a function', function () {
      expect(add, 'to be a', 'function');
    });
 
    it('adds numbers', function () {
      expect(add(1, 3), 'to be', 4);
    });
  });
});

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

See the releases page.

Configure the error output

Disable stack trace trimming

You can disable stack trace trimming the following way:

UNEXPECTED_FULL_TRACE=true mocha

You can achieve the same in the browser by setting the query parameter full-trace to true.

Controlling the inspection depth

To change the level subtrees gets dotted out, you can set the inspection depth the following way:

UNEXPECTED_DEPTH=9 mocha

You can achieve the same in the browser by setting the query parameter depth to the inspection depth you want.

MIT License

Copyright (c) 2013 Sune Simonsen mailto: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.

Fork me on GitHub