Skip to content
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

Add string support to throws #125

Closed
wants to merge 2 commits into from
Closed

Add string support to throws #125

wants to merge 2 commits into from

Conversation

SamVerschueren
Copy link
Contributor

I added the possibility to pass in the error message as string to the throws method.

Example

test('test it', t => {
    t.throws(throws, 'foo bar');
    t.end();

    function throws() {
        throw new Error('foo bar');
    }
});

Benefit

If you want to test the returned error message you either should use a regex or a validation function. The validation function spoils the test file in my opinion, and the regex is, well yeah, it's a regex. People have to escape some characters and a string just looks cleaner.

Downside

Not sure if it is a downside, but it's not standard assert stuff which might confuse people. Although, I don't really see it as downside, it's more an improvement.

// @sindresorhus @vdemedes @kevva @Qix-

@Qix-
Copy link
Contributor

Qix- commented Nov 4, 2015

should does this and to be honest it's the only way I could see reliably testing for expected exceptions. Expecting an exception but not specifying which one introduced a few regressions before I realized I needed to specify the message.

@sindresorhus
Copy link
Member

Not sure if it is a downside, but it's not standard assert stuff which might confuse people.

It's not. We only use the core assert so not to duplicate code. If anything can be done better, we'll do it. Also the core Node.js people have said assert is done and won't change anymore.

👍 from me.

@Qix-
Copy link
Contributor

Qix- commented Nov 4, 2015

Also the core Node.js people have said assert is done and won't change anymore.

One of my problems with the Node team. /offtopic

@SamVerschueren
Copy link
Contributor Author

Things can always be done better!

@sindresorhus
Copy link
Member

@Qix- In their defense it was a mistake to expose it as it was only really meant to be used to test core, from what I've read.

Also why ideally almost everything should be in user-land so it can be individually versioned and evolved.

@Qix-
Copy link
Contributor

Qix- commented Nov 4, 2015

That makes sense.

}, msg);
} else {
assert.throws(fn, err, msg);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (typeof err === 'string') {
  err = function (e) {
    return e.message === err;
  };
}

assert.throws(fn, err, msg);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely cleaner, but then I guess it should be like this.

if (typeof err === 'string') {
  var e = err;
  err = function (err) {
    return err.message === e;
  };
}

assert.throws(fn, err, msg);

In your example, at the time the function is executed, err is not the string anymore but it is the function. So it will check if err.message is equal to the function instead of to the original string.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup

@sindresorhus
Copy link
Member

👍 Thanks Sam :)

sindresorhus pushed a commit that referenced this pull request Nov 5, 2015
@SamVerschueren
Copy link
Contributor Author

No problem, always happy to contribute ;).

Any idea when these latest changes (string support in throws, promise fix in throws) will be released? They are needed in pageres and gh-user :). (and many others off course)

@sindresorhus
Copy link
Member

@SamVerschueren Hopefully today. Just want to get in the two open PRs. Any help reviewing #120 welcome ;)

@SamVerschueren
Copy link
Contributor Author

That's not an easy one :). Will have a look at it later today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants