-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
assert: add includes
api
#44029
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
assert: add includes
api
#44029
Conversation
My concern with adding APIs like this is that it is a slippery slope to a more confusing API. Right now this is a wrapper around |
I agree it isn't very worthwhile to add a (very specific wrapper) function like this. |
i think your concern about confusion is valid, but as the desire is for an api that delivers a useful |
I think most users would expect the negation
It's also a slippery slope to bloating the
Unfortunately, adding types to such an API later can be considered a breaking change (see #41007 (comment), for example). In short: if this API is defined to reject non-
|
if we end up making i don't mind adding a negation.
agreed, it would need to be done now, as part of this pr. |
assert.includes('I will fail', 'pass'); | ||
// AssertionError [ERR_ASSERTION]: The substring was not located inside the target string. | ||
|
||
assert.includes(123, 'pass'); | ||
// AssertionError [ERR_ASSERTION]: The "string" argument must be of type string. | ||
|
||
assert.includes('I will fail', /pass/); | ||
// AssertionError [ERR_ASSERTION]: The "substring" argument must be of type string. | ||
|
||
assert.includes('I will pass', 'pass'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, it would be nice to add a valid case with for regexp, like:
assert.includes(/\bpass\b/, 'It will pass');
@zackschuster WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's just the assert.match
api, but in reverse. i think that would be far more confusing than helpful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay, but for me, the word "includes" suitable to regexp matches. Well, with that point of view, it probably better to rename includes
into strictIncludes
, similar to toEqual and toStrictEqual
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"strict equality" refers to a real JS thing (https://tc39.es/ecma262/#sec-isstrictlyequal), "strict includes" does not. I don't think we should go with strictIncludes
.
the word "includes" suitable to regexp matches
Arguably match
is even more suitable.
That's the big issue, there is no built-in way to escape regexes. If there was, there would be no need for this specific API (nor |
That is not a Node.js issue (e.g., here is a discussion from 12 years ago), and a TC39 proposal exists to address it. |
I think it would be a nice to have, especially since the TC39 proposal seems stalled, but I can see the argument why we wouldn't add it to Node.js to keep our API simpler. Maybe @nodejs/test_runner has some thoughts? |
This issue/PR was marked as stalled, it will be automatically closed in 30 days. If it should remain open, please leave a comment explaining why it should remain open. |
@aduh95 i have the proposal on the agenda for next week’s plenary; it’s not stalled. |
very simple proxy to
StringPrototypeIncludes
inspired by #43784 (comment)