Socket
Socket
Sign inDemoInstall

babel-plugin-espower

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-espower

Babel plugin for power-assert


Version published
Weekly downloads
72K
decreased by-10.85%
Maintainers
1
Weekly downloads
 
Created
Source

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.

INSTALL

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

HOW TO USE

via Babel CLI

$ $(npm bin)/babel --plugins babel-plugin-espower /path/to/test/some_test.js > /path/to/build/some_test.js

or shortly,

$ $(npm bin)/babel --plugins espower /path/to/test/some_test.js > /path/to/build/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, {
    plugins: ['babel-plugin-espower']
});
console.log(transformed.code);

via Babel Require Hook

require('babel/register')({
    only: /test\/tobe_instrumented/,
    plugins: ['babel-plugin-espower'],
    extensions: ['.es6', '.js']
});

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({ 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 --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({
        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', {plugins: ['babel-plugin-espower']}]
      ]
    },
    // ...

EXAMPLE

For given test file 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);
    });

});

Run babel with --plugins espower to transform tests.

$ $(npm bin)/babel --plugins espower /path/to/test/demo_test.js > /path/to/build/demo_test.js

Then run. You will see the power-assert output appears.

$ $(npm bin)/mocha /path/to/build/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> (build/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> (build/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> (build/demo_test.js:40:29)

CUSTOMIZE

var plugin = createEspowerPlugin(babel, [options])

You can customize assertion patterns via Babel API,

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

or via Require Hook.

var createEspowerPlugin = require('babel-plugin-espower/create');
require('babel/register')({
    only: /test\/tobe_instrumented/,
    plugins: [
        createEspowerPlugin(babel, {
            patterns: [
                'assert.isNull(object, [message])',
                'assert.same(actual, expected, [message])',
                'assert.near(actual, expected, delta, [message])'
            ]
        })
    ],
    extensions: ['.es6', '.js']
});
options
typedefault value
object(return value of espower.defaultOptions() with default visitorKeys, path, sourceRoot and sourceMap)

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

CHANGELOG

See CHANGELOG

AUTHOR

CONTRIBUTORS

LICENSE

Licensed under the MIT license.

Keywords

FAQs

Package last updated on 06 Nov 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc