Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

babel-plugin-espower

power-assert-js265.9kMIT3.0.1

Babel plugin for power-assert

assert, assertion, babel, babel-plugin, power-assert, test, testing, transform

readme

babel-plugin-espower

Babel plugin for power-assert.

Build Status NPM version Dependency Status License

DESCRIPTION

babel-plugin-espower is a Babel plugin for power-assert.

power-assert provides descriptive assertion messages for your tests, like this.

  1) ES6 demo Destructuring and TemplateLiteral:

      AssertionError:   # test/demo_test.js:7

  assert(`${ alice.name } and ${ bob.name }` === `bob and alice`)
         |   |     |             |   |       |   |
         |   |     |             |   |       |   "bob and alice"
         |   |     |             |   "bob"   false
         |   |     "alice"       Object{name:"bob"}
         |   Object{name:"alice"}
         "alice and bob"

  --- [string] `bob and alice`
  +++ [string] `${ alice.name } and ${ bob.name }`
  @@ -1,13 +1,13 @@
  -bob and alice
  +alice and bob

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

FYI: There's a babel preset for all power-assert plugins

INSTALL

$ npm install --save-dev babel-plugin-espower

CAUTION

Babel7 is incompatible with Babel6. Babel6 is incompatible with Babel5.

For Babel6, you need to use the 2.x release of babel-plugin-espower.

$ npm install --save-dev babel-plugin-espower@2

For Babel5 or lower, you need to use the 1.x release of babel-plugin-espower.

$ npm install --save-dev babel-plugin-espower@1

HOW TO USE

via .babelrc

{
  "presets": [
    ...
  ],
  "plugins": [
    "babel-plugin-espower"
  ]
}
$ babel /path/to/test/some_test.js > /path/to/build/some_test.js

For example, with @babel/register module and .babelrc above, you can run mocha without code generation steps.

$ $(npm bin)/mocha --require @babel/register test/some_test.js

via Babel API

var babel = require('@babel/core');
var jsCode = fs.readFileSync('/path/to/test/some_test.js');
var transformed = babel.transform(jsCode, {
    presets: [...],
    plugins: ['babel-plugin-espower']
});
console.log(transformed.code);

via Babel Require Hook

require('@babel/register')({
    presets: [...],
    plugins: ['babel-plugin-espower']
});

For example, with babel_hook.js above, you can run mocha without code generation steps.

$ $(npm bin)/mocha --require ./babel_hook /path/to/test/demo_test.js

with babelify

var fs = require('fs');
var browserify = require('browserify');
var babelify = require('babelify');
var glob = require('glob'),
browserify({ entries: glob.sync('./test/*_test.js'), debug: true })
  .transform(babelify.configure({
      presets: [...],
      plugins: ['babel-plugin-espower']
  }))
  .bundle()
  .on('error', function (err) { console.log('Error : ' + err.message); })
  .pipe(fs.createWriteStream('all_test.js'));
$ $(npm bin)/browserify -d -e ./test/*_test.js -t [ babelify --presets ... --plugins babel-plugin-espower ]

with babelify and gulp

var source = require('vinyl-source-stream');
var browserify = require('browserify');
var glob = require('glob'),
gulp.task('build_test', function() {
    var files = glob.sync('./test/*_test.js');
    var b = browserify({entries: files, debug: true});
    b.transform(babelify.configure({
        presets: [...],
        plugins: ['babel-plugin-espower']
    }));
    return b.bundle()
        .pipe(source('all_test.js'))
        .pipe(gulp.dest('./build'));
});

with babelify and Karma

module.exports = function(config) {
  config.set({
    frameworks: ['mocha', 'browserify'],
    files: [
      "test/**/*.js"
    ],
    preprocessors: {
      "test/**/*.js": "browserify"
    },
    browserify: {
      debug: true,
      transform: [
        [
          'babelify', {
            presets: [...],
            plugins: ['babel-plugin-espower']
          }
        ]
      ]
    },
    // ...

EXAMPLE

For given test file test/demo_test.js below,

import assert from 'power-assert';

describe('ES6 demo', () => {

    it('Destructuring and TemplateLiteral', () => {
        let [alice, bob] = [ { name: 'alice' }, { name: 'bob' } ];
        assert(`${alice.name} and ${bob.name}` === `bob and alice`);
    });

    it('ArrowFunctionExpression and SpreadElement', () => {
        let seven = 7, ary = [4, 5];
        assert(seven === ((v, i) => v + i)(...[...ary]));
    });

    it('Enhanced Object Literals', () => {
        let name = 'bobby';
        assert.deepEqual({
            name,
            [ `${name}'s greet` ]: `Hello, I'm ${name}`
        }, null);
    });

});

prepare babel_hook.js to transform tests.

{
  "presets": [
    ...
  ],
  "plugins": [
    "babel-plugin-espower"
  ]
}

Run mocha with --require @babel/register option. You will see the power-assert output appears.

$ $(npm bin)/mocha --require @babel/register test/demo_test.js

  ES6 demo
    1) Destructuring and TemplateLiteral
    2) ArrowFunctionExpression and SpreadElement
    3) Enhanced Object Literals


  0 passing
  3 failing

  1) ES6 demo Destructuring and TemplateLiteral:

      AssertionError:   # test/demo_test.js:7

  assert(`${ alice.name } and ${ bob.name }` === `bob and alice`)
         |   |     |             |   |       |   |
         |   |     |             |   |       |   "bob and alice"
         |   |     |             |   "bob"   false
         |   |     "alice"       Object{name:"bob"}
         |   Object{name:"alice"}
         "alice and bob"

  --- [string] `bob and alice`
  +++ [string] `${ alice.name } and ${ bob.name }`
  @@ -1,13 +1,13 @@
  -bob and alice
  +alice and bob

      at Context.<anonymous> (test/demo_test.js:19:28)

  2) ES6 demo ArrowFunctionExpression and SpreadElement:

      AssertionError:   # test/demo_test.js:12

  assert(seven === ((v, i) => v + i)(...[...ary]))
         |     |   |                    |   |
         |     |   |                    |   [4,5]
         |     |   9                    [4,5]
         7     false

  [number] ((v, i) => v + i)(...[...ary])
  => 9
  [number] seven
  => 7

      at Context.<anonymous> (test/demo_test.js:29:28)

  3) ES6 demo Enhanced Object Literals:
     AssertionError:   # test/demo_test.js:17

  assert.deepEqual({name,[`${ name }'s greet`]: `Hello, I'm ${ name }`}, null)
                   |      |   |                 |              |
                   |      |   |                 |              "bobby"
                   |      |   "bobby"           "Hello, I'm bobby"
                   |      "bobby's greet"
                   Object{name:"bobby","bobby's greet":"Hello, I'm bobby"}

      at Context.<anonymous> (test/demo_test.js:40:29)

CUSTOMIZE

by plugin options

You can customize configs such as assertion patterns via .babelrc

{
  "presets": [
    ...
  ],
  "plugins": [
    ["babel-plugin-espower", {
      "embedAst": true,
      "patterns": [
        "assert.isNull(object, [message])",
        "assert.same(actual, expected, [message])",
        "assert.near(actual, expected, delta, [message])"
      ]
    }]
  ]
}

or via Require Hook.

require('@babel/register')({
    presets: [...],
    plugins: [
        ['babel-plugin-espower', {
            embedAst: true,
            patterns: [
                'assert.isNull(object, [message])',
                'assert.same(actual, expected, [message])',
                'assert.near(actual, expected, delta, [message])'
            ]
        }]
    ]
});

or via Babel API,

var babel = require('@babel/core');
var jsCode = fs.readFileSync('/path/to/test/some_test.js');
var transformed = babel.transform(jsCode, {
    presets: [...],
    plugins: [
        ['babel-plugin-espower', {
            embedAst: true,
            patterns: [
                'assert.isNull(object, [message])',
                'assert.same(actual, expected, [message])',
                'assert.near(actual, expected, delta, [message])'
            ]
        }]
    ]
});
console.log(transformed.code);

options

type default value
object objects shown below

Configuration options for babel-plugin-espower. If not passed, default options will be used (return value of defaultOptions() with default embedAst, visitorKeys, astWhiteList, path, sourceRoot and sourceMap. visitorKeys is value of babel.types.VISITOR_KEYS. astWhiteList is value of babel.types.BUILDER_KEYS. path is filename passed to babel. sourceRoot is be return value of process.cwd(), sourceMap is babel's internal SourceMap object).

{
    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])'
    ],
    embedAst: true,
    visitorKeys: babel.types.VISITOR_KEYS,
    astWhiteList: babel.types.BUILDER_KEYS,
    sourceRoot: process.cwd(),
    path: file.opts.filename,
    sourceMap: file.inputMap ? file.inputMap.toObject() : false
}

options.embedAst

If you want to use non-ECMASCript-standard features such as JSX tags in your assert(), you should set embedAst option to true.

assert(shallow(<Foo />).is('.foo'));

CHANGELOG

See CHANGELOG

AUTHOR

CONTRIBUTORS

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 babel-plugin-espower works in any of the unsupported environments, it is purely coincidental and has no bearing on future compatibility. Use at your own risk.

LICENSE

Licensed under the MIT license.

changelog

3.0.1 (2019-01-08)

Bug Fixes

3.0.0 (2018-08-30)

Features

  • Babel7 support

Breaking Changes

  • Babel7 is incompatible with Babel6

For Babel6, you need to use the 2.x release of babel-plugin-espower.

$ npm install --save-dev babel-plugin-espower@2

3.0.0-beta.2 (2018-07-20)

Chore

  • Rename babylon to @babel/parser to sync with upstream Babel7 changes (0db432e)

3.0.0-beta.1 (2017-12-25)

Features

Breaking Changes

  • Babel7 is incompatible with Babel6. Babel6 is incompatible with Babel5.

For Babel6, you need to use the 2.x release of babel-plugin-espower.

$ npm install --save-dev babel-plugin-espower@2.3.2

For Babel5 or lower, you need to use the 1.x release of babel-plugin-espower.

$ npm install --save-dev babel-plugin-espower@1.1.1
  • An internal & interim module with-experimental-syntax.js is removed, since embedAst is true by default in 3.0.0.

Notice

Changing embedAst option's default to true does not break builds but may slow your build time down. If you are aware that you are not using non-ES-standard syntax, changing embedAst option to false restores the former behavior.

2.4.0 (2018-01-16)

Features

Notice

with-experimental-syntax.js is an internal & interim module and will be removed from next major version, since embedAst will be true by default in next major.

2.3.2 (2017-01-13)

Bug Fixes

2.3.1 (2016-06-21)

Bug Fixes

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

2.3.0 (2016-06-21)

Features

2.2.0 (2016-05-31)

Features

2.1.2 (2016-02-14)

Bug Fixes

  • do not include comments in one-line format assertion (c5af6c55)

2.1.1 (2016-01-26)

Performance Improvements

2.1.0 (2016-01-10)

Features

2.0.0 (2015-11-13)

Features

Breaking Changes

Babel6 is incompatible with Babel5. For Babel 5 or lower, you need to use the 1.x release of babel-plugin-espower.

$ npm install --save-dev babel-plugin-espower@1.1.0

1.1.1 (2016-06-22)

Bug Fixes

  • pin espower dependency to >=1.0.0 <1.3.0 since there was a little breaking change (f9a7d781)

1.1.0 (2015-11-06)

Features

1.0.1 (2015-11-07)

Bug Fixes

  • pin espower dependency to >=1.0.0 <=1.2.0 since there was a little breaking change (6420b3dc, closes #8)

1.0.0 (2015-05-25)

Features

  • use process.cwd() for sourceRoot option value (71016432)
  • update espower to 1.0.0 (337cdfd2)

0.4.1 (2015-05-21)

Bug Fixes

  • eliminate remaining babel-core dependencies (7735ed5f)

0.4.0 (2015-05-21)

Bug Fixes

  • avoid visiting replacement node (c4da8f8f)

Features

  • use new 5.2.0+ API with shared babel-core. No more peerDependencies. (39eb684b)

Breaking Changes

  • function returned by babel-plugin-espower/create takes babel instance as a first argument.

If you are customizing babel-plugin-espower using babel-plugin-espower/create, you may have to migrate.

To migrate, change your code from the following:

var babel = require('babel-core');
var createEspowerPlugin = require('babel-plugin-espower/create');
var transformed = babel.transform(jsCode, {
    plugins: [
        createEspowerPlugin({
            patterns: [
                'assert.isNull(object, [message])',
                'assert.same(actual, expected, [message])',
                'assert.near(actual, expected, delta, [message])'
            ]
        })
    ]
});

To:

var babel = require('babel-core');
var createEspowerPlugin = require('babel-plugin-espower/create');
var transformed = babel.transform(jsCode, {
    plugins: [
        createEspowerPlugin(babel, {
            patterns: [
                'assert.isNull(object, [message])',
                'assert.same(actual, expected, [message])',
                'assert.near(actual, expected, delta, [message])'
            ]
        })
    ]
});

(39eb684b)

0.3.1 (2015-05-18)

Bug Fixes

  • use version range since babel-core 5.4.3 does not work. (3b586fa9

0.3.0 (2015-05-01)

Bug Fixes

  • deal with babel 5.2.x internal changes. (17698583, closes #3)

0.2.2 (2015-04-24)

  • update escallmatch to 1.3.2 (941c75c2)

0.2.1 (2015-04-21)

  • down peerDependencies to minimum version to make dedupe friendly (093ce106)

0.2.0 (2015-04-19)

  • export create.js to customize assertion patterns (092c3464)
  • docs about customizing assertion patterns via Babel API (f72a9b1)

0.1.0 (2015-04-18)

The first release.