-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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: accept regular expressions to validate #20485
Conversation
This makes sure regular expressions on validation objects validate against strings when used with `assert.throws` and `assert.rejects`.
doc/api/assert.md
Outdated
an object where each property will be tested for, or an instance of error where | ||
each property will be tested for including the non-enumerable `message` and | ||
`name` properties. | ||
an validation object where each property will be tested for strict deep |
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.
an validation -> a validation.
doc/api/assert.md
Outdated
an validation object where each property will be tested for strict deep | ||
equality, or an instance of error where each property will be tested for strict | ||
deep equality including the non-enumerable `message` and `name` properties. When | ||
using an validation object, it is also possible to use a regular expression, |
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.
an validation -> a validation.
I am a bit confused.
From this description, I would infer that a RegExp from validation object would be checked against a string value of the same property in the thrown error. But in the code and comments we have two RegExps: err.reg = /abc/i;
// ...
// This will strictly validate that the regular expression is identical to
// the regular expression on the error.
reg: /abc/i How does this relate? |
@vsemozhetbyt they are both checked. That is the key point. It is unlikely that regular expressions are going to exist on errors but if they do, they are still validated properly. |
Then maybe we need both examples in the code example? RegExp against RegExp and RegExp against the string, both pairs for the same name properties? |
@vsemozhetbyt that is exactly my test case. So I guess it is not obvious enough so far. I tried to make it clear, but I am going to iterate over it later on. |
doc/api/assert.md
Outdated
a validation object where each property will be tested for strict deep equality, | ||
or an instance of error where each property will be tested for strict deep | ||
equality including the non-enumerable `message` and `name` properties. When | ||
using a object, it is also possible to use a regular expression, when validating |
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.
an object)
Comments addressed. New CI https://ci.nodejs.org/job/node-test-pull-request/14699/ @nodejs/testing PTAL |
PTAL |
Landed in cf7be86 |
This makes sure regular expressions on validation objects validate against strings when used with `assert.throws` and `assert.rejects`. PR-URL: nodejs#20485 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
This makes sure regular expressions on validation objects validate against strings when used with `assert.throws` and `assert.rejects`. PR-URL: #20485 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
* addons: - Fixed a memory leak for users of `AsyncResource` and N-API. (Michael Dawson) #20668 * assert: - The `error` parameter of `assert.throws()` can be an object containing regular expressions now. (Ruben Bridgewater) #20485 * crypto: - The `authTagLength` option has been made more flexible (Tobias Nießen) #20235) #20039 * esm: - Builtin modules (e.g. `fs`) now provide named exports in ES6 modules. (Gus Caplan) #20403 * http: - Handling of `close` and `aborted` events has been made more consistent. (Robert Nagy) #20075 #20611 * module: - add --preserve-symlinks-main (David Goldstein) #19911 * timers: - `timeout.refresh()` has been added to the public API. (Jeremiah Senkpiel) #20298 * Embedder support: - Functions for creating V8 `Isolate` and `Context` objects with Node.js-specific behaviour have been added to the API. (Allen Yonghuang Wang) #20639 - Node.js `Environment`s clean up resources before exiting now. (Anna Henningsen) #19377 - Support for multi-threaded embedding has been improved. (Anna Henningsen) #20542 #20539 #20541 PR-URL: #20724
* addons: - Fixed a memory leak for users of `AsyncResource` and N-API. (Michael Dawson) #20668 * assert: - The `error` parameter of `assert.throws()` can be an object containing regular expressions now. (Ruben Bridgewater) #20485 * crypto: - The `authTagLength` option has been made more flexible (Tobias Nießen) #20235) #20039 * esm: - Builtin modules (e.g. `fs`) now provide named exports in ES6 modules. (Gus Caplan) #20403 * http: - Handling of `close` and `aborted` events has been made more consistent. (Robert Nagy) #20075 #20611 * module: - add --preserve-symlinks-main (David Goldstein) #19911 * timers: - `timeout.refresh()` has been added to the public API. (Jeremiah Senkpiel) #20298 * Embedder support: - Functions for creating V8 `Isolate` and `Context` objects with Node.js-specific behaviour have been added to the API. (Allen Yonghuang Wang) #20639 - Node.js `Environment`s clean up resources before exiting now. (Anna Henningsen) #19377 - Support for multi-threaded embedding has been improved. (Anna Henningsen) #20542 #20539 #20541 PR-URL: #20724
Can we get this in |
@BeyondEvil it should work identically using |
Thanks @BridgeAR , I did give it a try. Searching for the solution led me here. 😊 Maybe I'm doing something wrong, but here's what I tried: it('should fail if incorrect url', async () => {
const domain = 'sekhfskdfdjksfkjdhfsd.com'
const incorrectUrl = `https://${domain}`
await assert.rejects(
post(incorrectUrl),
new SemanticReleaseError(/.+ENOTFOUND.+/, 'SLACK CONNECTION FAILED')
)
}) result:
What am I doing wrong? |
You pass through a regular expression to your error constructor. That is first coerced to a string and then set as message. During the assertion the message is now checked against the errors message property which is a string and the comparison fails. I guess you want to do something like:
|
Ah, of course! Thank you, much appreciated! 🙏 |
This makes sure regular expressions on validation objects validate
against strings when used with
assert.throws
andassert.rejects
.Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes