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
-
actualany —unchanged
actualvalue passed to constructor -
expectedany —unchanged
expectedvalue passed to constructor
new UnexpectedOutputError(actual, expected, prefix)
Creates a new UnexpectedOutputError object and sets the this.actual and this.expected properties.
-
actualany —actual value
-
expectedany —expected value
-
prefixstring —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);
});