UNPKG

unexpected

Version:
1,228 lines (1,132 loc) 121 kB
/*global unexpected*/ // 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". it.skipIf = function (condition) { (condition ? it.skip : it).apply(it, Array.prototype.slice.call(arguments, 1)); }; describe("documentation tests", function () { var isBrowser = typeof weknowhow !== 'undefined'; var isPhantom = typeof mochaPhantomJS !== 'undefined'; var expect; beforeEach(function () { expect = unexpected.clone(); expect.output.preferredWidth = 80; }); it("api/addAssertion.md contains correct examples", function () { var testPromises = []; 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" + " ]" ); } function Timelock(value, delay) { this.value = value; this.delay = delay; } Timelock.prototype.getValue = function (cb) { var that = this; setTimeout(function () { cb(that.value); }, this.delay); }; expect.addType({ name: 'Timelock', identify: function (value) { return value && value instanceof Timelock; }, inspect: function (value, depth, output) { output.jsFunctionName('Timelock'); } }); expect.addAssertion('Timelock', 'to satisfy', function (expect, subject, spec) { return expect.promise(function (run) { subject.getValue(run(function (value) { return expect(value, 'to satisfy', spec); })); }); }); testPromises.push(expect.promise(function () { return expect(new Timelock('Hello world!', 5), 'to satisfy', expect.it('not to match', /!/)); }).then(function () { return expect.promise(function () { expect.fail(function (output) { output.error("expected:").nl(); output.code("return expect(new Timelock('Hello world!', 5), 'to satisfy', expect.it('not to match', /!/));").nl(); output.error("to throw"); }); }); }).caught(function (e) { expect(e, "to have message", "expected Timelock to satisfy expect.it('not to match', /!/)\n" + "\n" + "expected 'Hello world!' not to match /!/\n" + "\n" + "Hello world!" ); })); return expect.promise.all(testPromises); }); it("api/addType.md contains correct examples", function () { var testPromises = []; 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') }); return expect.promise.all(testPromises); }); it("api/clone.md contains correct examples", function () { var testPromises = []; 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\"" ); } return expect.promise.all(testPromises); }); it("api/fail.md contains correct examples", function () { var testPromises = []; 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!" ); } try { expect.fail({ message: 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); }); }, diff: function (output, diff, inspect, equal) { return diff('You have been a very bad boy!', 'You have been a very mad boy!') } }); expect.fail(function (output) { output.error("expected:").nl(); output.code("expect.fail({").nl(); output.code(" message: 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.code(" diff: function (output, diff, inspect, equal) {").nl(); output.code(" return diff('You have been a very bad boy!', 'You have been a very mad boy!')").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!\n" + "\n" + "-You have been a very bad boy!\n" + "+You have been a very mad boy!" ); } return expect.promise.all(testPromises); }); it("api/installPlugin.md contains correct examples", function () { var testPromises = []; 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" ); } return expect.promise.all(testPromises); }); it("api/promise-all.md contains correct examples", function () { var testPromises = []; expect.addAssertion('to be a number after a short delay', function (expect, subject) { return expect.promise(function (run) { setTimeout(run(function () { expect(subject, 'to be a number'); }), 1); }); }); testPromises.push(expect.promise(function () { return expect.promise.all({ foo: [ expect(42, 'to be a number after a short delay') ], bar: expect([0, 1, 2], 'to have items satisfying', expect.it('to be a number after a short delay')), baz: expect({ a: 1, b: 2 }, 'to have values satisfying', 'to be a number after a short delay') }); })); testPromises.push(expect.promise(function () { return expect.promise.all({ foo: [ expect(42, 'to be a number after a short delay') ], bar: expect([0, 1, 2], 'to have items satisfying', expect.it('to be a number after a short delay')), baz: expect({ a: '0', b: 1 }, 'to have values satisfying', 'to be a number after a short delay') }); }).then(function () { return expect.promise(function () { expect.fail(function (output) { output.error("expected:").nl(); output.code("return expect.promise.all({").nl(); output.code(" foo: [").nl(); output.code(" expect(42, 'to be a number after a short delay')").nl(); output.code(" ],").nl(); output.code(" bar: expect([0, 1, 2], 'to have items satisfying',").nl(); output.code(" expect.it('to be a number after a short delay')),").nl(); output.code("").nl(); output.code(" baz: expect({ a: '0', b: 1 }, 'to have values satisfying',").nl(); output.code(" 'to be a number after a short delay')").nl(); output.code("});").nl(); output.error("to throw"); }); }); }).caught(function (e) { expect(e, "to have message", "failed expectation in { a: '0', b: 1 }:\n" + " a: expected '0' to be a number after a short delay" ); })); return expect.promise.all(testPromises); }); it("api/promise-any.md contains correct examples", function () { var testPromises = []; expect.addAssertion('to be a number after a short delay', function (expect, subject) { return expect.promise(function (run) { setTimeout(run(function () { expect(subject, 'to be a number'); }), 0); }); }); testPromises.push(expect.promise(function () { return expect.promise.any({ foo: [ expect('42', 'to be a number after a short delay') ], bar: expect([0, 1, 2], 'to have items satisfying', expect.it('to be a number after a short delay')), baz: expect({ a: '1', b: 2 }, 'to have values satisfying', 'to be a number after a short delay') }); })); testPromises.push(expect.promise(function () { return expect.promise.any({ foo: [ expect('42', 'to be a number after a short delay') ], bar: expect([0, '1', 2], 'to have items satisfying', expect.it('to be a number after a short delay')), baz: expect({ a: '0', b: 1 }, 'to have values satisfying', 'to be a number after a short delay') }).caught(function (aggregateError) { // Let's reformat the error a bit expect.fail(function (output) { output.error(aggregateError.message); var errors = []; for (var i = 0; i < aggregateError.length; i += 1) { errors.push(aggregateError[i]); } errors.sort(function (a, b) { // Make the output stable if (a.message < b.message) return -1; if (a.message > b.message) return 1; return 0; }); output.indentLines(); errors.forEach(function (e, i) { output.nl().i().text(i + ': ').block(e.output); }); }); }); }).then(function () { return expect.promise(function () { expect.fail(function (output) { output.error("expected:").nl(); output.code("return expect.promise.any({").nl(); output.code(" foo: [").nl(); output.code(" expect('42', 'to be a number after a short delay')").nl(); output.code(" ],").nl(); output.code(" bar: expect([0, '1', 2], 'to have items satisfying',").nl(); output.code(" expect.it('to be a number after a short delay')),").nl(); output.code("").nl(); output.code(" baz: expect({ a: '0', b: 1 }, 'to have values satisfying',").nl(); output.code(" 'to be a number after a short delay')").nl(); output.code("}).caught(function (aggregateError) {").nl(); output.code(" // Let's reformat the error a bit").nl(); output.code(" expect.fail(function (output) {").nl(); output.code(" output.error(aggregateError.message);").nl(); output.code(" var errors = [];").nl(); output.code(" for (var i = 0; i < aggregateError.length; i += 1) {").nl(); output.code(" errors.push(aggregateError[i]);").nl(); output.code(" }").nl(); output.code("").nl(); output.code(" errors.sort(function (a, b) { // Make the output stable").nl(); output.code(" if (a.message < b.message) return -1;").nl(); output.code(" if (a.message > b.message) return 1;").nl(); output.code(" return 0;").nl(); output.code(" });").nl(); output.code("").nl(); output.code(" output.indentLines();").nl(); output.code(" errors.forEach(function (e, i) {").nl(); output.code(" output.nl().i().text(i + ': ').block(e.output);").nl(); output.code(" });").nl(); output.code(" });").nl(); output.code("});").nl(); output.error("to throw"); }); }); }).caught(function (e) { expect(e, "to have message", "aggregate error\n" + " 0: expected '42' to be a number after a short delay\n" + " 1: failed expectation in [ 0, '1', 2 ]:\n" + " 1: expected '1' to be a number after a short delay\n" + " 2: failed expectation in { a: '0', b: 1 }:\n" + " a: expected '0' to be a number after a short delay" ); })); return expect.promise.all(testPromises); }); it("api/promise-settle.md contains correct examples", function () { var testPromises = []; expect.addAssertion('to be a number after a short delay', function (expect, subject) { return expect.promise(function (run) { setTimeout(run(function () { expect(subject, 'to be a number'); }), 1); }); }); testPromises.push(expect.promise(function () { var promises = { foo: expect('42', 'to be a number after a short delay'), bar: expect([0, 1, 2], 'to have items satisfying', expect.it('to be a number after a short delay')), baz: expect({ a: '1', b: 2 }, 'to have values satisfying', 'to be a number after a short delay') }; return expect.promise.all(promises).caught(function () { return expect.promise.settle(promises).then(function () { expect.fail(function (output) { output.text('{').nl(); output.indentLines(); Object.keys(promises).forEach(function (key, index) { output.i().jsKey(key).text(':').sp(); if (promises[key].isFulfilled()) { output.success('✓'); } else { output.error('⨯ ').block(promises[key].reason().output); } output.nl(); }); output.outdentLines(); output.text('}'); }); }); }); }).then(function () { return expect.promise(function () { expect.fail(function (output) { output.error("expected:").nl(); output.code("var promises = {").nl(); output.code(" foo: expect('42', 'to be a number after a short delay'),").nl(); output.code(" bar: expect([0, 1, 2], 'to have items satisfying',").nl(); output.code(" expect.it('to be a number after a short delay')),").nl(); output.code(" baz: expect({ a: '1', b: 2 }, 'to have values satisfying',").nl(); output.code(" 'to be a number after a short delay')").nl(); output.code("};").nl(); output.code("").nl(); output.code("return expect.promise.all(promises).caught(function () {").nl(); output.code(" return expect.promise.settle(promises).then(function () {").nl(); output.code(" expect.fail(function (output) {").nl(); output.code(" output.text('{').nl();").nl(); output.code(" output.indentLines();").nl(); output.code(" Object.keys(promises).forEach(function (key, index) {").nl(); output.code(" output.i().jsKey(key).text(':').sp();").nl(); output.code(" if (promises[key].isFulfilled()) {").nl(); output.code(" output.success('✓');").nl(); output.code(" } else {").nl(); output.code(" output.error('⨯ ').block(promises[key].reason().output);").nl(); output.code(" }").nl(); output.code(" output.nl();").nl(); output.code(" });").nl(); output.code(" output.outdentLines();").nl(); output.code(" output.text('}');").nl(); output.code(" });").nl(); output.code(" });").nl(); output.code("});").nl(); output.error("to throw"); }); }); }).caught(function (e) { expect(e, "to have message", "{\n" + " foo: ⨯ expected '42' to be a number after a short delay\n" + " bar: ✓\n" + " baz: ⨯ failed expectation in { a: '1', b: 2 }:\n" + " a: expected '1' to be a number after a short delay\n" + "}" ); })); return expect.promise.all(testPromises); }); it("api/withError.md contains correct examples", function () { var testPromises = []; try { function Person(options) { this.name = options.name; this.gender = options.gender; } Person.prototype.genderSign = function () { switch (this.gender) { case 'female': return '♀'; case 'male': return '♂'; default: return '⚧'; } }; expect.addAssertion('to have same gender as', function (expect, subject, value) { expect.withError(function () { expect(subject.gender, 'to be', value.gender); }, function (e) { expect.fail({ diff: function (output) { return { inline: false, diff: output.bold(subject.genderSign()).text(' ≠ ').bold(value.genderSign()) }; } }); }); }); expect(new Person({ name: 'John Doe', gender: 'male' }), 'to have same gender as', new Person({ name: 'Jane Doe', gender: 'female' })); expect.fail(function (output) { output.error("expected:").nl(); output.code("function Person(options) {").nl(); output.code(" this.name = options.name;").nl(); output.code(" this.gender = options.gender;").nl(); output.code("}").nl(); output.code("").nl(); output.code("Person.prototype.genderSign = function () {").nl(); output.code(" switch (this.gender) {").nl(); output.code(" case 'female': return '♀';").nl(); output.code(" case 'male': return '♂';").nl(); output.code(" default: return '⚧';").nl(); output.code(" }").nl(); output.code("};").nl(); output.code("").nl(); output.code("expect.addAssertion('to have same gender as', function (expect, subject, value) {").nl(); output.code(" expect.withError(function () {").nl(); output.code(" expect(subject.gender, 'to be', value.gender);").nl(); output.code(" }, function (e) {").nl(); output.code(" expect.fail({").nl(); output.code(" diff: function (output) {").nl(); output.code(" return {").nl(); output.code(" inline: false,").nl(); output.code(" diff: output.bold(subject.genderSign()).text(' ≠ ').bold(value.genderSign())").nl(); output.code(" };").nl(); output.code(" }").nl(); output.code(" });").nl(); output.code(" });").nl(); output.code("});").nl(); output.code("").nl(); output.code("expect(new Person({ name: 'John Doe', gender: 'male' }),").nl(); output.code(" 'to have same gender as',").nl(); output.code(" new Person({ name: 'Jane Doe', gender: 'female' }));").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "expected Person({ name: 'John Doe', gender: 'male' }) to have same gender as Person({ name: 'Jane Doe', gender: 'female' })\n" + "\n" + "♂ ≠ ♀" ); } return expect.promise.all(testPromises); }); it("assertions/Buffer/when-decoded-as.md contains correct examples", function () { var testPromises = []; if (!isBrowser) { expect(new Buffer([0xe2, 0x98, 0xba]), 'when decoded as', 'utf-8', 'to equal', '☺'); } if (!isBrowser) { try { expect(new Buffer([0xe2, 0x98, 0xba]), 'when decoded as', 'utf-8', 'to equal', 'happy face'); expect.fail(function (output) { output.error("expected:").nl(); output.code("expect(new Buffer([0xe2, 0x98, 0xba]), 'when decoded as', 'utf-8', 'to equal', 'happy face');").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "expected Buffer([0xE2, 0x98, 0xBA]) when decoded as 'utf-8' to equal 'happy face'\n" + "\n" + "-☺\n" + "+happy face" ); } } return expect.promise.all(testPromises); }); it("assertions/Error/to-have-message.md contains correct examples", function () { var testPromises = []; expect(new Error('foobar'), 'to have message', 'foobar'); try { expect(new Error('foobar'), 'to have message', 'barfoo'); expect.fail(function (output) { output.error("expected:").nl(); output.code("expect(new Error('foobar'), 'to have message', 'barfoo');").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "expected Error('foobar') to have message 'barfoo'\n" + " expected 'foobar' to satisfy 'barfoo'\n" + "\n" + " -foobar\n" + " +barfoo" ); } return expect.promise.all(testPromises); }); it("assertions/any/to-be-a.md contains correct examples", function () { var testPromises = []; 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" ); } return expect.promise.all(testPromises); }); it("assertions/any/to-be-defined.md contains correct examples", function () { var testPromises = []; 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" ); } return expect.promise.all(testPromises); }); it("assertions/any/to-be-falsy.md contains correct examples", function () { var testPromises = []; 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" ); } return expect.promise.all(testPromises); }); it("assertions/any/to-be-null.md contains correct examples", function () { var testPromises = []; 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" ); } return expect.promise.all(testPromises); }); it("assertions/any/to-be-ok.md contains correct examples", function () { var testPromises = []; 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" ); } return expect.promise.all(testPromises); }); it("assertions/any/to-be-truthy.md contains correct examples", function () { var testPromises = []; expect(1, 'to be truthy'); expect(true, 'to be truthy'); expect({}, 'to be truthy'); expect('foo', 'to be truthy'); expect(/foo/,