Skip to content
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

tools: eslint rule for assert.throws -> common.expectsError #17557

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions test/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ rules:
# Custom rules in tools/eslint-rules
prefer-assert-iferror: error
prefer-assert-methods: error
prefer-common-expectserror: error
prefer-common-mustnotcall: error
crypto-check: error
inspector-check: error
Expand Down
6 changes: 3 additions & 3 deletions test/async-hooks/test-embedder.api.async-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ common.expectsError(
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
});
assert.throws(() => {
common.expectsError(() => {
new AsyncResource('invalid_trigger_id', { triggerAsyncId: null });
}, common.expectsError({
}, {
code: 'ERR_INVALID_ASYNC_ID',
type: RangeError,
}));
});

assert.strictEqual(
new AsyncResource('default_trigger_id').triggerAsyncId(),
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-assert-fail.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

/* eslint-disable prefer-common-expectserror */

const common = require('../common');
const assert = require('assert');

Expand Down
3 changes: 3 additions & 0 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';

/* eslint-disable prefer-common-expectserror */

const common = require('../common');
const assert = require('assert');
const a = assert;
Expand Down
5 changes: 2 additions & 3 deletions test/parallel/test-buffer-alloc.js
Original file line number Diff line number Diff line change
Expand Up @@ -974,12 +974,11 @@ assert.strictEqual(SlowBuffer.prototype.offset, undefined);
}

// ParseArrayIndex() should reject values that don't fit in a 32 bits size_t.
assert.throws(() => {
common.expectsError(() => {
const a = Buffer.alloc(1);
const b = Buffer.alloc(1);
a.copy(b, 0, 0x100000000, 0x100000001);
}, common.expectsError(
{ code: undefined, type: RangeError, message: 'Index out of range' }));
}, { code: undefined, type: RangeError, message: 'Index out of range' });

// Unpooled buffer (replaces SlowBuffer)
{
Expand Down
10 changes: 4 additions & 6 deletions test/parallel/test-buffer-fill.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,21 +424,19 @@ common.expectsError(() => {

// Testing process.binding. Make sure "end" is properly checked for -1 wrap
// around.
assert.throws(() => {
common.expectsError(() => {
process.binding('buffer').fill(Buffer.alloc(1), 1, 1, -2, 1);
}, common.expectsError(
{ code: undefined, type: RangeError, message: 'Index out of range' }));
}, { code: undefined, type: RangeError, message: 'Index out of range' });

// Test that bypassing 'length' won't cause an abort.
assert.throws(() => {
common.expectsError(() => {
const buf = new Buffer('w00t');
Object.defineProperty(buf, 'length', {
value: 1337,
enumerable: true
});
buf.fill('');
}, common.expectsError(
{ code: undefined, type: RangeError, message: 'Index out of range' }));
}, { code: undefined, type: RangeError, message: 'Index out of range' });

assert.deepStrictEqual(
Buffer.allocUnsafeSlow(16).fill('ab', 'utf16le'),
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-console-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ common.expectsError(
);

// Console constructor should throw if stderr exists but is not writable
assert.throws(
common.expectsError(
() => {
out.write = () => {};
err.write = undefined;
new Console(out, err);
},
common.expectsError({
{
code: 'ERR_CONSOLE_WRITABLE_STREAM',
type: TypeError,
message: /stderr/
})
}
);

out.write = err.write = (d) => {};
Expand Down
16 changes: 8 additions & 8 deletions test/parallel/test-dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,20 +205,20 @@ assert.doesNotThrow(() => {
}, common.mustCall());
});

assert.throws(() => dns.lookupService('0.0.0.0'), common.expectsError({
common.expectsError(() => dns.lookupService('0.0.0.0'), {
code: 'ERR_MISSING_ARGS',
type: TypeError,
message: 'The "host", "port", and "callback" arguments must be specified'
}));
});

const invalidHost = 'fasdfdsaf';
assert.throws(() => {
common.expectsError(() => {
dns.lookupService(invalidHost, 0, common.mustNotCall());
}, common.expectsError({
}, {
code: 'ERR_INVALID_OPT_VALUE',
type: TypeError,
message: `The value "${invalidHost}" is invalid for option "host"`
}));
});

const portErr = (port) => {
common.expectsError(
Expand All @@ -238,9 +238,9 @@ portErr(undefined);
portErr(65538);
portErr('test');

assert.throws(() => {
common.expectsError(() => {
dns.lookupService('0.0.0.0', 80, null);
}, common.expectsError({
}, {
code: 'ERR_INVALID_CALLBACK',
type: TypeError
}));
});
27 changes: 27 additions & 0 deletions test/parallel/test-eslint-prefer-common-expectserror.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

require('../common');

const RuleTester = require('../../tools/eslint').RuleTester;
const rule = require('../../tools/eslint-rules/prefer-common-expectserror');

const message = 'Please use common.expectsError(fn, err) instead of ' +
'assert.throws(fn, common.expectsError(err)).';

new RuleTester().run('prefer-common-expectserror', rule, {
valid: [
'assert.throws(fn, /[a-z]/)',
'assert.throws(function () {}, function() {})',
'common.expectsError(function() {}, err)'
],
invalid: [
{
code: 'assert.throws(function() {}, common.expectsError(err))',
errors: [{ message }]
},
{
code: 'assert.throws(fn, common.expectsError(err))',
errors: [{ message }]
}
]
});
60 changes: 30 additions & 30 deletions test/parallel/test-http-outgoing-proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,80 +21,80 @@ assert.strictEqual(
typeof ServerResponse.prototype._implicitHeader, 'function');

// validateHeader
assert.throws(() => {
common.expectsError(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.setHeader();
}, common.expectsError({
}, {
code: 'ERR_INVALID_HTTP_TOKEN',
type: TypeError,
message: 'Header name must be a valid HTTP token ["undefined"]'
}));
});

assert.throws(() => {
common.expectsError(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.setHeader('test');
}, common.expectsError({
}, {
code: 'ERR_HTTP_INVALID_HEADER_VALUE',
type: TypeError,
message: 'Invalid value "undefined" for header "test"'
}));
});

assert.throws(() => {
common.expectsError(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.setHeader(404);
}, common.expectsError({
}, {
code: 'ERR_INVALID_HTTP_TOKEN',
type: TypeError,
message: 'Header name must be a valid HTTP token ["404"]'
}));
});

assert.throws(() => {
common.expectsError(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.setHeader.call({ _header: 'test' }, 'test', 'value');
}, common.expectsError({
}, {
code: 'ERR_HTTP_HEADERS_SENT',
type: Error,
message: 'Cannot set headers after they are sent to the client'
}));
});

assert.throws(() => {
common.expectsError(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.setHeader('200', 'あ');
}, common.expectsError({
}, {
code: 'ERR_INVALID_CHAR',
type: TypeError,
message: 'Invalid character in header content ["200"]'
}));
});

// write
assert.throws(() => {
common.expectsError(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.write();
}, common.expectsError({
}, {
code: 'ERR_METHOD_NOT_IMPLEMENTED',
type: Error,
message: 'The _implicitHeader() method is not implemented'
}));
});

assert(OutgoingMessage.prototype.write.call({ _header: 'test' }));

assert.throws(() => {
common.expectsError(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.write.call({ _header: 'test', _hasBody: 'test' });
}, common.expectsError({
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The first argument must be one of type string or Buffer'
}));
});

assert.throws(() => {
common.expectsError(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.write.call({ _header: 'test', _hasBody: 'test' }, 1);
}, common.expectsError({
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The first argument must be one of type string or Buffer'
}));
});

// addTrailers()
// The `Error` comes from the JavaScript engine so confirm that it is a
Expand All @@ -105,20 +105,20 @@ assert.throws(() => {
outgoingMessage.addTrailers();
}, TypeError);

assert.throws(() => {
common.expectsError(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.addTrailers({ 'あ': 'value' });
}, common.expectsError({
}, {
code: 'ERR_INVALID_HTTP_TOKEN',
type: TypeError,
message: 'Trailer name must be a valid HTTP token ["あ"]'
}));
});

assert.throws(() => {
common.expectsError(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.addTrailers({ 404: 'あ' });
}, common.expectsError({
}, {
code: 'ERR_INVALID_CHAR',
type: TypeError,
message: 'Invalid character in trailer content ["404"]'
}));
});
7 changes: 3 additions & 4 deletions test/parallel/test-http2-client-request-options-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const http2 = require('http2');

// Check if correct errors are emitted when wrong type of data is passed
Expand Down Expand Up @@ -40,19 +39,19 @@ server.listen(0, common.mustCall(() => {
return;
}

assert.throws(
common.expectsError(
() => client.request({
':method': 'CONNECT',
':authority': `localhost:${port}`
}, {
[option]: types[type]
}),
common.expectsError({
{
type: TypeError,
code: 'ERR_INVALID_OPT_VALUE',
message: `The value "${String(types[type])}" is invalid ` +
`for option "${option}"`
})
}
);
});
});
Expand Down
18 changes: 9 additions & 9 deletions test/parallel/test-http2-connect-method.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,36 +55,36 @@ server.listen(0, common.mustCall(() => {
const client = http2.connect(`http://localhost:${proxy.address().port}`);

// confirm that :authority is required and :scheme & :path are forbidden
assert.throws(
common.expectsError(
() => client.request({
[HTTP2_HEADER_METHOD]: 'CONNECT'
}),
common.expectsError({
{
code: 'ERR_HTTP2_CONNECT_AUTHORITY',
message: ':authority header is required for CONNECT requests'
})
}
);
assert.throws(
common.expectsError(
() => client.request({
[HTTP2_HEADER_METHOD]: 'CONNECT',
[HTTP2_HEADER_AUTHORITY]: `localhost:${port}`,
[HTTP2_HEADER_SCHEME]: 'http'
}),
common.expectsError({
{
code: 'ERR_HTTP2_CONNECT_SCHEME',
message: 'The :scheme header is forbidden for CONNECT requests'
})
}
);
assert.throws(
common.expectsError(
() => client.request({
[HTTP2_HEADER_METHOD]: 'CONNECT',
[HTTP2_HEADER_AUTHORITY]: `localhost:${port}`,
[HTTP2_HEADER_PATH]: '/'
}),
common.expectsError({
{
code: 'ERR_HTTP2_CONNECT_PATH',
message: 'The :path header is forbidden for CONNECT requests'
})
}
);

// valid CONNECT request
Expand Down
Loading