unexpected
Version:
Extensible BDD assertion toolkit
1,314 lines (1,196 loc) • 91.8 kB
JavaScript
/*global describe, it, beforeEach*/
// THIS FILE IS AUTOGENERATED! DO NOT CHANGE IT MANUALLY.
// THIS FILE IS AUTOGENERATED! DO NOT CHANGE IT MANUALLY.
// It is built based on the examples in the documentation folder
// when the documentation site gets build by running "make site-build".
var expect = require("../").clone();
expect.addAssertion("to have message", function (expect, subject, value) {
var message;
if (subject._isUnexpected) {
message = subject.output.toString();
} else if (subject && Object.prototype.toString.call(subject) === "[object Error]") {
message = subject.message;
} else {
message = String(subject);
}
this.errorMode = "bubble";
expect(message, "to equal", value);
});
describe("documentation tests", function () {
beforeEach(function () {
expect = expect.clone();
});
it("api/addAssertion.md contains correct examples", function () {
var errorMode = 'default'; // use to control the error mode later in the example
expect.addAssertion('array', '[not] to be (sorted|ordered)', function(expect, subject, cmp) {
this.errorMode = errorMode;
expect(subject, '[not] to equal', [].concat(subject).sort(cmp));
});
expect([1,2,3], 'to be sorted');
expect([1,2,3], 'to be ordered');
expect([2,1,3], 'not to be sorted');
expect([2,1,3], 'not to be ordered');
expect([3,2,1], 'to be sorted', function (x, y) { return y - x; });
try {
expect([ 1, 3, 2, 4 ], 'to be sorted');
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect([ 1, 3, 2, 4 ], 'to be sorted');").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected [ 1, 3, 2, 4 ] to be sorted\n" +
"\n" +
"[\n" +
" 1,\n" +
" 3, // should equal 2\n" +
" 2, // should equal 3\n" +
" 4\n" +
"]"
);
}
try {
errorMode = 'bubble';
expect([ 1, 3, 2, 4 ], 'to be sorted');
expect.fail(function (output) {
output.error("expected:").nl();
output.code("errorMode = 'bubble';").nl();
output.code("expect([ 1, 3, 2, 4 ], 'to be sorted');").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected [ 1, 3, 2, 4 ] to equal [ 1, 2, 3, 4 ]\n" +
"\n" +
"[\n" +
" 1,\n" +
" 3, // should equal 2\n" +
" 2, // should equal 3\n" +
" 4\n" +
"]"
);
}
try {
errorMode = 'nested';
expect([ 1, 3, 2, 4 ], 'to be sorted');
expect.fail(function (output) {
output.error("expected:").nl();
output.code("errorMode = 'nested';").nl();
output.code("expect([ 1, 3, 2, 4 ], 'to be sorted');").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected [ 1, 3, 2, 4 ] to be sorted\n" +
" expected [ 1, 3, 2, 4 ] to equal [ 1, 2, 3, 4 ]\n" +
"\n" +
" [\n" +
" 1,\n" +
" 3, // should equal 2\n" +
" 2, // should equal 3\n" +
" 4\n" +
" ]"
);
}
});
it("api/addType.md contains correct examples", function () {
function Person(name, age) {
this.name = name;
this.age = age;
}
expect.addType({
name: 'Person',
base: 'object',
identify: function (value) {
return value instanceof Person;
}
});
try {
expect(new Person('John Doe', 42), 'to equal', new Person('Jane Doe', 24));
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect(new Person('John Doe', 42), 'to equal', new Person('Jane Doe', 24));").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected Person({ name: 'John Doe', age: 42 }) to equal Person({ name: 'Jane Doe', age: 24 })\n" +
"\n" +
"Person({\n" +
" name: 'John Doe', // should equal 'Jane Doe'\n" +
" // -John Doe\n" +
" // +Jane Doe\n" +
" age: 42 // should equal 24\n" +
"})"
);
}
expect.addType({
name: 'Person',
base: 'object',
identify: function (value) {
return value instanceof Person;
},
inspect: function (person, depth, output, inspect) {
output.text('new Person(')
.append(inspect(person.name, depth))
.text(', ')
.append(inspect(person.age, depth))
.text(')');
}
});
try {
expect(new Person('John Doe', 42), 'to equal', new Person('Jane Doe', 24));
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect(new Person('John Doe', 42), 'to equal', new Person('Jane Doe', 24));").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected new Person('John Doe', 42) to equal new Person('Jane Doe', 24)\n" +
"\n" +
"Person({\n" +
" name: 'John Doe', // should equal 'Jane Doe'\n" +
" // -John Doe\n" +
" // +Jane Doe\n" +
" age: 42 // should equal 24\n" +
"})"
);
}
expect.addType({
name: 'Person',
base: 'object',
identify: function (value) {
return value instanceof Person;
},
inspect: function (person, depth, output, inspect) {
output.text('new Person(')
.append(inspect(person.name, depth))
.text(', ')
.append(inspect(person.age, depth))
.text(')');
},
equal: function (a, b, equal) {
return a === b || equal(a.name, b.name);
}
});
expect.addType({
name: 'Person',
base: 'object',
identify: function (value) {
return value instanceof Person;
},
inspect: function (person, depth, output, inspect) {
output.text('new Person(')
.append(inspect(person.name, depth))
.text(', ')
.append(inspect(person.age, depth))
.text(')');
},
equal: function (a, b, equal) {
return a === b || equal(a.name, b.name);
},
diff: function (actual, expected, output, diff, inspect) {
return this.baseType.diff({name: actual.name}, {name: expected.name});
}
});
try {
expect(new Person('John Doe', 42), 'to equal', new Person('Jane Doe', 24));
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect(new Person('John Doe', 42), 'to equal', new Person('Jane Doe', 24));").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected new Person('John Doe', 42) to equal new Person('Jane Doe', 24)\n" +
"\n" +
"{\n" +
" name: 'John Doe' // should equal 'Jane Doe'\n" +
" // -John Doe\n" +
" // +Jane Doe\n" +
"}"
);
}
var inlineDiff = true; // used to change inlining in a later example
expect.addType({
name: 'Person',
base: 'object',
identify: function (value) {
return value instanceof Person;
},
inspect: function (person, depth, output, inspect) {
output.text('new Person(')
.append(inspect(person.name, depth))
.text(', ')
.append(inspect(person.age, depth))
.text(')');
},
equal: function (a, b, equal) {
return a === b || equal(a.name, b.name);
},
diff: function (actual, expected, output, diff, inspect) {
var nameDiff = diff(actual.name, expected.name);
output.text('new Person(')
.nl()
.indentLines();
if (nameDiff && nameDiff.inline) {
output.append(nameDiff.diff);
} else {
output.i().append(inspect(actual.name)).text(',').sp()
.annotationBlock(function () {
this.error('should be ').append(inspect(expected.name));
if (nameDiff) {
this.nl().append(nameDiff.diff);
}
})
.nl();
}
output.i().append(inspect(actual.age))
.outdentLines()
.nl()
.text(')');
return {
inline: inlineDiff,
diff: output
};
}
});
try {
expect(new Person('John Doe', 42), 'to equal', new Person('Jane Doe', 24));
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect(new Person('John Doe', 42), 'to equal', new Person('Jane Doe', 24));").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected new Person('John Doe', 42) to equal new Person('Jane Doe', 24)\n" +
"\n" +
"new Person(\n" +
" 'John Doe', // should be 'Jane Doe'\n" +
" // -John Doe\n" +
" // +Jane Doe\n" +
" 42\n" +
")"
);
}
try {
inlineDiff = true;
expect(
{'John Doe': new Person('John Doe', 42), 'Jane Doe': new Person('Janie Doe', 24)},
'to equal',
{'John Doe': new Person('John Doe', 42), 'Jane Doe': new Person('Jane Doe', 24)}
);
expect.fail(function (output) {
output.error("expected:").nl();
output.code("inlineDiff = true;").nl();
output.code("expect(").nl();
output.code(" {'John Doe': new Person('John Doe', 42), 'Jane Doe': new Person('Janie Doe', 24)},").nl();
output.code(" 'to equal',").nl();
output.code(" {'John Doe': new Person('John Doe', 42), 'Jane Doe': new Person('Jane Doe', 24)}").nl();
output.code(");").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected\n" +
"{\n" +
" 'John Doe': new Person('John Doe', 42),\n" +
" 'Jane Doe': new Person('Janie Doe', 24)\n" +
"}\n" +
"to equal\n" +
"{\n" +
" 'John Doe': new Person('John Doe', 42),\n" +
" 'Jane Doe': new Person('Jane Doe', 24)\n" +
"}\n" +
"\n" +
"{\n" +
" 'John Doe': new Person('John Doe', 42),\n" +
" 'Jane Doe': new Person(\n" +
" 'Janie Doe', // should be 'Jane Doe'\n" +
" // -Janie Doe\n" +
" // +Jane Doe\n" +
" 24\n" +
" )\n" +
"}"
);
}
try {
inlineDiff = false;
expect(
{'John Doe': new Person('John Doe', 42), 'Jane Doe': new Person('Janie Doe', 24)},
'to equal',
{'John Doe': new Person('John Doe', 42), 'Jane Doe': new Person('Jane Doe', 24)}
);
expect.fail(function (output) {
output.error("expected:").nl();
output.code("inlineDiff = false;").nl();
output.code("expect(").nl();
output.code(" {'John Doe': new Person('John Doe', 42), 'Jane Doe': new Person('Janie Doe', 24)},").nl();
output.code(" 'to equal',").nl();
output.code(" {'John Doe': new Person('John Doe', 42), 'Jane Doe': new Person('Jane Doe', 24)}").nl();
output.code(");").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected\n" +
"{\n" +
" 'John Doe': new Person('John Doe', 42),\n" +
" 'Jane Doe': new Person('Janie Doe', 24)\n" +
"}\n" +
"to equal\n" +
"{\n" +
" 'John Doe': new Person('John Doe', 42),\n" +
" 'Jane Doe': new Person('Jane Doe', 24)\n" +
"}\n" +
"\n" +
"{\n" +
" 'John Doe': new Person('John Doe', 42),\n" +
" 'Jane Doe': new Person('Janie Doe', 24) // should equal new Person('Jane Doe', 24)\n" +
" // new Person(\n" +
" // 'Janie Doe', // should be 'Jane Doe'\n" +
" // // -Janie Doe\n" +
" // // +Jane Doe\n" +
" // 24\n" +
" // )\n" +
"}"
);
}
expect.addAssertion('Person', 'to be above legal age', function (expect, subject) {
expect(subject.age, 'to be greater than or equal to', 18);
});
expect(new Person('Jane Doe', 24), 'to be above legal age');
expect(new Person('Jane Doe', 24), 'to have keys', 'name', 'age');
expect(new Person('Jane Doe', 24), 'to satisfy', {
name: expect.it('to be a string').and('not to be empty'),
age: expect.it('to be a number').and('not to be negative')
});
});
it("api/clone.md contains correct examples", function () {
var originalExpect = expect;
expect = expect.clone().addAssertion('to be an integer', function (expect, subject) {
expect(subject, 'to be a number');
expect(Math.round(subject), 'to be', subject);
});
expect(42, 'to be an integer');
try {
originalExpect(42, 'to be an integer');
expect.fail(function (output) {
output.error("expected:").nl();
output.code("originalExpect(42, 'to be an integer');").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"Unknown assertion \"to be an integer\", did you mean: \"to be a number\""
);
}
});
it("api/fail.md contains correct examples", function () {
try {
expect.fail()
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect.fail()").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"Explicit failure"
);
}
try {
expect.fail('Custom failure message')
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect.fail('Custom failure message')").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"Custom failure message"
);
}
try {
expect.fail('{0} was expected to be {1}', 0, 'zero');
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect.fail('{0} was expected to be {1}', 0, 'zero');").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"0 was expected to be 'zero'"
);
}
try {
var error = new Error('throw me');
expect.fail(new Error(error));
expect.fail(function (output) {
output.error("expected:").nl();
output.code("var error = new Error('throw me');").nl();
output.code("expect.fail(new Error(error));").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"Error: throw me"
);
}
try {
expect.fail(function (output) {
'You have been a very bad boy!'.split(/ /).forEach(function (word, index) {
if (index > 0) { output.sp(); }
var style = index % 2 === 0 ? 'jsPrimitive' : 'jsString';
output[style](word);
});
});
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect.fail(function (output) {").nl();
output.code(" 'You have been a very bad boy!'.split(/ /).forEach(function (word, index) {").nl();
output.code(" if (index > 0) { output.sp(); }").nl();
output.code(" var style = index % 2 === 0 ? 'jsPrimitive' : 'jsString';").nl();
output.code(" output[style](word);").nl();
output.code(" });").nl();
output.code("});").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"You have been a very bad boy!"
);
}
});
it("api/installPlugin.md contains correct examples", function () {
function IntegerInterval(from, to) {
this.from = from;
this.to = to;
}
expect.installPlugin({
name: 'unexpected-integer-intervals',
installInto: function (expect) {
expect.addType({
name: 'IntegerInterval',
base: 'object',
identify: function (value) {
return value && value instanceof IntegerInterval;
},
inspect: function (value, depth, output) {
output.text('[').jsNumber(value.from).text(',').jsNumber(value.to).text(']');
}
});
expect.addAssertion('[not] to contain', function (expect, subject, value) {
expect(value, '[not] to be within', subject.from, subject.to);
});
}
})
expect(new IntegerInterval(7, 13), 'to contain', 9);
try {
expect(new IntegerInterval(7, 13), 'to contain', 27);
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect(new IntegerInterval(7, 13), 'to contain', 27);").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected [7,13] to contain 27"
);
}
});
it("assertions/any/to-be-a.md contains correct examples", function () {
expect(true, 'to be a', 'boolean');
expect(5, 'to be a', 'number');
expect('abc', 'to be a', 'string');
expect(expect, 'to be a', 'function');
expect({foo: 123}, 'to be an', 'object');
expect([123], 'to be an', 'array');
expect(/regex/, 'to be a', 'regexp');
expect(/regex/, 'to be a', 'regex');
expect(/regex/, 'to be a', 'regular expression');
expect(new Error(), 'to be an', 'Error');
expect(expect, 'to be a', 'function');
expect(expect, 'to be an', 'object');
expect(true, 'to be a boolean');
expect(5, 'to be a number');
expect('abc', 'to be a string');
expect(expect, 'to be a function');
expect({foo: 123}, 'to be an object');
expect([123], 'to be an array');
expect(/regex/, 'to be a regexp');
expect(/regex/, 'to be a regex');
expect(/regex/, 'to be a regular expression');
function Person(name) {
this.name = name;
}
expect(new Person('John Doe'), 'to be a', Person);
expect(new Person('John Doe'), 'to be an', Object);
try {
expect({ 0: 'foo', 1: 'bar', 2: 'baz' }, 'to be an array');
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect({ 0: 'foo', 1: 'bar', 2: 'baz' }, 'to be an array');").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected { 0: 'foo', 1: 'bar', 2: 'baz' } to be an array"
);
}
expect(true, 'not to be an object');
expect('5', 'not to be a', 'number');
expect('abc', 'not to be an', Object);
try {
expect(function () { return 'wat'; }, 'not to be an', Object);
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect(function () { return 'wat'; }, 'not to be an', Object);").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected function () { return 'wat'; } not to be an Object"
);
}
});
it("assertions/any/to-be-defined.md contains correct examples", function () {
expect('Hello world!', 'to be defined');
expect({ foo: { bar: 'baz' } }, 'to be defined');
try {
expect(undefined, 'to be defined');
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect(undefined, 'to be defined');").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected undefined to be defined"
);
}
});
it("assertions/any/to-be-falsy.md contains correct examples", function () {
expect(0, 'to be falsy');
expect(false, 'to be falsy');
expect('', 'to be falsy');
expect(undefined, 'to be falsy');
expect(null, 'to be falsy');
try {
expect({}, 'to be falsy');
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect({}, 'to be falsy');").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected {} to be falsy"
);
}
expect(1, 'not to be falsy');
expect(true, 'not to be falsy');
expect({}, 'not to be falsy');
expect('foo', 'not to be falsy');
expect(/foo/, 'not to be falsy');
try {
expect('', 'not to be falsy');
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect('', 'not to be falsy');").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected '' not to be falsy"
);
}
});
it("assertions/any/to-be-null.md contains correct examples", function () {
expect(null, 'to be null');
try {
expect({ foo: { bar: 'baz' } }, 'to be null');
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect({ foo: { bar: 'baz' } }, 'to be null');").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected { foo: { bar: 'baz' } } to be null"
);
}
expect({ foo: { bar: 'baz' } }, 'not to be null');
expect('Hello world!', 'not to be null');
try {
expect(null, 'not to be null');
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect(null, 'not to be null');").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected null not to be null"
);
}
});
it("assertions/any/to-be-ok.md contains correct examples", function () {
expect(1, 'to be ok');
expect(true, 'to be ok');
expect({}, 'to be ok');
expect('foo', 'to be ok');
expect(/foo/, 'to be ok');
try {
expect('', 'to be ok');
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect('', 'to be ok');").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected '' to be ok"
);
}
expect(0, 'not to be ok');
expect(false, 'not to be ok');
expect('', 'not to be ok');
expect(undefined, 'not to be ok');
expect(null, 'not to be ok');
try {
expect({}, 'not to be ok');
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect({}, 'not to be ok');").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected {} not to be ok"
);
}
});
it("assertions/any/to-be-truthy.md contains correct examples", function () {
expect(1, 'to be truthy');
expect(true, 'to be truthy');
expect({}, 'to be truthy');
expect('foo', 'to be truthy');
expect(/foo/, 'to be truthy');
try {
expect('', 'to be truthy');
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect('', 'to be truthy');").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected '' to be truthy"
);
}
expect(0, 'not to be truthy');
expect(false, 'not to be truthy');
expect('', 'not to be truthy');
expect(undefined, 'not to be truthy');
expect(null, 'not to be truthy');
try {
expect({}, 'not to be truthy');
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect({}, 'not to be truthy');").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected {} not to be truthy"
);
}
});
it("assertions/any/to-be-undefined.md contains correct examples", function () {
expect(undefined, 'to be undefined');
try {
expect('Hello world', 'to be undefined');
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect('Hello world', 'to be undefined');").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected 'Hello world' to be undefined"
);
}
expect('Hello world!', 'not to be undefined');
expect({ foo: { bar: 'baz' } }, 'not to be undefined');
try {
expect(undefined, 'not to be undefined');
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect(undefined, 'not to be undefined');").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected undefined not to be undefined"
);
}
});
it("assertions/any/to-be.md contains correct examples", function () {
var obj = {};
expect(obj, 'to be', obj);
expect(1, 'to be', 1);
expect(null, 'to be', null);
expect(undefined, 'to be', obj.foo);
expect(true, 'to be', !false);
expect(NaN, 'to be', NaN);
expect(-0, 'not to be', 0);
try {
expect('1', 'to be', 1);
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect('1', 'to be', 1);").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected '1' to be 1"
);
}
expect({}, 'not to be', {});
expect(1, 'not to be', true);
expect('1', 'not to be', 1);
expect(null, 'not to be', undefined);
expect(0, 'not to be', 'null');
expect(undefined, 'not to be', 'null');
expect(false, 'not to be', 'true');
expect(true, 'not to be', 'false');
try {
expect(1, 'not to be', 1);
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect(1, 'not to be', 1);").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected 1 not to be 1"
);
}
});
it("assertions/any/to-equal.md contains correct examples", function () {
expect({ a: 'b' }, 'to equal', { a: 'b' });
var now = new Date();
expect(now, 'to equal', now);
expect(now, 'to equal', new Date(now.getTime()));
expect({ now: now }, 'to equal', { now: now });
try {
expect({ text: 'foo!' }, 'to equal', { text: 'f00!' });
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect({ text: 'foo!' }, 'to equal', { text: 'f00!' });").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected { text: 'foo!' } to equal { text: 'f00!' }\n" +
"\n" +
"{\n" +
" text: 'foo!' // should equal 'f00!'\n" +
" // -foo!\n" +
" // +f00!\n" +
"}"
);
}
try {
expect({ one: 1, two: 2, four: 4, five: 5 }, 'to equal', { one: 1, two: 2, three: 3, four: 4 });
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect({ one: 1, two: 2, four: 4, five: 5 }, 'to equal', { one: 1, two: 2, three: 3, four: 4 });").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected { one: 1, two: 2, four: 4, five: 5 } to equal { one: 1, two: 2, three: 3, four: 4 }\n" +
"\n" +
"{\n" +
" one: 1,\n" +
" two: 2,\n" +
" four: 4,\n" +
" five: 5, // should be removed\n" +
" three: undefined // should equal 3\n" +
"}"
);
}
try {
expect([ 0, 1, 2, 4, 5], 'to equal', [ 1, 2, 3, 4]);
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect([ 0, 1, 2, 4, 5], 'to equal', [ 1, 2, 3, 4]);").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected [ 0, 1, 2, 4, 5 ] to equal [ 1, 2, 3, 4 ]\n" +
"\n" +
"[\n" +
" 0, // should be removed\n" +
" 1,\n" +
" 2,\n" +
" // missing 3\n" +
" 4,\n" +
" 5 // should be removed\n" +
"]"
);
}
try {
expect(
new Buffer('\x00\x01\x02Here is the thing I was talking about', 'utf-8'),
'to equal',
new Buffer('\x00\x01\x02Here is the thing I was quuxing about', 'utf-8')
);
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect(").nl();
output.code(" new Buffer('\\x00\\x01\\x02Here is the thing I was talking about', 'utf-8'),").nl();
output.code(" 'to equal',").nl();
output.code(" new Buffer('\\x00\\x01\\x02Here is the thing I was quuxing about', 'utf-8')").nl();
output.code(");").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected Buffer([0x00, 0x01, 0x02, 0x48, 0x65, 0x72, 0x65, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74 /* 24 more */ ])\n" +
"to equal Buffer([0x00, 0x01, 0x02, 0x48, 0x65, 0x72, 0x65, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74 /* 24 more */ ])\n" +
"\n" +
" 00 01 02 48 65 72 65 20 69 73 20 74 68 65 20 74 │...Here is the t│\n" +
"-68 69 6E 67 20 49 20 77 61 73 20 74 61 6C 6B 69 │hing I was talki│\n" +
"+68 69 6E 67 20 49 20 77 61 73 20 71 75 75 78 69 │hing I was quuxi│\n" +
" 6E 67 20 61 62 6F 75 74 │ng about│"
);
}
expect(1, 'not to equal', '1');
expect({ one: 1 }, 'not to equal', { one: '1' });
expect(null, 'not to equal', '1');
var now = new Date();
var later = new Date(now.getTime() + 42);
expect(now, 'not to equal', later);
expect({ time: now }, 'not to equal', { time: later });
try {
expect({ a: { b: 'd'} }, 'not to equal', { a: { b: 'd'} });
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect({ a: { b: 'd'} }, 'not to equal', { a: { b: 'd'} });").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected { a: { b: 'd' } } not to equal { a: { b: 'd' } }"
);
}
});
it("assertions/any/to-satisfy.md contains correct examples", function () {
expect({ hey: { there: true } }, 'to satisfy', { hey: {} });
expect({ hey: { there: true } }, 'to exhaustively satisfy', { hey: { there: true } });
expect({ bar: 'quux', baz: true }, 'to satisfy', { bar: /QU*X/i });
expect({foo: 123, bar: 'bar', baz: 'bogus', qux: 42}, 'to satisfy', {
foo: expect.it('to be a number').and('to be greater than', 10),
baz: expect.it('not to match', /^boh/),
qux: expect.it('to be a string')
.and('not to be empty')
.or('to be a number')
.and('to be positive')
});
try {
expect({foo: 9, bar: 'bar', baz: 'bogus', qux: 42}, 'to satisfy', {
foo: expect.it('to be a number').and('to be greater than', 10),
baz: expect.it('not to match', /^bog/),
qux: expect.it('to be a string')
.and('not to be empty')
.or('to be a number')
.and('to be positive')
});
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect({foo: 9, bar: 'bar', baz: 'bogus', qux: 42}, 'to satisfy', {").nl();
output.code(" foo: expect.it('to be a number').and('to be greater than', 10),").nl();
output.code(" baz: expect.it('not to match', /^bog/),").nl();
output.code(" qux: expect.it('to be a string')").nl();
output.code(" .and('not to be empty')").nl();
output.code(" .or('to be a number')").nl();
output.code(" .and('to be positive')").nl();
output.code("});").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected { foo: 9, bar: 'bar', baz: 'bogus', qux: 42 } to satisfy\n" +
"{\n" +
" foo: expect.it('to be a number')\n" +
" .and('to be greater than', 10),\n" +
" baz: expect.it('not to match', /^bog/),\n" +
" qux: expect.it('to be a string')\n" +
" .and('not to be empty')\n" +
" .or('to be a number')\n" +
" .and('to be positive')\n" +
"}\n" +
"\n" +
"{\n" +
" foo: 9, // ✓ expected 9 to be a number and\n" +
" // ⨯ expected 9 to be greater than 10\n" +
" bar: 'bar',\n" +
" baz: 'bogus', // expected 'bogus' not to match /^bog/\n" +
" //\n" +
" // bogus\n" +
" qux: 42\n" +
"}"
);
}
});
it("assertions/array/to-be-an-array-whose-items-satisfy.md contains correct examples", function () {
expect([0, 1, 2, 3, 4], 'to be an array whose items satisfy', function (item, index) {
expect(item, 'to be a number');
});
expect([0, 1, 2, 3, 4], 'to be an array whose items satisfy', 'to be a number');
expect([[1], [2]], 'to be an array whose items satisfy',
'to be an array whose items satisfy', 'to be a number');
expect([[], []], 'to be a non-empty array whose items satisfy', function (item) {
expect(item, 'to be an empty array');
});
expect([1, 2, 3, 4], 'to be an array whose items satisfy',
expect.it('to be a number').and('to be positive'));
try {
expect([ [0, 1, 2], [4, '5', '6'], [7, '8', 9] ],
'to be an array whose items satisfy',
'to be an array whose items satisfy',
'to be a number');
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect([ [0, 1, 2], [4, '5', '6'], [7, '8', 9] ],").nl();
output.code(" 'to be an array whose items satisfy',").nl();
output.code(" 'to be an array whose items satisfy',").nl();
output.code(" 'to be a number');").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"failed expectation in [ [ 0, 1, 2 ], [ 4, '5', '6' ], [ 7, '8', 9 ] ]:\n" +
" 1: failed expectation in [ 4, '5', '6' ]:\n" +
" 1: expected '5' to be a number\n" +
" 2: expected '6' to be a number\n" +
" 2: failed expectation in [ 7, '8', 9 ]:\n" +
" 1: expected '8' to be a number"
);
}
try {
expect([0, 1, 2, 3, 4], 'to be an array whose items satisfy',
expect.it('to be a number').and('to be positive'));
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect([0, 1, 2, 3, 4], 'to be an array whose items satisfy',").nl();
output.code(" expect.it('to be a number').and('to be positive'));").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"failed expectation in [ 0, 1, 2, 3, 4 ]:\n" +
" 0: ✓ expected 0 to be a number and\n" +
" ⨯ expected 0 to be positive"
);
}
});
it("assertions/array/to-be-empty.md contains correct examples", function () {
expect([], 'to be empty');
try {
expect([1,2,3], 'to be empty');
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect([1,2,3], 'to be empty');").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected [ 1, 2, 3 ] to be empty"
);
}
expect([1,2,3], 'not to be empty');
try {
expect([], 'not to be empty');
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect([], 'not to be empty');").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected [] not to be empty"
);
}
});
it("assertions/array/to-be-non-empty.md contains correct examples", function () {
expect([1, 2, 3], 'to be non-empty');
try {
expect([], 'to be non-empty');
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect([], 'to be non-empty');").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected [] to be non-empty"
);
}
});
it("assertions/array/to-contain.md contains correct examples", function () {
expect([0, 1, 2], 'to contain', 1);
expect([ { name: 'John Doe' }, { name: 'Jane Doe' } ], 'to contain', { name: 'Jane Doe' });
expect([0, 1, 2], 'to contain', 0, 2);
try {
expect([ { name: 'John Doe' }, { name: 'Jane Doe' } ], 'to contain', { name: 'Jonnie Doe' });
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect([ { name: 'John Doe' }, { name: 'Jane Doe' } ], 'to contain', { name: 'Jonnie Doe' });").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected [ { name: 'John Doe' }, { name: 'Jane Doe' } ] to contain { name: 'Jonnie Doe' }"
);
}
expect([ { name: 'John Doe' }, { name: 'Jane Doe' } ], 'not to contain', { name: 'Jonnie Doe' });
try {
expect([ { name: 'John Doe' }, { name: 'Jane Doe' } ], 'not to contain', { name: 'Jane Doe' });
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect([ { name: 'John Doe' }, { name: 'Jane Doe' } ], 'not to contain', { name: 'Jane Doe' });").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected [ { name: 'John Doe' }, { name: 'Jane Doe' } ] not to contain { name: 'Jane Doe' }\n" +
"\n" +
"[\n" +
" { name: 'John Doe' },\n" +
" { name: 'Jane Doe' } // should be removed\n" +
"]"
);
}
});
it("assertions/array/to-have-length.md contains correct examples", function () {
expect([1,2,3], 'to have length', 3);
try {
expect([1,2,3], 'to have length', 4);
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect([1,2,3], 'to have length', 4);").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected [ 1, 2, 3 ] to have length 4\n" +
" expected 3 to be 4"
);
}
expect([1,2,3], 'not to have length', 4);
try {
expect([1,2,3], 'not to have length', 3);
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect([1,2,3], 'not to have length', 3);").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected [ 1, 2, 3 ] not to have length 3"
);
}
});
it("assertions/boolean/to-be-false.md contains correct examples", function () {
expect(false, 'to be false');
try {
expect(true, 'to be false');
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect(true, 'to be false');").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected true to be false"
);
}
});
it("assertions/boolean/to-be-true.md contains correct examples", function () {
expect(true, 'to be true');
try {
expect(false, 'to be true');
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect(false, 'to be true');").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected false to be true"
);
}
});
it("assertions/function/to-have-arity.md contains correct examples", function () {
expect(Math.max, 'to have arity', 2);
expect('wat'.substring, 'to have arity', 2);
try {
expect(function wat(foo, bar) {
return foo + bar;
}, 'to have arity', 3);
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect(function wat(foo, bar) {").nl();
output.code(" return foo + bar;").nl();
output.code("}, 'to have arity', 3);").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected\n" +
"function wat(foo, bar) {\n" +
" return foo + bar;\n" +
"}\n" +
"to have arity 3"
);
}
});
it("assertions/function/to-throw.md contains correct examples", function () {
function willThrow() {
throw new Error('The error message');
}
expect(willThrow, 'to throw');
expect(willThrow, 'to throw error');
expect(willThrow, 'to throw exception');
try {
expect(function willNotThrow() {}, 'to throw');
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect(function willNotThrow() {}, 'to throw');").nl();
output.error("to throw");
});
} catch (e) {
expect(e, "to have message",
"expected function willNotThrow() {} to throw"
);
}
expect(function () {
throw new Error('The error message');
}, 'to throw', 'The error message');
try {
expect(function () {
throw new Error('The error message!');
}, 'to throw', 'The error message');
expect.fail(function (output) {
output.error("expected:").nl();
output.code("expect(function () {").nl();
output.code(" throw new Error('The error message!');").nl();
output.code("}, 'to throw', 'The