Skip to content

Commit 0267d27

Browse files
Trottruyadorno
authored andcommitted
test: use Object.hasOwn() where applicable
Replace Object.prototpye.hasOwnProperty() with Object.hasOwn() where applicable. PR-URL: #41664 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Mestery <mestery@protonmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Tierney Cyren <hello@bnb.im> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent cca9210 commit 0267d27

21 files changed

+40
-40
lines changed

test/common/report.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function _validateContent(report, fields = []) {
6969

7070
checkForUnknownFields(report, sections);
7171
sections.forEach((section) => {
72-
assert(report.hasOwnProperty(section));
72+
assert(Object.hasOwn(report, section));
7373
assert(typeof report[section] === 'object' && report[section] !== null);
7474
});
7575

test/doctool/test-doctool-versions.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ for (const version of versions) {
5151
assert.strictEqual(parts[parts.length - 1], 'x',
5252
`'num' from ${tested} doesn't end in '.x'.`);
5353
const isEvenRelease = Number.parseInt(parts[expectedLength - 2]) % 2 === 0;
54-
const hasLtsProperty = version.hasOwnProperty('lts');
54+
const hasLtsProperty = Object.hasOwn(version, 'lts');
5555
if (hasLtsProperty) {
5656
// Odd-numbered versions of Node.js are never LTS.
5757
assert.ok(isEvenRelease, `${tested} should not be an 'lts' release.`);

test/parallel/test-child-process-constructor.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ child.spawn({
7878
stdio: 'pipe'
7979
});
8080

81-
assert.strictEqual(child.hasOwnProperty('pid'), true);
81+
assert.strictEqual(Object.hasOwn(child, 'pid'), true);
8282
assert(Number.isInteger(child.pid));
8383

8484
// Try killing with invalid signal

test/parallel/test-child-process-validate-stdio.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ assert.throws(() => getValidStdio(600), expectedError);
1818
const stdio1 = [];
1919
const result = getValidStdio(stdio1, false);
2020
assert.strictEqual(stdio1.length, 3);
21-
assert.strictEqual(result.hasOwnProperty('stdio'), true);
22-
assert.strictEqual(result.hasOwnProperty('ipc'), true);
23-
assert.strictEqual(result.hasOwnProperty('ipcFd'), true);
21+
assert.strictEqual(Object.hasOwn(result, 'stdio'), true);
22+
assert.strictEqual(Object.hasOwn(result, 'ipc'), true);
23+
assert.strictEqual(Object.hasOwn(result, 'ipcFd'), true);
2424
}
2525

2626
// Should throw if stdio has ipc and sync is true

test/parallel/test-cluster-basic.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ if (cluster.isWorker) {
131131
assert.strictEqual(Object.keys(arguments[0]).length, 4);
132132
assert.strictEqual(arguments[0].address, '127.0.0.1');
133133
assert.strictEqual(arguments[0].addressType, 4);
134-
assert(arguments[0].hasOwnProperty('fd'));
134+
assert(Object.hasOwn(arguments[0], 'fd'));
135135
assert.strictEqual(arguments[0].fd, undefined);
136136
const port = arguments[0].port;
137137
assert(Number.isInteger(port));

test/parallel/test-cluster-dgram-1.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function primary() {
7272
// Set up event handlers for every worker. Each worker sends a message when
7373
// it has received the expected number of packets. After that it disconnects.
7474
for (const key in cluster.workers) {
75-
if (cluster.workers.hasOwnProperty(key))
75+
if (Object.hasOwn(cluster.workers, key))
7676
setupWorker(cluster.workers[key]);
7777
}
7878

test/parallel/test-cluster-dgram-bind-fd.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function primary() {
6363
// Set up event handlers for every worker. Each worker sends a message when
6464
// it has received the expected number of packets. After that it disconnects.
6565
for (const key in cluster.workers) {
66-
if (cluster.workers.hasOwnProperty(key))
66+
if (Object.hasOwn(cluster.workers, key))
6767
setupWorker(cluster.workers[key]);
6868
}
6969

test/parallel/test-disable-proto-delete.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ const { Worker, isMainThread } = require('worker_threads');
99

1010
// eslint-disable-next-line no-proto
1111
assert.strictEqual(Object.prototype.__proto__, undefined);
12-
assert(!Object.prototype.hasOwnProperty('__proto__'));
12+
assert(!Object.hasOwn(Object.prototype, '__proto__'));
1313

1414
const ctx = vm.createContext();
1515
const ctxGlobal = vm.runInContext('this', ctx);
1616

1717
// eslint-disable-next-line no-proto
1818
assert.strictEqual(ctxGlobal.Object.prototype.__proto__, undefined);
19-
assert(!ctxGlobal.Object.prototype.hasOwnProperty('__proto__'));
19+
assert(!Object.hasOwn(ctxGlobal.Object.prototype, '__proto__'));
2020

2121
if (isMainThread) {
2222
new Worker(__filename);

test/parallel/test-disable-proto-throw.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const assert = require('assert');
77
const vm = require('vm');
88
const { Worker, isMainThread } = require('worker_threads');
99

10-
assert(Object.prototype.hasOwnProperty('__proto__'));
10+
assert(Object.hasOwn(Object.prototype, '__proto__'));
1111

1212
assert.throws(() => {
1313
// eslint-disable-next-line no-proto,no-unused-expressions

test/parallel/test-event-emitter-check-listener-leaks.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -32,40 +32,40 @@ const events = require('events');
3232
for (let i = 0; i < 10; i++) {
3333
e.on('default', common.mustNotCall());
3434
}
35-
assert.ok(!e._events.default.hasOwnProperty('warned'));
35+
assert.ok(!Object.hasOwn(e._events.default, 'warned'));
3636
e.on('default', common.mustNotCall());
3737
assert.ok(e._events.default.warned);
3838

3939
// symbol
4040
const symbol = Symbol('symbol');
4141
e.setMaxListeners(1);
4242
e.on(symbol, common.mustNotCall());
43-
assert.ok(!e._events[symbol].hasOwnProperty('warned'));
43+
assert.ok(!Object.hasOwn(e._events[symbol], 'warned'));
4444
e.on(symbol, common.mustNotCall());
45-
assert.ok(e._events[symbol].hasOwnProperty('warned'));
45+
assert.ok(Object.hasOwn(e._events[symbol], 'warned'));
4646

4747
// specific
4848
e.setMaxListeners(5);
4949
for (let i = 0; i < 5; i++) {
5050
e.on('specific', common.mustNotCall());
5151
}
52-
assert.ok(!e._events.specific.hasOwnProperty('warned'));
52+
assert.ok(!Object.hasOwn(e._events.specific, 'warned'));
5353
e.on('specific', common.mustNotCall());
5454
assert.ok(e._events.specific.warned);
5555

5656
// only one
5757
e.setMaxListeners(1);
5858
e.on('only one', common.mustNotCall());
59-
assert.ok(!e._events['only one'].hasOwnProperty('warned'));
59+
assert.ok(!Object.hasOwn(e._events['only one'], 'warned'));
6060
e.on('only one', common.mustNotCall());
61-
assert.ok(e._events['only one'].hasOwnProperty('warned'));
61+
assert.ok(Object.hasOwn(e._events['only one'], 'warned'));
6262

6363
// unlimited
6464
e.setMaxListeners(0);
6565
for (let i = 0; i < 1000; i++) {
6666
e.on('unlimited', common.mustNotCall());
6767
}
68-
assert.ok(!e._events.unlimited.hasOwnProperty('warned'));
68+
assert.ok(!Object.hasOwn(e._events.unlimited, 'warned'));
6969
}
7070

7171
// process-wide
@@ -76,16 +76,16 @@ const events = require('events');
7676
for (let i = 0; i < 42; ++i) {
7777
e.on('fortytwo', common.mustNotCall());
7878
}
79-
assert.ok(!e._events.fortytwo.hasOwnProperty('warned'));
79+
assert.ok(!Object.hasOwn(e._events.fortytwo, 'warned'));
8080
e.on('fortytwo', common.mustNotCall());
81-
assert.ok(e._events.fortytwo.hasOwnProperty('warned'));
81+
assert.ok(Object.hasOwn(e._events.fortytwo, 'warned'));
8282
delete e._events.fortytwo.warned;
8383

8484
events.EventEmitter.defaultMaxListeners = 44;
8585
e.on('fortytwo', common.mustNotCall());
86-
assert.ok(!e._events.fortytwo.hasOwnProperty('warned'));
86+
assert.ok(!Object.hasOwn(e._events.fortytwo, 'warned'));
8787
e.on('fortytwo', common.mustNotCall());
88-
assert.ok(e._events.fortytwo.hasOwnProperty('warned'));
88+
assert.ok(Object.hasOwn(e._events.fortytwo, 'warned'));
8989
}
9090

9191
// But _maxListeners still has precedence over defaultMaxListeners
@@ -94,9 +94,9 @@ const events = require('events');
9494
const e = new events.EventEmitter();
9595
e.setMaxListeners(1);
9696
e.on('uno', common.mustNotCall());
97-
assert.ok(!e._events.uno.hasOwnProperty('warned'));
97+
assert.ok(!Object.hasOwn(e._events.uno, 'warned'));
9898
e.on('uno', common.mustNotCall());
99-
assert.ok(e._events.uno.hasOwnProperty('warned'));
99+
assert.ok(Object.hasOwn(e._events.uno, 'warned'));
100100

101101
// chainable
102102
assert.strictEqual(e, e.setMaxListeners(1));

test/parallel/test-fs-stat.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ const fs = require('fs');
2727

2828
fs.stat('.', common.mustSucceed(function(stats) {
2929
assert.ok(stats.mtime instanceof Date);
30-
assert.ok(stats.hasOwnProperty('blksize'));
31-
assert.ok(stats.hasOwnProperty('blocks'));
30+
assert.ok(Object.hasOwn(stats, 'blksize'));
31+
assert.ok(Object.hasOwn(stats, 'blocks'));
3232
// Confirm that we are not running in the context of the internal binding
3333
// layer.
3434
// Ref: https://github.com/nodejs/node/commit/463d6bac8b349acc462d345a6e298a76f7d06fb1

test/parallel/test-http-client-default-headers-exist.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const expectedMethods = Object.keys(expectedHeaders);
4141
const server = http.createServer(common.mustCall((req, res) => {
4242
res.end();
4343

44-
assert(expectedHeaders.hasOwnProperty(req.method),
44+
assert(Object.hasOwn(expectedHeaders, req.method),
4545
`${req.method} was an unexpected method`);
4646

4747
const requestHeaders = Object.keys(req.headers);

test/parallel/test-http-server-capture-rejections.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ events.captureRejections = true;
2525

2626
req.on('response', common.mustCall((res) => {
2727
assert.strictEqual(res.statusCode, 500);
28-
assert.strictEqual(res.headers.hasOwnProperty('content-type'), false);
28+
assert.strictEqual(Object.hasOwn(res.headers, 'content-type'), false);
2929
let data = '';
3030
res.setEncoding('utf8');
3131
res.on('data', common.mustCall((chunk) => {

test/parallel/test-internal-errors.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ assert.throws(() => {
8585
{
8686
const myError = new errors.codes.TEST_ERROR_1('foo');
8787
assert.strictEqual(myError.code, 'TEST_ERROR_1');
88-
assert.strictEqual(myError.hasOwnProperty('code'), true);
89-
assert.strictEqual(myError.hasOwnProperty('name'), false);
88+
assert.strictEqual(Object.hasOwn(myError, 'code'), true);
89+
assert.strictEqual(Object.hasOwn(myError, 'name'), false);
9090
assert.deepStrictEqual(Object.keys(myError), ['code']);
9191
const initialName = myError.name;
9292
myError.code = 'FHQWHGADS';

test/parallel/test-module-version.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ require('../common');
33
const assert = require('assert');
44

55
// check for existence
6-
assert(process.config.variables.hasOwnProperty('node_module_version'));
6+
assert(Object.hasOwn(process.config.variables, 'node_module_version'));
77

88
// Ensure that `node_module_version` is an Integer > 0
99
assert(Number.isInteger(process.config.variables.node_module_version));

test/parallel/test-process-config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const fs = require('fs');
3131
const path = require('path');
3232

3333
// Check for existence of `process.config`.
34-
assert(process.hasOwnProperty('config'));
34+
assert(Object.hasOwn(process, 'config'));
3535

3636
// Ensure that `process.config` is an Object.
3737
assert.strictEqual(Object(process.config), process.config);

test/parallel/test-process-env.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ if (process.argv[2] === 'you-are-the-child') {
3939

4040
assert.strictEqual(Object.prototype.hasOwnProperty,
4141
process.env.hasOwnProperty);
42-
const has = process.env.hasOwnProperty('hasOwnProperty');
42+
const has = Object.hasOwn(process.env, 'hasOwnProperty');
4343
assert.strictEqual(has, false);
4444

4545
process.env.hasOwnProperty = 'asdf';

test/parallel/test-stream-duplex-writable-finished.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const assert = require('assert');
77
// basic
88
{
99
// Find it on Duplex.prototype
10-
assert(Duplex.prototype.hasOwnProperty('writableFinished'));
10+
assert(Object.hasOwn(Duplex.prototype, 'writableFinished'));
1111
}
1212

1313
// event

test/parallel/test-stream-readable-ended.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const assert = require('assert');
77
// basic
88
{
99
// Find it on Readable.prototype
10-
assert(Readable.prototype.hasOwnProperty('readableEnded'));
10+
assert(Object.hasOwn(Readable.prototype, 'readableEnded'));
1111
}
1212

1313
// event

test/parallel/test-stream-writable-finished.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const assert = require('assert');
77
// basic
88
{
99
// Find it on Writable.prototype
10-
assert(Writable.prototype.hasOwnProperty('writableFinished'));
10+
assert(Object.hasOwn(Writable.prototype, 'writableFinished'));
1111
}
1212

1313
// event

test/parallel/test-stream2-basic.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -422,23 +422,23 @@ class TestWriter extends EE {
422422

423423
{
424424
// Verify readableEncoding property
425-
assert(R.prototype.hasOwnProperty('readableEncoding'));
425+
assert(Object.hasOwn(R.prototype, 'readableEncoding'));
426426

427427
const r = new R({ encoding: 'utf8' });
428428
assert.strictEqual(r.readableEncoding, 'utf8');
429429
}
430430

431431
{
432432
// Verify readableObjectMode property
433-
assert(R.prototype.hasOwnProperty('readableObjectMode'));
433+
assert(Object.hasOwn(R.prototype, 'readableObjectMode'));
434434

435435
const r = new R({ objectMode: true });
436436
assert.strictEqual(r.readableObjectMode, true);
437437
}
438438

439439
{
440440
// Verify writableObjectMode property
441-
assert(W.prototype.hasOwnProperty('writableObjectMode'));
441+
assert(Object.hasOwn(W.prototype, 'writableObjectMode'));
442442

443443
const w = new W({ objectMode: true });
444444
assert.strictEqual(w.writableObjectMode, true);

0 commit comments

Comments
 (0)