Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d6fece1

Browse files
BridgeARrefack
authored andcommittedJul 8, 2017
test: add optional throw fn to expectsError
PR-URL: #14089 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent 44483b6 commit d6fece1

File tree

4 files changed

+63
-55
lines changed

4 files changed

+63
-55
lines changed
 

‎test/common/README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ Platform normalizes the `dd` command
5050

5151
Check if there is more than 1gb of total memory.
5252

53-
### expectsError(settings)
53+
### expectsError([fn, ]settings)
54+
* `fn` [&lt;Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function)
5455
* `settings` [&lt;Object>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
5556
with the following optional properties:
5657
* `code` [&lt;String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)
@@ -66,6 +67,8 @@ Check if there is more than 1gb of total memory.
6667
* return function suitable for use as a validation function passed as the second
6768
argument to `assert.throws()`
6869

70+
If `fn` is provided, it will be passed to `assert.throws` as first argument.
71+
6972
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).
7073

7174
### expectWarning(name, expected)

‎test/common/index.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -702,8 +702,13 @@ Object.defineProperty(exports, 'hasSmallICU', {
702702
});
703703

704704
// Useful for testing expected internal/error objects
705-
exports.expectsError = function expectsError({code, type, message}) {
706-
return function(error) {
705+
exports.expectsError = function expectsError(fn, options) {
706+
if (typeof fn !== 'function') {
707+
options = fn;
708+
fn = undefined;
709+
}
710+
const { code, type, message } = options;
711+
function innerFn(error) {
707712
assert.strictEqual(error.code, code);
708713
if (type !== undefined) {
709714
assert(error instanceof type,
@@ -716,7 +721,12 @@ exports.expectsError = function expectsError({code, type, message}) {
716721
assert.strictEqual(error.message, message);
717722
}
718723
return true;
719-
};
724+
}
725+
if (fn) {
726+
assert.throws(fn, innerFn);
727+
return;
728+
}
729+
return innerFn;
720730
};
721731

722732
exports.skipIfInspectorDisabled = function skipIfInspectorDisabled() {

‎test/parallel/test-assert-deep.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ for (const a of similar) {
114114
}
115115
}
116116

117-
assert.throws(
118-
() => { assert.deepEqual(new Set([{a: 0}]), new Set([{a: 1}])); },
119-
common.expectsError({
120-
code: 'ERR_ASSERTION',
121-
message: /^Set { { a: 0 } } deepEqual Set { { a: 1 } }$/
122-
}));
117+
common.expectsError(() => {
118+
assert.deepEqual(new Set([{a: 0}]), new Set([{a: 1}]));
119+
}, {
120+
code: 'ERR_ASSERTION',
121+
message: /^Set { { a: 0 } } deepEqual Set { { a: 1 } }$/
122+
});
123123

124124
function assertDeepAndStrictEqual(a, b) {
125125
assert.deepEqual(a, b);

‎test/parallel/test-assert-fail.js

+40-45
Original file line numberDiff line numberDiff line change
@@ -17,57 +17,52 @@ assert.throws(
1717
);
1818

1919
// One arg = message
20-
assert.throws(
21-
() => { assert.fail('custom message'); },
22-
common.expectsError({
23-
code: 'ERR_ASSERTION',
24-
type: assert.AssertionError,
25-
message: 'custom message',
26-
operator: undefined,
27-
actual: undefined,
28-
expected: undefined
29-
})
30-
);
20+
common.expectsError(() => {
21+
assert.fail('custom message');
22+
}, {
23+
code: 'ERR_ASSERTION',
24+
type: assert.AssertionError,
25+
message: 'custom message',
26+
operator: undefined,
27+
actual: undefined,
28+
expected: undefined
29+
});
3130

3231
// Two args only, operator defaults to '!='
33-
assert.throws(
34-
() => { assert.fail('first', 'second'); },
35-
common.expectsError({
36-
code: 'ERR_ASSERTION',
37-
type: assert.AssertionError,
38-
message: '\'first\' != \'second\'',
39-
operator: '!=',
40-
actual: 'first',
41-
expected: 'second'
42-
43-
})
44-
);
32+
common.expectsError(() => {
33+
assert.fail('first', 'second');
34+
}, {
35+
code: 'ERR_ASSERTION',
36+
type: assert.AssertionError,
37+
message: '\'first\' != \'second\'',
38+
operator: '!=',
39+
actual: 'first',
40+
expected: 'second'
41+
});
4542

4643
// Three args
47-
assert.throws(
48-
() => { assert.fail('ignored', 'ignored', 'another custom message'); },
49-
common.expectsError({
50-
code: 'ERR_ASSERTION',
51-
type: assert.AssertionError,
52-
message: 'another custom message',
53-
operator: undefined,
54-
actual: 'ignored',
55-
expected: 'ignored'
56-
})
57-
);
44+
common.expectsError(() => {
45+
assert.fail('ignored', 'ignored', 'another custom message');
46+
}, {
47+
code: 'ERR_ASSERTION',
48+
type: assert.AssertionError,
49+
message: 'another custom message',
50+
operator: undefined,
51+
actual: 'ignored',
52+
expected: 'ignored'
53+
});
5854

5955
// No third arg (but a fourth arg)
60-
assert.throws(
61-
() => { assert.fail('first', 'second', undefined, 'operator'); },
62-
common.expectsError({
63-
code: 'ERR_ASSERTION',
64-
type: assert.AssertionError,
65-
message: '\'first\' operator \'second\'',
66-
operator: 'operator',
67-
actual: 'first',
68-
expected: 'second'
69-
})
70-
);
56+
common.expectsError(() => {
57+
assert.fail('first', 'second', undefined, 'operator');
58+
}, {
59+
code: 'ERR_ASSERTION',
60+
type: assert.AssertionError,
61+
message: '\'first\' operator \'second\'',
62+
operator: 'operator',
63+
actual: 'first',
64+
expected: 'second'
65+
});
7166

7267
// The stackFrameFunction should exclude the foo frame
7368
assert.throws(

0 commit comments

Comments
 (0)
Please sign in to comment.