Register to get access to free programming courses with interactive exercises

power-assert JS: Automated testing

Conventional asserts have a powerful alternative: power-assert. This library adds a little magic to a familiar tool.

Here's an example of a check using the standard assert module:

const user = {
  name: 'Madonna',
  friends: ['Kate', 'Michel'],
  email: '',

assert( === 'Michel');

// AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:
//  assert( === 'Michel')

The output shows the statement itself and the result of the test. But it isn't clear what the user object is and what structure it has. To get this information, you'll have to get involved in debugging. But you can instead use the power-assert library:

import assert from 'power-assert';

// All the code remains the same
const user = {
  name: 'Madonna',
  friends: ['Kate', 'Michel'],
  email: '',

// The power-assert library interface is 100% compatible with the built-in assert module
assert( === 'Michel');

And look at the output:

AssertionError [ERR_ASSERTION]:   # test.js:10

  assert( === 'Michel')
         |    |    |
         |    |    false
         |    "Madonna"

  --- [string] 'Michel'
  +++ [string]
  @@ -1,6 +1,7 @@

Try to stop and examine this conclusion carefully. What is shown here? power-assert makes debugging as easy as physically possible. It shows the value of each object and the result of each operation included in the expression passed to the assert function. In addition, at the end it compares the lines and says exactly what the difference was between them.

Here's another interesting example from the documentation:

import assert from 'power-assert';

const array = [1, 2, 3];
const zero = 0;
const two = 2;

assert(array.indexOf(zero) === two);

// AssertionError [ERR_ASSERTION]:   # test.js:7
//   assert(array.indexOf(zero) === two)
//          |     |       |     |   |
//          |     |       |     |   2
//          |     -1      0     false
//          [1,2,3]
//   [number] two
//   => 2
//   [number] array.indexOf(zero)
//   => -1

Impressed? Most modern frameworks don't produce an output as convenient as what power-assert does. It can be integrated with anything, but you will need additional tools to get this mapping, such as Babel / Webpack or other libraries listed in the documentation.

An example of using power-assert with Babel:

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

About Hexlet learning process

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
hours of theory

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing:">Bookmate</span>
<span class="translation_missing" title="translation missing:">Healthsamurai</span>
<span class="translation_missing" title="translation missing:">Dualboot</span>
<span class="translation_missing" title="translation missing:">Abbyy</span>
Suggested learning programs
Development of front-end components for web applications
10 months
from scratch
Start at any time

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.