Gathering detailed insights and metrics for power-assert
Gathering detailed insights and metrics for power-assert
Gathering detailed insights and metrics for power-assert
Gathering detailed insights and metrics for power-assert
Power Assert in JavaScript. Provides descriptive assertion messages through standard assert interface. No API is the best API.
npm install power-assert
Typescript
Module System
Node Version
NPM Version
JavaScript (99.96%)
HTML (0.04%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
2,809 Stars
820 Commits
68 Forks
41 Watchers
15 Branches
19 Contributors
Updated on Jul 03, 2025
Latest Version
1.6.1
Package Id
power-assert@1.6.1
Size
150.25 kB
NPM Version
6.2.0
Node Version
10.8.0
Published on
Sep 08, 2018
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Power Assert in JavaScript. Provides descriptive assertion messages through standard assert interface. No API is the best API.
What is power-assert
?
assert(any_expression)
function)assert(any_expression)
.require('power-assert')
any more. Keep using require('assert')
, and power-assert enhances them transparently. See slides: From Library to Tool - power-assert as a General Purpose Assertion Enhancement Toolpower-assert - power = assert
. Without code transpilation, power-assert works just as normal assert
does.power-assert
provides descriptive assertion messages for your tests, like this.
1) Array #indexOf() should return index when the value is present:
AssertionError: # path/to/test/mocha_node.js:10
assert(ary.indexOf(zero) === two)
| | | | |
| | | | 2
| -1 0 false
[1,2,3]
[number] two
=> 2
[number] ary.indexOf(zero)
=> -1
power-assert enhances these assert functions by espower. Produces descriptive message when assertion is failed.
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])
power-assert is fully compatible with assert. So functions below are also available though they are not enhanced (does not produce descriptive message).
assert.fail(actual, expected, message, operator)
assert.throws(block, [error], [message])
assert.doesNotThrow(block, [message])
assert.ifError(value)
Since version 1.5.0, power-assert supports "strict mode" as well.
power-assert provides an API for customization.
assert.customize(options)
Though power-assert is fully compatible with standard assert interface, all you need to remember is just an assert(any_expression)
function in most cases.
The core value of power-assert is absolute simplicity and stability. Especially, power-assert sticks to the simplest form of testing, assert(any_expression)
.
assert(types[index].name === bob.name)
| || | | | |
| || | | | "bob"
| || | | Person{name:"bob",age:5}
| || | false
| |11 "alice"
| Person{name:"alice",age:3}
["string",98.6,true,false,null,undefined,#Array#,#Object#,NaN,Infinity,/^not/,#Person#]
--- [string] bob.name
+++ [string] types[index].name
@@ -1,3 +1,5 @@
-bob
+alice
TypeError: assert._capt is not a function
assert
calls in productionshould/expect
code to power-assert
?npm install --save-dev power-assert <one of instrumentors>
or
bower install --save-dev power-assert
npm install --save-dev <one of instrumentors>
See CHANGELOG
See HOW TO USE section for more details.
Note: There is an online demo site available.
1'use strict'; 2 3const assert = require('assert'); 4 5describe('Array', function(){ 6 let ary; 7 beforeEach(() => { 8 ary = [1,2,3]; 9 }); 10 describe('#indexOf()', () => { 11 it('should return index when the value is present', () => { 12 const zero = 0, two = 2; 13 assert(ary.indexOf(zero) === two); 14 }); 15 it('should return -1 when the value is not present', () => { 16 const minusOne = -1, two = 2; 17 assert.ok(ary.indexOf(two) === minusOne, 'THIS IS AN ASSERTION MESSAGE'); 18 }); 19 }); 20}); 21 22describe('various types', () => { 23 let types; 24 class Person { 25 constructor(name, age) { 26 this.name = name; 27 this.age = age; 28 } 29 } 30 beforeEach(() => { 31 types = [ 32 'string', 98.6, true, false, null, undefined, 33 ['nested', 'array'], 34 {object: true}, 35 NaN, Infinity, 36 /^not/, 37 new Person('alice', 3) 38 ]; 39 }); 40 it('demo', () => { 41 const index = types.length -1, 42 bob = new Person('bob', 5); 43 assert(types[index].name === bob.name); 44 }); 45});
To use power-assert, you need to transform your test code for power-assert output.
Code transform is done by instrumentors below:
If you are using Node.js only, the easiest way is to use intelli-espower-loader. Steps are as follows.
npm install --save-dev mocha power-assert intelli-espower-loader
Put tests into test
directory then run. You will see the power-assert output appears.
$ $(npm bin)/mocha --require intelli-espower-loader path/to/test/mocha_node.js
Array
#indexOf()
1) should return index when the value is present
2) should return -1 when the value is not present
various types
3) demo
0 passing (43ms)
3 failing
1) Array #indexOf() should return index when the value is present:
AssertionError: # test/example2.js:13
assert(ary.indexOf(zero) === two)
| | | | |
| | | | 2
| -1 0 false
[1,2,3]
[number] two
=> 2
[number] ary.indexOf(zero)
=> -1
+ expected - actual
-false
+true
at Context.it (test/example2.js:13:13)
2) Array #indexOf() should return -1 when the value is not present:
AssertionError: THIS IS AN ASSERTION MESSAGE # test/example2.js:17
assert.ok(ary.indexOf(two) === minusOne, 'THIS IS AN ASSERTION MESSAGE')
| | | | |
| | | | -1
| 1 2 false
[1,2,3]
[number] minusOne
=> -1
[number] ary.indexOf(two)
=> 1
+ expected - actual
-false
+true
at Context.it (test/example2.js:17:20)
3) various types demo:
AssertionError: # test/example2.js:43
assert(types[index].name === bob.name)
| || | | | |
| || | | | "bob"
| || | | Person{name:"bob",age:5}
| || | false
| |11 "alice"
| Person{name:"alice",age:3}
["string",98.6,true,false,null,undefined,#Array#,#Object#,NaN,Infinity,/^not/,#Person#]
--- [string] bob.name
+++ [string] types[index].name
@@ -1,3 +1,5 @@
-bob
+alice
+ expected - actual
-false
+true
at Context.it (test/example2.js:43:9)
Some seed projects are available to help you start with power-assert.
module | env | tech stack |
---|---|---|
power-assert-node-seed | Node.js | power-assert + intelli-espower-loader |
power-assert-testem-seed | Browsers(by testem) | power-assert + gulp-espower + testem. |
power-assert-karma-seed | Browsers(by Karma) | power-assert + espowerify + browserify + Karma. |
There are some ways to use power-assert. (If you want to see running examples, see SEED PROJECTS)
power-assert
+ Babel
+ babel-preset-power-assert
: The only way to enable power-assert if you are using Babel6+.power-assert
+ espower-loader
or intelli-espower-loader
: Simple and recommended (but only works under Node).power-assert
+ espower-coffee
or espower-typescript
: Use power-assert with AltJS. Recommended but only works under Node.power-assert
+ browserify
+ espowerify
: if you are using browserify but not with Babel.power-assert
+ webpack
+ webpack-espower-loader
: if you are using webpack but not with Babel.power-assert
+ espower-cli
or grunt-espower
or gulp-espower
: Generate instrumented code so works anywhere.babel-preset-power-assert
or babel-plugin-espower
If you are writing your code with Babel, you can instrument Power Assert feature with Babel and babel-preset-power-assert (or babel-plugin-espower).
see babel-plugin-espower README and babel-preset-power-assert README
espower-loader
or intelli-espower-loader
If you are writing Node.js app/module, you can instrument Power Assert feature without code generation by using espower-loader
.
FYI: You may be interested in intelli-espower-loader to go one step further. With intelli-espower-loader, you don't need to create loader file (like enable-power-assert.js
). Just define test directory in package.json
wow!
espower-typescript
If you are writing Node.js app/module in TypeScript, you can instrument Power Assert feature without code generation by using espower-typescript
.
see espower-typescript README.
espower-coffee
If you are writing Node.js app/module in CoffeeScript, you can instrument Power Assert feature without code generation by using espower-coffee
.
espowerify
If you are using browserify but not with Babel, you can instrument Power Assert feature via espowerify
.
see espowerify README.
webpack-espower-loader
If you are using webpack but not with Babel, you can instrument Power Assert feature via webpack-espower-loader
.
see webpack-espower-loader README.
espower-cli
If you don't want to use grunt, gulp, browserify, and so on, you can use power-assert
via bower, with generated code by espower-cli
see espower-cli README.
gulp-espower
On the browser side and you are not using browserify but bower and gulp, you can use power-assert
via bower, with generated code by gulp-espower
see gulp-espower README.
grunt-espower
On the browser side and you are not using browserify but bower and Grunt, you can use power-assert
via bower, with generated code by grunt-espower
see grunt-espower README.
power-assert
provides an API for customization.
Through this API, you can customize power-assert by changing some options.
1var assert = require('power-assert').customize({ 2 output: { 3 maxDepth: 2 4 } 5});
options
has two top-level keys. assertion
and output
.
customization options for empower module. See empower API documentation for details. Note that some default values are different from empower
's (modifyMessageOnRethrow: true
and saveContextOnRethrow: true
).
customization options for power-assert-formatter module. See power-assert-formatter API documentation for details.
customizable properties and their default values are as follows.
1var assert = require('power-assert').customize({ 2 assertion: { 3 destructive: false, 4 modifyMessageOnRethrow: true, 5 saveContextOnRethrow: true, 6 patterns: [ 7 'assert(value, [message])', 8 'assert.ok(value, [message])', 9 'assert.equal(actual, expected, [message])', 10 'assert.notEqual(actual, expected, [message])', 11 'assert.strictEqual(actual, expected, [message])', 12 'assert.notStrictEqual(actual, expected, [message])', 13 'assert.deepEqual(actual, expected, [message])', 14 'assert.notDeepEqual(actual, expected, [message])', 15 'assert.deepStrictEqual(actual, expected, [message])', 16 'assert.notDeepStrictEqual(actual, expected, [message])' 17 ] 18 }, 19 output: { 20 lineDiffThreshold: 5, 21 maxDepth: 1, 22 anonymous: 'Object', 23 circular: '#@Circular#', 24 lineSeparator: '\n', 25 ambiguousEastAsianCharWidth: 2, 26 widthOf: (Function to calculate width of string. Please see power-assert-formatter's documentation) 27 stringify: (Function to stringify any target value. Please see power-assert-formatter's documentation) 28 diff: (Function to create diff string between two strings. Please see power-assert-formatter's documentation) 29 writerClass: (Constructor Function for output writer class. Please see power-assert-formatter's documentation) 30 renderers: [ 31 './built-in/file', 32 './built-in/assertion', 33 './built-in/diagram', 34 './built-in/binary-expression' 35 ] 36 } 37});
power-assert
family provides 1 main module, 4 core modules and many more instrumentors.
Main (facade) module is,
module | description |
---|---|
power-assert | Standard assert function on top of empower and power-assert-formatter |
core modules are,
module | description |
---|---|
empower | Power Assert feature enhancer for assert function/object. |
power-assert-formatter | Power Assert output formatter. |
espower | Power Assert feature instrumentor core based on the ECMAScript AST defined in The ESTree Spec (formerly known as Mozilla SpiderMonkey Parser API). |
espower-source | Power Assert instrumentor from source to source, with source-map. (Thin wrapper of espower ). |
and instrumentors are,
module | description |
---|---|
espower-loader | Node module loader to apply espower on the fly. |
intelli-espower-loader | configure espower-loader with ease. |
babel-preset-power-assert | Babel preset to instrument power-assert feature into target files. |
babel-plugin-espower | Babel plugin to instrument power-assert feature into target files. |
espowerify | Browserify transform to apply espower to target files. |
webpack-espower-loader | Power Assert instrumentor module for webpack. |
espower-cli | Command line tool for power-assert. |
grunt-espower | Grunt task to apply espower to target files. |
gulp-espower | Gulp plugin to apply espower to target files. |
karma-espower-preprocessor | karma-preprocessor for power-assert. |
espower-coffee | power-assert instrumentor for CoffeeScript. |
espower-typescript | power-assert instrumentor for TypeScript. |
espower-traceur | power-assert instrumentor for ES6 using Traceur Compiler. |
espower-babel | [DEPRECATED] power-assert instrumentor for ES6 using Babel. |
power-assert
provides standard assert compatible function with Power Assert feature.
(Best fit with Mocha. If you use assert-like objects provided by various testing frameworks such as QUnit or nodeunit. Please use empower and power-assert-formatter modules directly).
Internally, power-assert
uses empower module to enhance power assert feature into the standard assert module, to run with the power assert feature added code by espower module, and prettify output using power-assert-formatter.
See power-assert-demo project for power-assert Demo running with mocha.
For the Transpiler side, we support Node under maintenance. In other words, we stop supporting old Node version when their maintenance ends.
For the Runtime side, we support Node under maintenance and "modern enough" browsers such as Chrome, Firefox, Safari, Edge etc.
Any other environments are not supported officially (means that we do not test against them on CI service). power-assert is known to work with old browsers, and trying to keep them working though.
Licensed under the MIT license.
1var q = require('qunitjs'); 2 3(function () { 4 var empower = require('empower'), 5 formatter = require('power-assert-formatter'), 6 qunitTap = require("qunit-tap"); 7 empower(q.assert, formatter(), {destructive: true}); 8 qunitTap(q, require('util').puts, {showSourceOnFailure: false}); 9 q.config.autorun = false; 10})(); 11 12q.test('spike', function (assert) { 13 assert.ok(true); 14 15 var hoge = 'foo'; 16 var fuga = 'bar'; 17 assert.ok(hoge === fuga, 'comment'); 18 19 var piyo = 3; 20 assert.ok(fuga === piyo); 21 22 var longString = 'very very loooooooooooooooooooooooooooooooooooooooooooooooooooong message'; 23 var anotherLongString = 'yet another loooooooooooooooooooooooooooooooooooooooooooooooooooong message'; 24 assert.ok(longString === anotherLongString); 25 26 assert.ok(4 === piyo); 27 28 assert.ok(4 !== 4); 29 30 var falsyStr = ''; 31 assert.ok(falsyStr); 32 33 var falsyNum = 0; 34 assert.ok(falsyNum); 35 36 var ary1 = ['foo', 'bar']; 37 var ary2 = ['aaa', 'bbb', 'ccc']; 38 assert.ok(ary1.length === ary2.length); 39 assert.deepEqual(ary1, ary2); 40 41 var actual = 16; 42 assert.ok(5 < actual && actual < 13); 43 44 actual = 4; 45 assert.ok(5 < actual && actual < 13); 46 47 actual = 10; 48 assert.ok(actual < 5 || 13 < actual); 49 50 51 var propName = 'bar', 52 foo = { 53 bar: { 54 baz: false 55 } 56 }; 57 58 assert.ok(foo.bar.baz); 59 assert.ok(foo['bar'].baz); 60 assert.ok(foo[propName]['baz']); 61 62 63 var truth = true; 64 assert.ok(!truth); 65 66 67 var func = function () { return false; }; 68 assert.ok(func()); 69 70 71 var obj = { 72 age: function () { 73 return 0; 74 } 75 }; 76 assert.ok(obj.age()); 77 78 79 var isFalsy = function (arg) { 80 return !(arg); 81 }; 82 var positiveInt = 50; 83 assert.ok(isFalsy(positiveInt)); 84 85 86 var sum = function () { 87 var result = 0; 88 for (var i = 0; i < arguments.length; i += 1) { 89 result += arguments[i]; 90 } 91 return result; 92 }; 93 var one = 1, two = 2, three = 3, seven = 7, ten = 10; 94 assert.ok(sum(one, two, three) === seven); 95 assert.ok(sum(sum(one, two), three) === sum(sum(two, three), seven)); 96 assert.ok((three * (seven * ten)) === three); 97 98 99 var math = { 100 calc: { 101 sum: function () { 102 var result = 0; 103 for (var i = 0; i < arguments.length; i += 1) { 104 result += arguments[i]; 105 } 106 return result; 107 } 108 } 109 }; 110 assert.ok(math.calc.sum(one, two, three) === seven); 111}); 112 113q.load();
espower
code above then running under Node.js# module: undefined
# test: spike
ok 1 - okay
not ok 2 - comment # path/to/examples/qunit_node.js:17
#
# assert.ok(hoge === fuga, 'comment')
# | | |
# | | "bar"
# | false
# "foo"
#
# --- [string] fuga
# +++ [string] hoge
# @@ -1,3 +1,3 @@
# -bar
# +foo
#
# , test: spike
not ok 3 - # path/to/examples/qunit_node.js:20
#
# assert.ok(fuga === piyo)
# | | |
# | | 3
# | false
# "bar"
#
# [number] piyo
# => 3
# [string] fuga
# => "bar"
# , test: spike
not ok 4 - # path/to/examples/qunit_node.js:24
#
# assert.ok(longString === anotherLongString)
# | | |
# | | "yet another loooooooooooooooooooooooooooooooooooooooooooooooooooong message"
# | false
# "very very loooooooooooooooooooooooooooooooooooooooooooooooooooong message"
#
# --- [string] anotherLongString
# +++ [string] longString
# @@ -1,15 +1,13 @@
# -yet anoth
# +very v
# er
# +y
# loo
#
# , test: spike
not ok 5 - # path/to/examples/qunit_node.js:26
#
# assert.ok(4 === piyo)
# | |
# | 3
# false
#
# [number] piyo
# => 3
# [number] 4
# => 4
# , test: spike
not ok 6 - # path/to/examples/qunit_node.js:28
#
# assert.ok(4 !== 4)
# |
# false
# , test: spike
not ok 7 - # path/to/examples/qunit_node.js:31
#
# assert.ok(falsyStr)
# |
# ""
# , test: spike
not ok 8 - # path/to/examples/qunit_node.js:34
#
# assert.ok(falsyNum)
# |
# 0
# , test: spike
not ok 9 - # path/to/examples/qunit_node.js:38
#
# assert.ok(ary1.length === ary2.length)
# | | | | |
# | | | | 3
# | | | ["aaa","bbb","ccc"]
# | 2 false
# ["foo","bar"]
#
# [number] ary2.length
# => 3
# [number] ary1.length
# => 2
# , test: spike
not ok 10 - # path/to/examples/qunit_node.js:39
#
# assert.deepEqual(ary1, ary2)
# | |
# | ["aaa","bbb","ccc"]
# ["foo","bar"]
# , expected: [
# "aaa",
# "bbb",
# "ccc"
# ], got: [
# "foo",
# "bar"
# ], test: spike
not ok 11 - # path/to/examples/qunit_node.js:42
#
# assert.ok(5 < actual && actual < 13)
# | | | | |
# | | | 16 false
# | 16 false
# true
# , test: spike
not ok 12 - # path/to/examples/qunit_node.js:45
#
# assert.ok(5 < actual && actual < 13)
# | | |
# | 4 false
# false
# , test: spike
not ok 13 - # path/to/examples/qunit_node.js:48
#
# assert.ok(actual < 5 || 13 < actual)
# | | | | |
# | | | | 10
# | | false false
# 10 false
# , test: spike
not ok 14 - # path/to/examples/qunit_node.js:58
#
# assert.ok(foo.bar.baz)
# | | |
# | | false
# | Object{baz:false}
# Object{bar:#Object#}
# , test: spike
not ok 15 - # path/to/examples/qunit_node.js:59
#
# assert.ok(foo['bar'].baz)
# | | |
# | | false
# | Object{baz:false}
# Object{bar:#Object#}
# , test: spike
not ok 16 - # path/to/examples/qunit_node.js:60
#
# assert.ok(foo[propName]['baz'])
# | || |
# | |"bar" false
# | Object{baz:false}
# Object{bar:#Object#}
# , test: spike
not ok 17 - # path/to/examples/qunit_node.js:64
#
# assert.ok(!truth)
# ||
# |true
# false
# , test: spike
not ok 18 - # path/to/examples/qunit_node.js:68
#
# assert.ok(func())
# |
# false
# , test: spike
not ok 19 - # path/to/examples/qunit_node.js:76
#
# assert.ok(obj.age())
# | |
# | 0
# Object{age:#function#}
# , test: spike
not ok 20 - # path/to/examples/qunit_node.js:83
#
# assert.ok(isFalsy(positiveInt))
# | |
# false 50
# , test: spike
not ok 21 - # path/to/examples/qunit_node.js:94
#
# assert.ok(sum(one, two, three) === seven)
# | | | | | |
# | | | | | 7
# 6 1 2 3 false
#
# [number] seven
# => 7
# [number] sum(one, two, three)
# => 6
# , test: spike
not ok 22 - # path/to/examples/qunit_node.js:95
#
# assert.ok(sum(sum(one, two), three) === sum(sum(two, three), seven))
# | | | | | | | | | | |
# | | | | | | 12 5 2 3 7
# 6 3 1 2 3 false
#
# [number] sum(sum(two, three), seven)
# => 12
# [number] sum(sum(one, two), three)
# => 6
# , test: spike
not ok 23 - # path/to/examples/qunit_node.js:96
#
# assert.ok(three * (seven * ten) === three)
# | | | | | | |
# | | | | | | 3
# | | | | 10 false
# | | 7 70
# 3 210
#
# [number] three
# => 3
# [number] three * (seven * ten)
# => 210
# , test: spike
not ok 24 - # path/to/examples/qunit_node.js:110
#
# assert.ok(math.calc.sum(one, two, three) === seven)
# | | | | | | | |
# | | | | | | | 7
# | | 6 1 2 3 false
# | Object{sum:#function#}
# Object{calc:#Object#}
#
# [number] seven
# => 7
# [number] math.calc.sum(one, two, three)
# => 6
# , test: spike
1..24
Have fun!
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 2/13 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
49 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More