Skip to content

incorrect/unexpected behavior in assert module #9373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ORESoftware opened this issue Oct 31, 2016 · 2 comments
Closed

incorrect/unexpected behavior in assert module #9373

ORESoftware opened this issue Oct 31, 2016 · 2 comments
Labels
assert Issues and PRs related to the assert subsystem. question Issues that look for answers.

Comments

@ORESoftware
Copy link
Contributor

ORESoftware commented Oct 31, 2016

On MacOS 64bit, Node.js version 6.7.0

using the core "assert" module, like so:

  assert.throws(function () {
            const [a, b, c, d] = foo('oh yes');
        }, 
        /Argument is required at argument index = 1, but type was wrong/,
        'did not throw with expected message');

the above is fine, no error is thrown because the block does throw an error with a message that matches the regex

however, if I do this, I get something unexpected (I changed the regex):

 assert.throws(function () {                                                //block arg
            const [a, b, c, d] = foo('oh yes');   
        },
        /Argument is required at argument index = 2, but type was wrong/,  //error arg
        'did not throw with expected message');                            //message arg

it will give me this:

Error: Argument is required at argument index = 1, but type was wrong 
     => expected => "object"
     => actual => "undefined"
        at Object.parse (/Users/t_millal/WebstormProjects/oresoftware/pragmatik/index.js:250:19)
        at foo (/Users/t_millal/WebstormProjects/oresoftware/pragmatik/test/test-src/parse-to-array/three.test.js:31:26)
        at /Users/t_millal/WebstormProjects/oresoftware/pragmatik/test/test-src/parse-to-array/three.test.js:46:34

this is my own library that I am testing, so I am familiar with what is happening. Seems clear that the above error message should at least contain: " 'did not throw with expected message' ". The expected assert module behavior will help me in this regard, but it's not behaving.

Is this a known problem? What am I missing?

@cjihrig
Copy link
Contributor

cjihrig commented Oct 31, 2016

According to the docs, the third argument is only used if no error is thrown. I think in your case, an error is thrown, but with the wrong message.

@Fishrock123 Fishrock123 added question Issues that look for answers. assert Issues and PRs related to the assert subsystem. labels Oct 31, 2016
@Trott
Copy link
Member

Trott commented Nov 6, 2016

Implementing testing-specific assertions like assert.throws() is regarded by many in core to have been a mistake. We're stuck with it because so much of the ecosystem depends on it. But there is mild language in the docs discouraging people from using assert as a general purpose testing library. You might consider using something like chai instead. Their assert API is like the internal module, but likely contains fewer surprises like this. Good luck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assert Issues and PRs related to the assert subsystem. question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

4 participants