Plugins

Unexpected is designed to be highly extensible with new data types, assertions, output styles and themes.

Here is a list of plugins that is maintained by the unexpected team.

Community plugins

Installation

The recommended way to get plugins is installing them via npm:

$ npm install unexpected-dom

And then in your test suite:

const expect = require('unexpected').clone().use(require('unexpected-dom'));

For plugins that work in the browser, you'll either need to add an extra <script>, or use browserify or a script loader instead of the Common.js require in the above example. Please consult the documentation for each individual plugin.

Mixing plugins

All of these plugins should be able coexist in the same Unexpected instance and compose well together. For instance, you can grab a few and assert that an express app serves an HTML response body that contains a yellow <div>:

const expect = require('unexpected')
  .clone()
  .use(require('unexpected-express'))
  .use(require('unexpected-dom'))
  .use(require('unexpected-color'));
 
const app = require('express')().get('/myPage', function (req, res, next) {
  res.send('<html><body><div style="color: #ff0">Hey!</div></body></html>');
});
 
it('should deliver something pretty', function () {
  return expect(app, 'to yield exchange', {
    request: 'GET /myPage',
    response: {
      headers: { 'Content-Type': 'text/html; charset=utf-8' },
      body: expect.it(
        'when parsed as HTML',
        'queried for first',
        'div',
        'to satisfy',
        {
          attributes: {
            style: {
              color: expect.it('to be colored', 'yellow'),
            },
          },
        }
      ),
    },
  });
});

Or you could assert that a node.js readable stream outputs an image that's at most 10% different from a reference image:

const expect = require('unexpected')
  .clone()
  .use(require('unexpected-stream'))
  .use(require('unexpected-image'))
  .use(require('unexpected-resemble'));
 
it('should spew out the expected image', function () {
  const myStream = require('fs').createReadStream('foo.png');
 
  return expect(
    myStream,
    'to yield output satisfying',
    expect
      .it('to resemble', 'bar.png', {
        mismatchPercentage: expect.it('to be less than', 10),
      })
      .and('to have metadata satisfying', {
        format: 'PNG',
      })
  );
});
Fork me on GitHub