babel-plugin-espower
Babel plugin for power-assert.
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
.babelrc
via "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
Babel API
viavar babel = ;var jsCode = fs;var transformed = babel;console;
Babel Require Hook
via 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
babelify
withvar fs = ;var browserify = ;var babelify = ;var glob = ;
$ $(npm bin)/browserify -d -e ./test/*_test.js -t [ babelify --presets ... --plugins babel-plugin-espower ]
babelify and gulp
withvar source = ;var browserify = ;var glob = gulp;
babelify and Karma
withmodule { config
EXAMPLE
For given test file test/demo_test.js
below,
; ;
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.
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 = ;var jsCode = fs;var transformed = babel;console;
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: babeltypesVISITOR_KEYS astWhiteList: babeltypesBUILDER_KEYS sourceRoot: process path: fileoptsfilename sourceMap: fileinputMap ? fileinputMap : 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
.
;
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.