UnexpectedOutputError

Create an Error with a formatted message that compares actual/expected values. This error is not thrown by spooning, it is provided as a convenience for test implementations. Using it is entirely optional.

Properties

  • actual any — 

    unchanged actual value passed to constructor

  • expected any — 

    unchanged expected value passed to constructor

new UnexpectedOutputError(actual, expected, prefix)

Creates a new UnexpectedOutputError object and sets the this.actual and this.expected properties.

  • actual any — 

    actual value

  • expected any — 

    expected value

  • prefix string — 

    added to the start of the returned message

    (default: 'Unexpected output\n')

See

  • acEx — 

    used by the constructor to generate the error message

Examples

Show Error Message

const {UnexpectedOutputError} = require('spooning');
const error = new UnexpectedOutputError('A', 'B');
console.log(error.message);

Output

Unexpected output
<<<<<<< actual
A
=======
B
>>>>>>> expected

Use in Unit Test

const {test, UnexpectedOutputError} = require('spooning');

function greet(name) {
  return `Hello, ${name}!`;
}

test('Should greet world', (callback) => {
  const error = new UnexpectedOutputError(greet('world'), 'Hello, world!');
  callback(error.actual === error.expected ? null : error);
});

Use with assert in Unit Test

const {equal} = require('assert');
const {testSync, UnexpectedOutputError} = require('spooning');

function greet(name) {
  return `Hello, ${name}!`;
}

testSync('Should greet world', () => {
  const error = new UnexpectedOutputError(greet('world'), 'Hello, world!');
  equal(error.actual, error.expected, error.message);
});