power-assert-js/power-assert

Unable to compose power-assert + espower in vanilla node 7 or 8

TehShrike opened this issue · 9 comments

I am trying to use power-assert to get pretty assert output in node, without using mocha or babel.

I'm trying to implement assertions by reading the documentation, but haven't been able to get it working so far.

Here is my attempt at a "vanilla node" (no mocha) seed: https://github.com/TehShrike/power-assert-espower-repro

entry point:

require('espower-loader')({
	pattern: 'test/*.js',
})

require('./test/test.js')

test/test.js:

const assert = require('power-assert')

const test = () => {
	const value1 = true
	const value2 = 3
	assert.deepEqual({ value: value1 }, { value: value2 })
}

test()

Instead of a nice pretty error message, I get:

/Users/josh/code/power-assert-espower-repro/node_modules/empower-core/lib/decorator.js:110
        ret = func.apply(thisObj, args);
                   ^
AssertionError: { value: true } deepEqual { value: 3 }
    at Decorator._callFunc (/Users/josh/code/power-assert-espower-repro/node_modules/empower-core/lib/decorator.js:110:20)
(snip)

What am I missing? I'm guessing this is a documentation/implementation issue, and not an actual bug.

(node 7.6.0)

twada commented

@TehShrike Thank you for reporting and creating repro case!

I confirmed that vanilla assertions under Node v6 produces pretty output but Node v7 doesn't (but Node v7 + mocha produces pretty output as well).

I'll investigate this issue a bit deeper.

Awesome, thank you so much.

Looks like node 8 behaves the same as node 7.

twada commented

@TehShrike Thank you for the information. I'm bit busy this week but still investigating...

azu commented

I've figured out this.

// MIT © 2017 azu
const assert = require("assert");
try {
    assert(false);
} catch (e) {
    e.message = "custom message";
    throw e;
}

Node.js 6


/Users/azu/.ghq/github.com/TehShrike/power-assert-espower-repro/assert-error.js:7
    throw e;
    ^
AssertionError: custom message
    at Object.<anonymous> (/Users/azu/.ghq/github.com/TehShrike/power-assert-espower-repro/assert-error.js:4:5)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.runMain (module.js:590:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3

Node.js 7

/Users/azu/.ghq/github.com/TehShrike/power-assert-espower-repro/assert-error.js:7
    throw e;
    ^
AssertionError: false == true
    at Object.<anonymous> (/Users/azu/.ghq/github.com/TehShrike/power-assert-espower-repro/assert-error.js:4:5)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:420:7)
    at startup (bootstrap_node.js:139:9)
    at bootstrap_node.js:535:3

Node.js 8:

(Error code is added: nodejs/node#12651)

/Users/azu/.ghq/github.com/TehShrike/power-assert-espower-repro/assert-error.js:7
    throw e;
    ^

AssertionError [ERR_ASSERTION]: false == true
    at Object.<anonymous> (/Users/azu/.ghq/github.com/TehShrike/power-assert-espower-repro/assert-error.js:4:5)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Function.Module.runMain (module.js:605:10)
    at startup (bootstrap_node.js:158:16)
    at bootstrap_node.js:575:3

Actually, empower does e.message = buildPowerAssertText(formatter, errorEvent.originalMessage, errorEvent.powerAssertContext);.

But, Node.js 7~ does not show this e.message of throwing AssertionError.

I think that Node.js 7~ treat AssertionError as special object.

try {
    throw new Error("!");
} catch (e) {
    e.message = "custom message";
    throw e;
}

Node 8 work fine.

✈ node assert-error.js
/Users/azu/.ghq/github.com/TehShrike/power-assert-espower-repro/assert-error.js:7
    throw e;
    ^

Error: custom message
    at Object.<anonymous> (/Users/azu/.ghq/github.com/TehShrike/power-assert-espower-repro/assert-error.js:4:11)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Function.Module.runMain (module.js:605:10)
    at startup (bootstrap_node.js:158:16)
    at bootstrap_node.js:575:3
azu commented

This change is introduced by nodejs/node#11273

After updating the dependencies in my power-assert-espower-repro repository (and removing the duplicate log/throw), this is the output using node 8:

> power-assert-espower-repro@1.0.0 test /Users/josh/code/power-assert-espower-repro
> node index.js

AssertionError [ERR_ASSERTION]:   # test/test.js:6
  
  assert.deepEqual({ value: value1 }, { value: value2 })
                   |        |         |        |        
                   |        |         |        3        
                   |        true      Object{value:3}   
                   Object{value:true}    
twada commented

@TehShrike Yeah we did it! Thank you for reporting again.

twada commented

This issue is fixed by power-assert 1.4.4