包详细信息

espower

power-assert-js187.7kMIT2.1.2

Power Assert feature instrumentor based on the ECMAScript AST

power-assert, assert, assertion, test

自述文件

espower

Build Status NPM package Dependency Status Coverage Status Code Climate License

Power Assert feature instrumentor based on the ECMAScript AST.

DESCRIPTION

espower is a core module of power-assert family.

espower detects and manipulates assertion expression (JavaScript Code) in the form of ECMAScript AST defined in The ESTree Spec (formerly known as Mozilla SpiderMonkey Parser API), to instrument power-assert feature into returned new AST object. AST in, AST out. Since 0.11.0, espower can transform ES6 AST as well.

Pull-requests, issue reports and patches are always welcomed. See power-assert project for more documentation.

CHANGELOG

See CHANGELOG

API

var modifiedAst = espower(ast, [options])

return type
object

espower function manipulates ast then returns modifiedAst that is also an AST node object defined in The ESTree Spec. ast will be manipulated directly and returned modifiedAst will be the same instance of ast.

espower function throws EspowerError when

  • ast is already instrumented
  • ast does not contain location information
  • options argument is not valid

var visitor = espower.createVisitor(ast, [options])

return type
object

espower.createVisitor generates visitor object to be used with estraverse.replace. Arguments are the same as espower function.

ast

type default value
object N/A

ast should be an AST node object defined in The ESTree Spec.

options

type default value
object (return value of espower.defaultOptions())

Configuration options. If not passed, default options will be used.

options.patterns

type default value
Array of string objects shown below
[
    'assert(value, [message])',
    'assert.ok(value, [message])',
    'assert.equal(actual, expected, [message])',
    'assert.notEqual(actual, expected, [message])',
    'assert.strictEqual(actual, expected, [message])',
    'assert.notStrictEqual(actual, expected, [message])',
    'assert.deepEqual(actual, expected, [message])',
    'assert.notDeepEqual(actual, expected, [message])',
    'assert.deepStrictEqual(actual, expected, [message])',
    'assert.notDeepStrictEqual(actual, expected, [message])'
]

Target patterns for power assert feature instrumentation.

If callee name (for example, assert.equal) matches exactly and number of arguments is satisfied, then the assertion will be modified. Detection is done by escallmatch. Any arguments enclosed in bracket (for example, [message]) means optional parameters. Without bracket means mandatory parameters.

options.ecmaVersion

type default value
number 2018

The ECMAScript version to parse and analyze. Must be either 3, 5, 6 (2015), 2016, 2017, or 2018.

options.sourceType

type default value
string 'module'

The source type of the code. Must be either "script" or "module".

(optional) options.path

type default value
string N/A

Filepath of originalAst. If passed, espower stores filepath information for reporting. If options.path is absolute and it conflicts with options.sourceRoot or sourceRoot in options.sourceMap, then filepath in power-assert output will be fall back on basename of options.path. This property is optional.

(optional) options.sourceRoot

type default value
string N/A

Root filepath for target test files. Only works with options.path or options.sourceMap. If set, filepath in power-assert output will be relative from options.sourceRoot. When both options.sourceRoot and sourceMap's sourceRoot are given and both are absolute filepath, options.sourceRoot has precedence over sourceMap's sourceRoot. This property is optional.

(optional) options.sourceMap

type default value
object or string N/A

A raw (either as a string which can be JSON.parse'd, or an object) SourceMap associated with originalAst. This property is optional. If given, espower uses options.sourceMap to adjust information in the power-assert output.

(optional) options.visitorKeys

type default value
object N/A

VisitorKeys for AST traversal. See estraverse.VisitorKeys and babel.types.VISITOR_KEYS.

var options = espower.defaultOptions();

Returns default options object for espower function. In other words, returns

{
    ecmaVersion: 2018,
    sourceType: 'module',
    patterns: [
        'assert(value, [message])',
        'assert.ok(value, [message])',
        'assert.equal(actual, expected, [message])',
        'assert.notEqual(actual, expected, [message])',
        'assert.strictEqual(actual, expected, [message])',
        'assert.notStrictEqual(actual, expected, [message])',
        'assert.deepEqual(actual, expected, [message])',
        'assert.notDeepEqual(actual, expected, [message])',
        'assert.deepStrictEqual(actual, expected, [message])',
        'assert.notDeepStrictEqual(actual, expected, [message])'
    ]
}

EXAMPLE

For given test file example_test.js below,

var assert = require('power-assert');
var truthy = 'true';
var falsy = 'false';
assert(falsy);
assert.equal(truthy, falsy);

Apply espower then generate modified code to console,

var espower = require('espower');
var esprima = require('esprima');
var escodegen = require('escodegen');
var fs = require('fs');
var path = require('path');

var filepath = path.join(__dirname, 'example_test.js');
var jsAst = esprima.parse(fs.readFileSync(filepath), {tolerant: true, loc: true, tokens: true});
var modifiedAst = espower(jsAst, {path: filepath, sourceRoot: __dirname});

console.log(escodegen.generate(modifiedAst));

Output:

var _PowerAssertRecorder1 = function () {
    function PowerAssertRecorder() {
        this.captured = [];
    }
    PowerAssertRecorder.prototype._capt = function _capt(value, espath) {
        this.captured.push({
            value: value,
            espath: espath
        });
        return value;
    };
    PowerAssertRecorder.prototype._expr = function _expr(value, source) {
        var capturedValues = this.captured;
        this.captured = [];
        return {
            powerAssertContext: {
                value: value,
                events: capturedValues
            },
            source: source
        };
    };
    return PowerAssertRecorder;
}();
var _rec1 = new _PowerAssertRecorder1();
var _rec2 = new _PowerAssertRecorder1();
var _rec3 = new _PowerAssertRecorder1();
var assert = require('power-assert');
var truthy = 'true';
var falsy = 'false';
assert(_rec1._expr(_rec1._capt(falsy, 'arguments/0'), {
    content: 'assert(falsy)',
    filepath: 'example_test.js',
    line: 4
}));
assert.equal(_rec2._expr(_rec2._capt(truthy, 'arguments/0'), {
    content: 'assert.equal(truthy, falsy)',
    filepath: 'example_test.js',
    line: 5
}), _rec3._expr(_rec3._capt(falsy, 'arguments/1'), {
    content: 'assert.equal(truthy, falsy)',
    filepath: 'example_test.js',
    line: 5
}));

INSTALL

via npm

Install

$ npm install --save-dev espower

OUR SUPPORT POLICY

We support Node under maintenance. In other words, we stop supporting old Node version when their maintenance ends.

This means that any other environment is not supported.

NOTE: If espower works in any of the unsupported environments, it is purely coincidental and has no bearing on future compatibility. Use at your own risk.

AUTHOR

CONTRIBUTORS

LICENSE

Licensed under the MIT license.

更新日志

2.1.2 (2019-01-11)

Bug Fixes

Refactor

Chore

2.1.1 (2018-05-15)

  • Update default ecmaVersion to 2018 (f315b49)

2.1.0 (2017-05-21)

Features

2.0.3 (2017-02-19)

Bug Fixes

2.0.2 (2017-01-12)

Bug Fixes

2.0.1 (2016-11-16)

Bug Fixes

2.0.0 (2016-11-11)

Features

Breaking Changes

We stopped providing prebuilt bundle for browsers. Please build your own by using browserify, webpack and so on. We also dropped bower support. Please use npm instead.

  • Internal classes espower.Instrumentor and espower.AssertionVisitor are not exported any more.

    (afa380f9)

The destructive option has been removed.

New behavior is like destructive: true, means that passed AST is modified directly. If you do not want your AST to be modified directly, you should deep-clone your AST by yourself.

1.3.2 (2016-06-22)

Bug Fixes

  • fix breaking changes introduced in 1.3.0 and 1.3.1

1.3.1 (2016-06-21)

Bug Fixes

  • stop capturing SequenceExpression itself since SequenceExpressions are not enclosed in parentheses in some cases (e8acbc61)

1.3.0 (2016-06-21)

Features

1.2.1 (2015-11-06)

1.2.0 (2015-11-05)

1.1.0 (2015-11-03)

1.0.7 (2015-09-21)

  • update escodegen to 1.7.0 (feb96b0)
  • update source-map to 0.5.0 (7bc824b)

1.0.6 (2015-06-07)

  • SourceMap's sourceRoot should have precedence over options.sourceRoot if sourceRoot is URL (dcf9642d)

1.0.5 (2015-06-05)

  • update escallmatch to 1.4.2 and espurify to 1.3.0 (62973ed)
  • use licensify to prepend license header (d3bc5e8)

1.0.4 (2015-06-04)

  • try to create relative path if sourceMap.sourceRoot is locating source files on on a server (ec0a91ba)
  • never show absolute path or URL in power-assert output (252b043f)

1.0.3 (2015-05-31)

  • use basename when sourceRoot in options.sourceMap is locating source files on a server (7822df1b)

1.0.2 (2015-05-30)

  • use basename when incoming options.path is absolute and it conflicts with options.sourceRoot or sourceRoot in options.sourceMap (02f7b35a)

1.0.1 (2015-05-29)

  • apply options.sourceRoot if and only if incoming options.path is absolute (65b4012a)

1.0.0 (2015-05-25)

Features

0.11.0 (2015-04-18)

Bug Fixes

  • throw Error if AST is already instrumented (1d47bdc3)

Features

0.10.0 (2014-11-11)

0.9.1 (2014-09-15)

Bug Fixes

  • espower: decide to be skipped first, then enter node (9d0a778a)

0.9.0 (2014-08-21)

Features

  • espower:
    • adjust filepath in power-assert output if sourceMap option is given (f919d59d)
    • adjust line number in power-assert output if sourceMap option is given (8c035d89)

0.8.0 (2014-08-12)

Features

Breaking Changes

  • option powerAssertVariableName is now deprecated and ignored. Please use patterns option instead (2f023f91)
  • option targetMethods is now deprecated and ignored. Please use patterns option instead (e75e5d35)

If you already customize instrumentation pattern using powerAssertVariableName and targetMethods, you need to migarte. To migrate, change your code from the following:

var options = {
    powerAssertVariableName: 'yourAssert',
    targetMethods: {
        oneArg: [
            'okay'
        ],
        twoArgs: [
            'equal',
            'customEqual'
        ]
    }
};
var modifiedAst = espower(jsAst, options);

To:

var options = {
    patterns: [
        'yourAssert(value, [message])',
        'yourAssert.okay(value, [message])',
        'yourAssert.equal(actual, expected, [message])',
        'yourAssert.customEqual(actual, expected, [message])'
    ]
};
var modifiedAst = espower(jsAst, options);