Skip to content

power-assert-js/espower-babel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

espower-babel Build Status

power-assert instrumentor for Babel.

This module is wrapper of babel-plugin-espower.

⚠️ Deprecated ⚠️

espower-babel is deprecated module.

Please directly use combination of babel-register and babel-preset-power-assert.

❤️ We've created migration tool: migrate-espower-babel-to-babel-preset-power-assert.

migrate-espower-babel-to-babel-preset-power-assert migrate babel-register + babel-preset-power-assert from espower-babel.

- espower-babel
+ babel-register
+ babel-preset-power-assert

Related Issue: Deprecated: espower-babel · Issue #27 · power-assert-js/espower-babel

Purpose

  • Writing ES6 tests with Babel
  • Running tests with power-assert on the fly!
  • No configuration

DESCRIPTION

espower-babel is a Node.js module loader that instruments power-assert feature into target ECMAScript6 sources on the fly.

Please note that espower-babel is a beta version product. Pull-requests, issue reports and patches are always welcomed. See power-assert project for more documentation.

If you want to use with Traceur, please see yosuke-furukawa/espower-traceur.

EXAMPLE

Given test/demo_test.js

let assert = require('power-assert')

class Person {
  constructor(name, age) {
    this.name = name
    this.age = age
  }
  getAge() {
    return this.age
  }
}

describe("Person", ()=>{
  let alice = new Person("alice", 3)
  let bob = new Person("bob", 5)
  it("#getAge", ()=>{
    assert(alice.getAge() === 3)
  })
  it("#name", ()=>{
    assert(alice.name === "alice")
  })
  // failed
  it("#mistake", ()=>{
    assert(alice.name === bob.name)
  })
})

Run mocha with --compilers js:espower-babel/guess

$ mocha --compilers js:espower-babel/guess test/demo_test.js

  ․․․

  2 passing (17ms)
  1 failing

  1) Person #mistake:
     AssertionError:   # test/demo_test.js:24

  assert(alice.name === bob.name)
         |     |    |   |   |
         |     |    |   |   "bob"
         |     |    |   Person{name:"bob",age:5}
         |     |    false
         |     "alice"
         Person{name:"alice",age:3}

  --- [string] bob.name
  +++ [string] alice.name
  @@ -1,3 +1,5 @@
  -bob
  +alice

See the power-assert output appears!

INSTALL

$ npm install espower-babel -D

HOW TO USE

Zero-config mode(for testing)

Edit: Babel@6 require .babelrc. not work on zero config.

You can use directry babel-plugin-espower. We have a simple migration tool(work on specific case) - migrate-espower-babel-to-babel-plugin-espower

If your tests are located on 'test/**/*.js', just run mocha with --compilers js:espower-babel/guess

$ mocha --compilers js:espower-babel/guess test/**/*.js

If your tests are not in test dir

You can set test directory in your package.json

{
    "name": "your-module",
    "description": "Your module",
    "version": "0.0.1",
    "directories": {
        "test": "spec/"
    },
...
}

Then, run mocha with --compilers js:espower-babel/guess

$ mocha --compilers js:espower-babel/guess spec/**/*.js

Note: 'espower-babel/guess' is inspired by intelli-espower-loader

More customization

If you want to configure more explicitly, put espower-babel-loader.js somewhere in your project.

require('espower-babel')({
    // directory where match starts with
    cwd: process.cwd(),

    // glob pattern using minimatch module
    pattern: 'spec/unit/**/*.js',

    // options for espower module
    espowerOptions: {
        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])'
        ]
    }
});

Then, run mocha with --require option

$ mocha --require ./path/to/espower-babel-loader spec/unit/some_test_using_powerassert.js

Babel transform options

Babel has many transform options.

espower-babel support babel transform options with .babelrc.

espower-babel read ${cwd}/.babelrc if exists.

Also, you can manually configure babel transform options.

e.g.)

require('espower-babel')({
    babelrc: {
        "presets": ["es2015"],
        "plugins": ["transform-es2015-modules-commonjs"]
    }
})

Caution:

Babel 6 does not transform your code by default. It means that you must set babel config by .babelrc file or babelrc option.

Transform all files with Babel by default

Do limit transform files by setting babelrc

e.g.)

require('espower-babel')({
    babelrc: {
        only: [
            "src/**/*.js"
        ]
    }
})

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

License

MIT

Includes yosuke-furukawa/espower-traceur

Acknowledgements

Thanks to yosuke-furukawa/espower-traceur.