Skip to content

Commit 53b0f41

Browse files
edsadrMylesBorins
authored andcommitted
test: improve the code in test-process-cpuUsage
* validate the errors for assert.throws * use arrow functions PR-URL: #10714 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 725a896 commit 53b0f41

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

test/parallel/test-process-cpuUsage.js

+42-12
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,57 @@ for (let i = 0; i < 10; i++) {
3333
assert(diffUsage.system >= 0);
3434
}
3535

36+
const invalidUserArgument =
37+
/^TypeError: value of user property of argument is invalid$/;
38+
const invalidSystemArgument =
39+
/^TypeError: value of system property of argument is invalid$/;
40+
3641
// Ensure that an invalid shape for the previous value argument throws an error.
37-
assert.throws(function() { process.cpuUsage(1); });
38-
assert.throws(function() { process.cpuUsage({}); });
39-
assert.throws(function() { process.cpuUsage({ user: 'a' }); });
40-
assert.throws(function() { process.cpuUsage({ system: 'b' }); });
41-
assert.throws(function() { process.cpuUsage({ user: null, system: 'c' }); });
42-
assert.throws(function() { process.cpuUsage({ user: 'd', system: null }); });
43-
assert.throws(function() { process.cpuUsage({ user: -1, system: 2 }); });
44-
assert.throws(function() { process.cpuUsage({ user: 3, system: -2 }); });
45-
assert.throws(function() {
42+
assert.throws(() => {
43+
process.cpuUsage(1);
44+
}, invalidUserArgument);
45+
46+
assert.throws(() => {
47+
process.cpuUsage({});
48+
}, invalidUserArgument);
49+
50+
assert.throws(() => {
51+
process.cpuUsage({ user: 'a' });
52+
}, invalidUserArgument);
53+
54+
assert.throws(() => {
55+
process.cpuUsage({ system: 'b' });
56+
}, invalidUserArgument);
57+
58+
assert.throws(() => {
59+
process.cpuUsage({ user: null, system: 'c' });
60+
}, invalidUserArgument);
61+
62+
assert.throws(() => {
63+
process.cpuUsage({ user: 'd', system: null });
64+
}, invalidUserArgument);
65+
66+
assert.throws(() => {
67+
process.cpuUsage({ user: -1, system: 2 });
68+
}, invalidUserArgument);
69+
70+
assert.throws(() => {
71+
process.cpuUsage({ user: 3, system: -2 });
72+
}, invalidSystemArgument);
73+
74+
assert.throws(() => {
4675
process.cpuUsage({
4776
user: Number.POSITIVE_INFINITY,
4877
system: 4
4978
});
50-
});
51-
assert.throws(function() {
79+
}, invalidUserArgument);
80+
81+
assert.throws(() => {
5282
process.cpuUsage({
5383
user: 5,
5484
system: Number.NEGATIVE_INFINITY
5585
});
56-
});
86+
}, invalidSystemArgument);
5787

5888
// Ensure that the return value is the expected shape.
5989
function validateResult(result) {

0 commit comments

Comments
 (0)