summaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2018-05-02 19:26:32 +0200
committerRuben Bridgewater <ruben@bridgewater.de>2018-05-10 13:17:22 +0200
commitcf7be86cd927c99547f4774d79ea6d12d621033f (patch)
treefa2117ec46b92ea7e852898f340dec76131c1128 /doc
parent81b99da96e9a27fc7e55e002656968d99596a31d (diff)
downloadandroid-node-v8-cf7be86cd927c99547f4774d79ea6d12d621033f.tar.gz
android-node-v8-cf7be86cd927c99547f4774d79ea6d12d621033f.tar.bz2
android-node-v8-cf7be86cd927c99547f4774d79ea6d12d621033f.zip
assert: accept regular expressions to validate
This makes sure regular expressions on validation objects validate against strings when used with `assert.throws` and `assert.rejects`. PR-URL: https://github.com/nodejs/node/pull/20485 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Diffstat (limited to 'doc')
-rw-r--r--doc/api/assert.md50
1 files changed, 44 insertions, 6 deletions
diff --git a/doc/api/assert.md b/doc/api/assert.md
index 43e5800ff0..7f98e975f1 100644
--- a/doc/api/assert.md
+++ b/doc/api/assert.md
@@ -1070,18 +1070,26 @@ changes:
Expects the function `block` to throw an error.
If specified, `error` can be a [`Class`][], [`RegExp`][], a validation function,
-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.
+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 an object, it is also possible to use a regular expression, when
+validating against a string property. See below for examples.
If specified, `message` will be the message provided by the `AssertionError` if
the block fails to throw.
-Custom error object / error instance:
+Custom validation object / error instance:
```js
const err = new TypeError('Wrong value');
err.code = 404;
+err.foo = 'bar';
+err.info = {
+ nested: true,
+ baz: 'text'
+};
+err.reg = /abc/i;
assert.throws(
() => {
@@ -1089,8 +1097,38 @@ assert.throws(
},
{
name: 'TypeError',
- message: 'Wrong value'
- // Note that only properties on the error object will be tested!
+ message: 'Wrong value',
+ info: {
+ nested: true,
+ baz: 'text'
+ }
+ // Note that only properties on the validation object will be tested for.
+ // Using nested objects requires all properties to be present. Otherwise
+ // the validation is going to fail.
+ }
+);
+
+// Using regular expressions to validate error properties:
+assert.throws(
+ () => {
+ throw err;
+ },
+ {
+ // The `name` and `message` properties are strings and using regular
+ // expressions on those will match against the string. If they fail, an
+ // error is thrown.
+ name: /^TypeError$/,
+ message: /Wrong/,
+ foo: 'bar',
+ info: {
+ nested: true,
+ // It is not possible to use regular expressions for nested properties!
+ baz: 'text'
+ },
+ // The `reg` property contains a regular expression and only if the
+ // validation object contains an identical regular expression, it is going
+ // to pass.
+ reg: /abc/i
}
);