diff --git a/lib/unexpected-magicpen.js b/lib/unexpected-magicpen.js index f75b6d2..0209a44 100644 --- a/lib/unexpected-magicpen.js +++ b/lib/unexpected-magicpen.js @@ -1,170 +1,189 @@ module.exports = { - name: 'unexpected-magicpen', - version: require('../package.json').version, - installInto: function unexpectedMagicPen(expect) { - expect.addType({ - name: 'magicpen', - identify: function (obj) { - return obj && obj.isMagicPen; - }, - inspect: function (pen, depth, output) { - output.magicPen(pen); - }, - equal: function (a, b) { - if (a.format !== b.format) { - return false; - } - if (a.format) { - // Both have the same format - return a.toString() === b.toString(); - } else { - // Neither have a format, test all serializations - return a.toString() === b.toString() && - a.toString('ansi') === b.toString('ansi') && - a.toString('html') === b.toString('html'); - } + name: 'unexpected-magicpen', + version: require('../package.json').version, + installInto: function unexpectedMagicPen(expect) { + expect.addType({ + name: 'magicpen', + identify: function (obj) { + return obj && obj.isMagicPen; + }, + inspect: function (pen, depth, output) { + output.magicPen(pen); + }, + equal: function (a, b) { + if (a.format !== b.format) { + return false; + } + if (a.format) { + // Both have the same format + return a.toString() === b.toString(); + } else { + // Neither have a format, test all serializations + return ( + a.toString() === b.toString() && + a.toString('ansi') === b.toString('ansi') && + a.toString('html') === b.toString('html') + ); + } + }, + }); + + expect.addStyle('magicPenLine', function (line, pen) { + line.forEach(function (lineEntry, j) { + if (j > 0) { + this.nl(); + } + if (lineEntry.style === 'text') { + var styles = lineEntry.args.styles; + if ( + pen && + styles.length === 1 && + typeof pen[styles[0]] === 'function' + ) { + // Text with a single style also available as a method on the pen being inspected: + this.text('.') + .jsFunctionName(styles[0]) + .text('(') + .singleQuotedString(lineEntry.args.content) + .text(')'); + } else { + this.text('.') + .jsFunctionName('text') + .text('(') + .singleQuotedString(lineEntry.args.content); + if (styles.length > 0) { + this.text(', ').appendInspected( + styles.length === 1 && Array.isArray(styles[0]) + ? styles[0] + : styles + ); } - }); + this.text(')'); + } + } else if (lineEntry.style === 'raw') { + this.text('.') + .jsFunctionName('raw') + .text('(') + .appendInspected(lineEntry.args.content()) + .text(')'); + } else { + // lineEntry.style === 'block' + this.text('.') + .jsFunctionName('block') + .text('(') + .jsKeyword('function') + .text(' () {'); + if ( + lineEntry.args && + lineEntry.args.length > 0 && + lineEntry.args[0] && + lineEntry.args[0].length > 0 + ) { + this.nl() + .indentLines() + .i() + .magicPen(pen, lineEntry.args) + .outdentLines() + .nl(); + } + this.text('})'); + } + }, this); + }); - expect.addStyle('magicPenLine', function (line, pen) { - line.forEach(function (lineEntry, j) { - if (j > 0) { - this.nl(); - } - if (lineEntry.style === 'text') { - var styles = lineEntry.args.styles; - if (pen && styles.length === 1 && typeof pen[styles[0]] === 'function') { - // Text with a single style also available as a method on the pen being inspected: - this - .text('.') - .jsFunctionName(styles[0]) - .text('(') - .singleQuotedString(lineEntry.args.content) - .text(')'); - } else { - this - .text('.') - .jsFunctionName('text') - .text('(') - .singleQuotedString(lineEntry.args.content); - if (styles.length > 0) { - this - .text(', ') - .appendInspected(styles.length === 1 && Array.isArray(styles[0]) ? styles[0] : styles); - } - this.text(')'); - } - } else if (lineEntry.style === 'raw') { - this - .text('.') - .jsFunctionName('raw') - .text('(') - .appendInspected(lineEntry.args.content()) - .text(')'); - } else { - // lineEntry.style === 'block' - this - .text('.') - .jsFunctionName('block').text('(').jsKeyword('function').text(' () {'); - if (lineEntry.args && lineEntry.args.length > 0 && lineEntry.args[0] && lineEntry.args[0].length > 0) { - this - .nl() - .indentLines() - .i() - .magicPen(pen, lineEntry.args) - .outdentLines() - .nl(); - } - this.text('})'); - } + expect.addStyle('magicPen', function (pen, lines) { + var isTopLevel = !lines; + lines = lines || pen.output; + this.block(function () { + if (isTopLevel) { + this.jsFunctionName('magicpen').text('('); + if (pen.format) { + this.singleQuotedString(pen.format); + } + this.text(')'); + } else { + this.jsKeyword('this'); + } + if (!pen.isEmpty()) { + var inspectOnMultipleLines = lines.length > 1 || lines[0].length > 1; + if (inspectOnMultipleLines) { + this.nl().indentLines().i(); + } + this.block(function () { + lines.forEach(function (line, i) { + if (i > 0) { + this.text('.').jsFunctionName('nl').text('()').nl(); + } + this.magicPenLine(line, pen); }, this); - }); - - expect.addStyle('magicPen', function (pen, lines) { - var isTopLevel = !lines; - lines = lines || pen.output; - this.block(function () { - if (isTopLevel) { - this.jsFunctionName('magicpen').text('('); - if (pen.format) { - this.singleQuotedString(pen.format); - } - this.text(')'); - } else { - this.jsKeyword('this'); - } - if (!pen.isEmpty()) { - var inspectOnMultipleLines = lines.length > 1 || lines[0].length > 1; - if (inspectOnMultipleLines) { - this - .nl() - .indentLines() - .i(); - } - this.block(function () { - lines.forEach(function (line, i) { - if (i > 0) { - this.text('.').jsFunctionName('nl').text('()').nl(); - } - this.magicPenLine(line, pen); - }, this); - if (!isTopLevel) { - this.text(';'); - } - }); - if (inspectOnMultipleLines) { - this.outdentLines(); - } - } - }); - - // If we're at the top level of a non-empty pen compatible with the current output, - // render the output of the pen in a comment: - if (isTopLevel && !pen.isEmpty() && (pen.format === this.format || !pen.format)) { - this.sp().commentBlock(pen); + if (!isTopLevel) { + this.text(';'); } - }); + }); + if (inspectOnMultipleLines) { + this.outdentLines(); + } + } + }); - expect.addAssertion(' to have (ansi|html|text|) (message|diff) ', function (expect, subject, value) { - expect.errorMode = 'nested'; - var format = expect.alternations[0] || 'text'; - var useDiff = expect.alternations[1] === 'diff'; - if (subject.isUnexpected) { - var subjectPen; - if (useDiff) { - var diff = subject.getDiff({ format: format }); - if (diff) { - subjectPen = diff; - } else { - expect.fail('The UnexpectedError instance does not have a diff'); - } - } else { - subjectPen = subject.getErrorMessage({ format: format }); - } - var valueType = expect.argTypes[0]; - if (valueType.is('magicpen')) { - expect(subjectPen, 'to equal', value); - } else if (valueType.is('function') && !valueType.is('expect.it')) { - var expectedOutput = expect.createOutput(format); - var returnValue = value.call(expectedOutput, subjectPen.toString()); - if (!expectedOutput.isEmpty()) { - // If the function didn't generate any expected output, assume that it ran assertions based on the serialized message - expect(subjectPen, 'to equal', expectedOutput); - } - return returnValue; - } else { - return expect(subjectPen.toString(), 'to satisfy', value); - } + // If we're at the top level of a non-empty pen compatible with the current output, + // render the output of the pen in a comment: + if ( + isTopLevel && + !pen.isEmpty() && + (pen.format === this.format || !pen.format) + ) { + this.sp().commentBlock(pen); + } + }); + + expect.addAssertion( + ' to have (ansi|html|text|) (message|diff) ', + function (expect, subject, value) { + expect.errorMode = 'nested'; + var format = expect.alternations[0] || 'text'; + var useDiff = expect.alternations[1] === 'diff'; + if (subject.isUnexpected) { + var subjectPen; + if (useDiff) { + var diff = subject.getDiff({ format: format }); + if (diff) { + subjectPen = diff; } else { - if (useDiff) { - expect.fail('Cannot get the diff from a non-Unexpected error'); - } - if (format !== 'text') { - expect.fail('Cannot get the ' + format + ' representation of non-Unexpected error'); - } else { - return expect(subject.message, 'to satisfy', value); - } + expect.fail('The UnexpectedError instance does not have a diff'); + } + } else { + subjectPen = subject.getErrorMessage({ format: format }); + } + var valueType = expect.argTypes[0]; + if (valueType.is('magicpen')) { + expect(subjectPen, 'to equal', value); + } else if (valueType.is('function') && !valueType.is('expect.it')) { + var expectedOutput = expect.createOutput(format); + var returnValue = value.call(expectedOutput, subjectPen.toString()); + if (!expectedOutput.isEmpty()) { + // If the function didn't generate any expected output, assume that it ran assertions based on the serialized message + expect(subjectPen, 'to equal', expectedOutput); } - }); - } + return returnValue; + } else { + return expect(subjectPen.toString(), 'to satisfy', value); + } + } else { + if (useDiff) { + expect.fail('Cannot get the diff from a non-Unexpected error'); + } + if (format !== 'text') { + expect.fail( + 'Cannot get the ' + + format + + ' representation of non-Unexpected error' + ); + } else { + return expect(subject.message, 'to satisfy', value); + } + } + } + ); + }, }; diff --git a/rollup.config.js b/rollup.config.js index 20eb0b3..1179713 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -3,33 +3,29 @@ const commonjs = require('rollup-plugin-commonjs'); const resolve = require('rollup-plugin-node-resolve'); const json = require('rollup-plugin-json'); -const plugins = [ - commonjs(), - resolve(), - json() -]; +const plugins = [commonjs(), resolve(), json()]; module.exports = [ - { - input: pkg.main, - output: { - file: 'unexpected-magicpen.min.js', - name: 'weknowhow.unexpectedMagicPen', - format: 'umd', - exports: 'named', - legacy: true, - strict: false - }, - plugins + { + input: pkg.main, + output: { + file: 'unexpected-magicpen.min.js', + name: 'weknowhow.unexpectedMagicPen', + format: 'umd', + exports: 'named', + legacy: true, + strict: false, + }, + plugins, + }, + { + input: pkg.main, + output: { + file: 'unexpected-magicpen.esm.js', + format: 'esm', + exports: 'named', + strict: false, }, - { - input: pkg.main, - output: { - file: 'unexpected-magicpen.esm.js', - format: 'esm', - exports: 'named', - strict: false - }, - plugins - } + plugins, + }, ]; diff --git a/test/unexpected-magicpen.spec.js b/test/unexpected-magicpen.spec.js index f7d8a67..9d18690 100644 --- a/test/unexpected-magicpen.spec.js +++ b/test/unexpected-magicpen.spec.js @@ -1,110 +1,169 @@ var expect = require('unexpected') - .clone() - .use(require('../lib/unexpected-magicpen')); + .clone() + .use(require('../lib/unexpected-magicpen')); -expect.addAssertion(' to inspect as ', function (expect, subject, value) { +expect + .addAssertion(' to inspect as ', function ( + expect, + subject, + value + ) { expect(expect.inspect(subject).toString(), 'to equal', value); -}).addAssertion(' when delayed a little bit ', function (expect, subject) { + }) + .addAssertion(' when delayed a little bit ', function ( + expect, + subject + ) { return expect.promise(function (run) { - setTimeout(run(function () { - return expect.shift(); - }), 1); + setTimeout( + run(function () { + return expect.shift(); + }), + 1 + ); }); -}); + }); describe('magicpen type', function () { - describe('#inspect', function () { - it('should inspect an empty MagicPen instance', function () { - expect(expect.output.clone().magicPen(expect.output.clone()).toString(), 'to equal', 'magicpen()'); - }); - - it('should find two pens with different formats to not to be identical', function () { - var MagicPen = expect.output.constructor; - expect(new MagicPen('text').text('foo'), 'not to equal', new MagicPen('ansi').text('foo')); - }); + describe('#inspect', function () { + it('should inspect an empty MagicPen instance', function () { + expect( + expect.output.clone().magicPen(expect.output.clone()).toString(), + 'to equal', + 'magicpen()' + ); + }); - it('should find two format-less pens with the same contents to be identical', function () { - var MagicPen = expect.output.constructor; - expect(new MagicPen().text('foo'), 'to equal', new MagicPen().text('foo')); - }); + it('should find two pens with different formats to not to be identical', function () { + var MagicPen = expect.output.constructor; + expect( + new MagicPen('text').text('foo'), + 'not to equal', + new MagicPen('ansi').text('foo') + ); + }); - describe('with a pen in text format', function () { - var pen = expect.createOutput('text').green('abc').nl().text('def').block(function () { - this.text('foo').nl().text('bar'); - }); + it('should find two format-less pens with the same contents to be identical', function () { + var MagicPen = expect.output.constructor; + expect( + new MagicPen().text('foo'), + 'to equal', + new MagicPen().text('foo') + ); + }); - it('should inspect correctly', function () { - expect(pen, 'to inspect as', - "magicpen('text') // abc\n" + - " .green('abc').nl() // deffoo\n" + - " .text('def') // bar\n" + - ' .block(function () {\n' + - ' this\n' + - " .text('foo').nl()\n" + - " .text('bar');\n" + - ' })' - ); - }); + describe('with a pen in text format', function () { + var pen = expect + .createOutput('text') + .green('abc') + .nl() + .text('def') + .block(function () { + this.text('foo').nl().text('bar'); }); - describe('with a pen in ansi format', function () { - var pen = expect.createOutput('ansi').green('abc').text('def').block(function () { - this.text('foo'); - }); + it('should inspect correctly', function () { + expect( + pen, + 'to inspect as', + "magicpen('text') // abc\n" + + " .green('abc').nl() // deffoo\n" + + " .text('def') // bar\n" + + ' .block(function () {\n' + + ' this\n' + + " .text('foo').nl()\n" + + " .text('bar');\n" + + ' })' + ); + }); + }); - it('should inspect correctly', function () { - expect(pen, 'to inspect as', - "magicpen('ansi')\n" + - " .green('abc')\n" + - " .text('def')\n" + - ' .block(function () {\n' + - " this.text('foo');\n" + - ' })' - ); - }); + describe('with a pen in ansi format', function () { + var pen = expect + .createOutput('ansi') + .green('abc') + .text('def') + .block(function () { + this.text('foo'); }); - describe('with a pen in html format', function () { - var pen = expect.createOutput('html').green('abc').text('def').block(function () { - this.text('foo'); - }); + it('should inspect correctly', function () { + expect( + pen, + 'to inspect as', + "magicpen('ansi')\n" + + " .green('abc')\n" + + " .text('def')\n" + + ' .block(function () {\n' + + " this.text('foo');\n" + + ' })' + ); + }); + }); - it('should inspect correctly', function () { - expect(pen, 'to inspect as', - "magicpen('html')\n" + - " .green('abc')\n" + - " .text('def')\n" + - ' .block(function () {\n' + - " this.text('foo');\n" + - ' })' - ); - }); + describe('with a pen in html format', function () { + var pen = expect + .createOutput('html') + .green('abc') + .text('def') + .block(function () { + this.text('foo'); }); + + it('should inspect correctly', function () { + expect( + pen, + 'to inspect as', + "magicpen('html')\n" + + " .green('abc')\n" + + " .text('def')\n" + + ' .block(function () {\n' + + " this.text('foo');\n" + + ' })' + ); + }); }); + }); }); describe('magicPen style', function () { - it('renders a raw entry', function () { - expect(expect.createOutput('text').raw('foo'), 'to inspect as', "magicpen('text').raw('foo') // foo"); - }); + it('renders a raw entry', function () { + expect( + expect.createOutput('text').raw('foo'), + 'to inspect as', + "magicpen('text').raw('foo') // foo" + ); + }); - it('renders an empty block', function () { - expect( - expect.createOutput('text').block(function () {}), - 'to inspect as', - "magicpen('text').block(function () {}) // " - ); - }); + it('renders an empty block', function () { + expect( + expect.createOutput('text').block(function () {}), + 'to inspect as', + "magicpen('text').block(function () {}) // " + ); + }); - it('renders text with an empty array of styles', function () { - expect(expect.createOutput('text').text('foo', []), 'to inspect as', "magicpen('text').text('foo', []) // foo"); - }); + it('renders text with an empty array of styles', function () { + expect( + expect.createOutput('text').text('foo', []), + 'to inspect as', + "magicpen('text').text('foo', []) // foo" + ); + }); - it('renders text with a single style not defined as a top-level style', function () { - expect(expect.createOutput('text').text('foo', ['blabla']), 'to inspect as', "magicpen('text').text('foo', [ 'blabla' ]) // foo"); - }); + it('renders text with a single style not defined as a top-level style', function () { + expect( + expect.createOutput('text').text('foo', ['blabla']), + 'to inspect as', + "magicpen('text').text('foo', [ 'blabla' ]) // foo" + ); + }); - it('renders text with several styles', function () { - expect(expect.createOutput('text').text('foo', ['quux', 'baz']), 'to inspect as', "magicpen('text').text('foo', [ 'quux', 'baz' ]) // foo"); - }); + it('renders text with several styles', function () { + expect( + expect.createOutput('text').text('foo', ['quux', 'baz']), + 'to inspect as', + "magicpen('text').text('foo', [ 'quux', 'baz' ]) // foo" + ); + }); });