question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
azucommented, Jun 1, 2017

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: https://github.com/nodejs/node/pull/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
1reaction
TehShrikecommented, Jun 9, 2017

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}    
Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating and testing a complete Node.js Rest API (WITH NO ...
I'm gonna show you how to use only the Node.js native modules ... ... try restarting your device. Your browser can't play this...
Read more >
What is “assert” in JavaScript? - Stack Overflow
The assert function then simply creates a list item, applies a class of either “pass” or “fail,” dependent upon whether your test returned...
Read more >
Why the Hell Would I Use Node.js? A Case-by-case Tutorial
Node.js can solve I/O scaling, but was not created to compute scaling problems. Learn why and when to use Node.js in this case-by-case...
Read more >
Math208 Discrete Mathematics - College of Arts & Sciences
11.7 Exercises. 109. 12 Special Functions. 111. 12.1 Floor and ceiling functions. 111. 12.2 Fractional part. 111. 12.3 Integral part. 112. 12.4 Power...
Read more >
Known issues — nRF Connect SDK 2.2.99 documentation
If a device-bound message is sent to the device while it is in the LTE Power Saving Mode (PSM), the TCP connection will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found