power-assert

raw JSON →
1.6.1 verified Sat Apr 25 auth: no javascript

Power Assert in JavaScript v1.6.1 (stable, released 2017). Provides descriptive assertion messages via the standard Node.js assert interface. No API is the best API – you only need assert(any_expression). Requires code transformation (via Babel, browserify, webpack, etc.) to generate detailed failure messages. Differentiated from chai/should by reusing the native assert module; you can swap require('power-assert') for require('assert') with zero API change. Works server-side and browser-side. Maintained by the original author but has been stable with infrequent releases.

error TypeError: assert(expression) is not a function
cause Importing power-assert incorrectly (e.g., using named import).
fix
Use import 'power-assert' as a side-effect import, not expecting named exports.
error AssertionError [ERR_ASSERTION]: Expression is not truthy
cause power-assert transformation did not run; test code not transpiled.
fix
Add babel-plugin-espower to Babel config and run test with Babel.
error Cannot find module 'power-assert'
cause power-assert not installed in node_modules.
fix
Run npm install --save-dev power-assert.
error ReferenceError: require is not defined
cause Using `require('assert')` in ES module (type: module) environment.
fix
Switch to ES module import: import assert from 'assert' after import 'power-assert'.
gotcha Without code transformation, power-assert falls back to normal assert output.
fix Add a code transformation step (Babel plugin, browserify transform, webpack loader, etc.) to your build process.
breaking In v1.0.0, the API changed to require side-effect import (import 'power-assert') instead of requiring the module directly.
fix Use import 'power-assert' at module top; still call require('assert') for assertions.
deprecated Bower package is maintained but no longer updated; Bower itself is deprecated.
fix Use npm to install power-assert.
gotcha power-assert relies on AST transformation; test files must be transpiled before execution.
fix Ensure test files are transformed via Babel, browserify, or other tools. Use babel-plugin-espower or eslint-plugin-espower as part of the build.
gotcha When using mocha with power-assert, you must configure the test runner to perform transformation (e.g., require babel-register).
fix Run mocha with --require babel-register and Babel config with babel-plugin-espower.
npm install power-assert
yarn add power-assert
pnpm add power-assert

Usage with Babel: augment assert, then use native assert methods; power-assert provides detailed diffs.

// Install: npm install --save-dev power-assert babel-plugin-espower
// .babelrc: { "plugins": ["babel-plugin-espower"] }
// test.js
import 'power-assert';
const assert = require('assert');

const a = [1, 2, 3];
const b = [1, 2, 3, 4];
assert.deepStrictEqual(a, b);

// Without transformation: AssertionError [ERR_ASSERTION]: [1,2,3] deepStrictEqual [1,2,3,4]
// With power-assert transformation:
//   assert.deepStrictEqual(a, b)
//                         |  |
//                         |  [1,2,3,4]
//                         [1,2,3]