You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4.8 KiB

power-assert

Build Status NPM version License

Create function to format powerAssertContext object provided by power-assert at runtime.

USAGE

var createFormatter = require('power-assert-context-formatter');
var FileRenderer = require('power-assert-renderer-file');
var AssertionRenderer = require('power-assert-renderer-assertion');
var DiagramRenderer = require('power-assert-renderer-diagram');
var ComparisonRenderer = require('power-assert-renderer-comparison');

var format = createFormatter({
    renderers: [
        FileRenderer,
        AssertionRenderer,
        DiagramRenderer,
        ComparisonRenderer
    ]
});

var assert = require('assert');

var foo = 'foo';
var bar = 'bar';
try {
    assert(foo === bar);
} catch (e) {
    var formattedText = format(e.powerAssertContext);
    . . .
}

API

var createFormatter = require('power-assert-context-formatter');

return type
function

Returns creator function of formatter.

var format = createFormatter(options);

return type
function

Create format function to format powerAssertContext object provided by power-assert.

options.renderers

type default value
Array of function or Array of object null

Array of constructor function of various Renderers. Each Renderer is instantiated for each assertion and registered to ContextTraversal.

customization

Each renderer accepts its options via form of object literal.

var format = createFormatter({
    renderers: [
        { ctor: FileRenderer },
        { ctor: AssertionRenderer },
        { ctor: DiagramRenderer, options: { maxDepth: 2 } },
        { ctor: ComparisonRenderer, options: { lineDiffThreshold: 3 } }
    ]
});

options.reducers

type default value
Array of function empty array

Array of reducer function to be applied to powerAssertContext.

options.outputOffset

type default value
number 2

Number of spaces inserted at the left in power-assert output.

options.lineSeparator

type default value
string "\n"

Line separator in power assert output.

options.legacy

type default value
boolean false

When true, new renderers and legacy renderer implementations can be used together.

var formattedText = format(powerAssertContext);

return type
string

Format powerAssertContext into formattedText. powerAssertContext is an internal object structure, containing informations to render. Example of powerAssertContext is:

{
    source: {
        content: 'assert(foo === bar)',
        filepath: 'test/some_test.js',
        line: 1,
        ast: '### JSON representation of AST nodes ###',
        tokens: '### JSON representation of AST tokens ###',
        visitorKeys: '### JSON representation of AST visitor keys ###'
    },
    args: [
        {
            value: false,
            events: [
                {
                    value: "FOO",
                    espath: "arguments/0/left"
                },
                {
                    value: "BAR",
                    espath: "arguments/0/right"
                },
                {
                    value: false,
                    espath: "arguments/0"
                }
            ]
        }
    ]
}

INSTALL

$ npm install --save-dev power-assert-context-formatter

AUTHOR

LICENSE

Licensed under the MIT license.