summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2017-07-03 15:47:39 +0200
committerRefael Ackermann <refack@gmail.com>2017-07-19 15:18:02 -0400
commit2a621d40517b8ec17d6b6b15e31bdc6e5d34e768 (patch)
tree465b86a0ca7efae1a9a7896dc46b5febbd72a660 /test
parentb55ab01b456645bcd7fce5c394f2722112080c6b (diff)
downloadnode-new-2a621d40517b8ec17d6b6b15e31bdc6e5d34e768.tar.gz
test: validate more properties in expectsError
PR-URL: https://github.com/nodejs/node/pull/14058 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Diffstat (limited to 'test')
-rw-r--r--test/common/README.md43
-rw-r--r--test/common/index.js43
-rw-r--r--test/parallel/test-internal-errors.js2
3 files changed, 60 insertions, 28 deletions
diff --git a/test/common/README.md b/test/common/README.md
index 44e9c4e8cd..32ca671157 100644
--- a/test/common/README.md
+++ b/test/common/README.md
@@ -51,28 +51,41 @@ Platform normalizes the `dd` command
Check if there is more than 1gb of total memory.
### expectsError([fn, ]settings[, exact])
-* `fn` [&lt;Function>]
+* `fn` [&lt;Function>] a function that should throw.
* `settings` [&lt;Object>]
- with the following optional properties:
+ that must contain the `code` property plus any of the other following
+ properties (some properties only apply for `AssertionError`):
* `code` [&lt;String>]
- expected error must have this value for its `code` property
+ expected error must have this value for its `code` property.
* `type` [&lt;Function>]
- expected error must be an instance of `type`
- * `message` [&lt;String>]
- or [&lt;RegExp>]
+ expected error must be an instance of `type` and must be an Error subclass.
+ * `message` [&lt;String>] or [&lt;RegExp>]
if a string is provided for `message`, expected error must have it for its
`message` property; if a regular expression is provided for `message`, the
- regular expression must match the `message` property of the expected error
+ regular expression must match the `message` property of the expected error.
+ * `name` [&lt;String>]
+ expected error must have this value for its `name` property.
+ * `generatedMessage` [&lt;String>]
+ (`AssertionError` only) expected error must have this value for its
+ `generatedMessage` property.
+ * `actual` &lt;any>
+ (`AssertionError` only) expected error must have this value for its
+ `actual` property.
+ * `expected` &lt;any>
+ (`AssertionError` only) expected error must have this value for its
+ `expected` property.
+ * `operator` &lt;any>
+ (`AssertionError` only) expected error must have this value for its
+ `operator` property.
* `exact` [&lt;Number>] default = 1
+* return [&lt;Function>]
-* return function suitable for use as a validation function passed as the second
- argument to e.g. `assert.throws()`. If the returned function has not been
- called exactly `exact` number of times when the test is complete, then the
- test will fail.
-
-If `fn` is provided, it will be passed to `assert.throws` as first argument.
-
-The expected error should be [subclassed by the `internal/errors` module](https://github.com/nodejs/node/blob/master/doc/guides/using-internal-errors.md#api).
+ If `fn` is provided, it will be passed to `assert.throws` as first argument
+ and `undefined` will be returned.
+ Otherwise a function suitable as callback or for use as a validation function
+ passed as the second argument to `assert.throws()` will be returned. If the
+ returned function has not been called exactly `exact` number of times when the
+ test is complete, then the test will fail.
### expectWarning(name, expected)
* `name` [&lt;String>]
diff --git a/test/common/index.js b/test/common/index.js
index 64127e4c33..63e94adae5 100644
--- a/test/common/index.js
+++ b/test/common/index.js
@@ -696,24 +696,43 @@ Object.defineProperty(exports, 'hasSmallICU', {
});
// Useful for testing expected internal/error objects
-exports.expectsError = function expectsError(fn, options, exact) {
+exports.expectsError = function expectsError(fn, settings, exact) {
if (typeof fn !== 'function') {
- exact = options;
- options = fn;
+ exact = settings;
+ settings = fn;
fn = undefined;
}
- const { code, type, message } = options;
const innerFn = exports.mustCall(function(error) {
- assert.strictEqual(error.code, code);
- if (type !== undefined) {
+ assert.strictEqual(error.code, settings.code);
+ if ('type' in settings) {
+ const type = settings.type;
+ if (type !== Error && !Error.isPrototypeOf(type)) {
+ throw new TypeError('`settings.type` must inherit from `Error`');
+ }
assert(error instanceof type,
- `${error} is not the expected type ${type}`);
+ `${error.name} is not instance of ${type.name}`);
+ }
+ if ('message' in settings) {
+ const message = settings.message;
+ if (typeof message === 'string') {
+ assert.strictEqual(error.message, message);
+ } else {
+ assert(message.test(error.message),
+ `${error.message} does not match ${message}`);
+ }
}
- if (message instanceof RegExp) {
- assert(message.test(error.message),
- `${error.message} does not match ${message}`);
- } else if (typeof message === 'string') {
- assert.strictEqual(error.message, message);
+ if ('name' in settings) {
+ assert.strictEqual(error.name, settings.name);
+ }
+ if (error.constructor.name === 'AssertionError') {
+ ['generatedMessage', 'actual', 'expected', 'operator'].forEach((key) => {
+ if (key in settings) {
+ const actual = error[key];
+ const expected = settings[key];
+ assert.strictEqual(actual, expected,
+ `${key}: expected ${expected}, not ${actual}`);
+ }
+ });
}
return true;
}, exact);
diff --git a/test/parallel/test-internal-errors.js b/test/parallel/test-internal-errors.js
index 4fb99d8fa3..a65e7c15f8 100644
--- a/test/parallel/test-internal-errors.js
+++ b/test/parallel/test-internal-errors.js
@@ -166,7 +166,7 @@ assert.throws(() => {
}, common.expectsError({ code: 'TEST_ERROR_1', type: RangeError }));
}, common.expectsError({
code: 'ERR_ASSERTION',
- message: /^.+ is not the expected type \S/
+ message: /^.+ is not instance of \S/
}));
assert.throws(() => {