power-assert

power-assert (GitHub: twada/power-assert, License: MIT) is a
module written by Takuto Wada that provides pretty damn descriptive assert
messages for your tests.

1
npm install power-assert

Features

power-assert transforms your regular boring asserts to this:

1
2
3
4
5
6
7
8
AssertionError: # /path/to/examples/mocha_node.js:10
    assert(this.ary.indexOf(who) === two);
                |   |       |    |   |
                |   |       |    |   2
                |   |       |    false
                |   -1      "ariya"
                [1,2,3]

Usages

There are two ways to use power-assert.

  1. power-assert + espower-loader : Only works under Node.
  2. power-assert + grunt-espower or gulp-espower : Generates instrumented code so works anywhere.

To give you an idea what the trasnformed code looks like, for example, it transforms:

1
assert(this.ary.indexOf(who) === two);

into:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
assert(assert._expr(assert._capt(assert._capt(assert._capt(this.ary, 'ident', {
    start: {
        line: 9,
        column: 20
    }
}).indexOf(assert._capt(who, 'ident', {
    start: {
        line: 9,
        column: 32
    }
})), 'funcall', {
    start: {
        line: 9,
        column: 24
    }
}) === assert._capt(two, 'ident', {
    start: {
        line: 9,
        column: 41
    }
}), 'binary', {
    start: {
        line: 9,
        column: 37
    }
}), {
    start: {
        line: 9,
        column: 15
    },
    path: '/Users/alexgorbatchev/1-npmawesome/1/power-assert-demo/test/power_assert/mocha_power_assert_test.js'
}, '        assert(this.ary.indexOf(who) === two);'));

power-assert also works with CoffeeScript files to produce output:

1
2
3
4
5
6
7
8
AssertionError:
    assert this.ary.indexOf(who) isnt minusOne
                |   |       |    |    |
                |   |       |    |    -1
                |   |       |    false
                |   -1      "ariya"
                [1,2,3]

I’m very tempted to give this a try over my goto chai.js library. I’m a little bit worried that it changes number of lines and it might be harder to do certain type of debugging, but the benefit might be worth it!

There’s a full documentation on the power-assert github page and a demo project. What do you think?

Post navigation

Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *