-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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.match should return match result #33869
Comments
Maybe this needs to be discussed, the current implementation is throw if no match. Let me propose the "boolean implementation" so we can start the discussion. |
I think this can be merged into #33738 if we make it a bit more generic. |
@lundibundi could you please extend the idea of "generic"? |
The `assert.match` method by default will throws if there is no match, this change can add the function of instead of throwing or not, returns the boolean result of match. Fixes: nodejs#33869
Sorry, I forgot to answer here. My idea was to make all(most) of the methods in |
@awwright thanks for the suggestion. It is mostly untypical that an assertion returns something. I would like to keep the functionality to stick to the principle of least astonishment. @lundibundi I am not certain if your suggestion would resolve the requested feature. I am going to close this issue, since there was no further comment for a long time. If someone disagrees, please reopen. |
I'm not sure the principle of least astonishment applies, since the proposal is "we should have this feature", not "what is the least astonishing way to implement this feature." If you're suggesting that my tests should not be astonishing, I think that consideration is outside the scope of Node.js, and without this feature, failed tests are less comprehensible. |
I think what @BridgeAR was saying was that an assertion returning nothing is the expectation of an assertion and anything else would be surprising. Principle of least astonishment therefore indicates that assertions should not have a return value. Even if I'm wrong about what @BridgeAR meant, that's certainly how I see it. Conceptually, assertions should not have side effects. In compiled languages, the compilers complain about assertions that have side effects. We don't have that luxury/annoyance in JavaScript, but I do think that conceptually, assertions shouldn't affect the surrounding code. Outside of tests, you should be able to remove assertions and your code should be fine. That's my opinion, but I'm certainly willing to be persuaded by a convincing use case. |
Nobody is writing for Node.js with the expectation that the assert library always returns Also, Node.js has plenty of functions that have return values where I would not expect them. For example,
I am not proposing that assertions have side effects. A return value is not a side effect. If instead I had to write: assert(m = haystack.match(needle)); This would be a side effect, which is what I'm trying to avoid with this proposal. |
I would think the code would be this: m = haystack.match(needle);
assert(m); I'm not sure the proposal here improves on that: m = assert.match(haystack, needle); As @BridgeAR notes, it seems (to me at least) atypical and therefore surprising that an assertion would return anything. Maybe I need to rethink that, but at first blush, it seems surprising. It would enable patterns I wouldn't want to see like I suppose it's an easy enough thing to implement if people think it's a good idea. /ping @nodejs/assert Anyone else have additional thoughts? |
I agree that it's not of much value, and I agree that "assertions throw or else don't return anything" is the pretty universal expectation, and it's best not to violate that. I'm not sure why you can't do |
@ljharb You make a good point—I think I ultimately did something just like that, inside a short utility function. I'll also concede, this is a very small suggestion and I wouldn't mind seeing more people than just me interested in using this. |
Is your feature request related to a problem? Please describe.
I was using
assert(haystack.match(/needle/));
in my unit tests, and I found thatassert.match(haystack, /needle/);
produced much cleaner errors. +1, suggest to move to stable.However, in a small handful of cases, I was also using the match results later on in the test, like so:
Describe the solution you'd like
If I could rescue the match results from
assert.match
, and if theregexp
argument could be a string, this code could be simplified:Describe alternatives you've considered
This feature simply leads to cleaner tests.
Possible downsides are that people may overlook assert calls used like this, because they always expect "assert" to work like a C assertion, or having a return value is too inconsistent with the rest of the library.
The text was updated successfully, but these errors were encountered: