Skip to content

Commit 0142276

Browse files
gweraqrln
authored andcommitted
test: replace indexOf with includes
Start the transition to Array.prototype.includes() and String.prototype.includes(). This commit refactors most of the comparisons of Array.prototype.indexOf() and String.prototype.indexOf() return values with -1 to the former methods in tests. PR-URL: #12604 Refs: #12586 Reviewed-By: Alexey Orlenko <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]>
1 parent 0324ac6 commit 0142276

23 files changed

+83
-83
lines changed

test/addons-napi/test_constructor/test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ const propertyNames = [];
2222
for (const name in test_object) {
2323
propertyNames.push(name);
2424
}
25-
assert.ok(propertyNames.indexOf('echo') >= 0);
26-
assert.ok(propertyNames.indexOf('readwriteValue') >= 0);
27-
assert.ok(propertyNames.indexOf('readonlyValue') >= 0);
28-
assert.ok(propertyNames.indexOf('hiddenValue') < 0);
29-
assert.ok(propertyNames.indexOf('readwriteAccessor1') < 0);
30-
assert.ok(propertyNames.indexOf('readwriteAccessor2') < 0);
31-
assert.ok(propertyNames.indexOf('readonlyAccessor1') < 0);
32-
assert.ok(propertyNames.indexOf('readonlyAccessor2') < 0);
25+
assert.ok(propertyNames.includes('echo'));
26+
assert.ok(propertyNames.includes('readwriteValue'));
27+
assert.ok(propertyNames.includes('readonlyValue'));
28+
assert.ok(!propertyNames.includes('hiddenValue'));
29+
assert.ok(!propertyNames.includes('readwriteAccessor1'));
30+
assert.ok(!propertyNames.includes('readwriteAccessor2'));
31+
assert.ok(!propertyNames.includes('readonlyAccessor1'));
32+
assert.ok(!propertyNames.includes('readonlyAccessor2'));
3333

3434
// The napi_writable attribute should be ignored for accessors.
3535
test_object.readwriteAccessor1 = 1;

test/addons-napi/test_properties/test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ const propertyNames = [];
2121
for (const name in test_object) {
2222
propertyNames.push(name);
2323
}
24-
assert.ok(propertyNames.indexOf('echo') >= 0);
25-
assert.ok(propertyNames.indexOf('readwriteValue') >= 0);
26-
assert.ok(propertyNames.indexOf('readonlyValue') >= 0);
27-
assert.ok(propertyNames.indexOf('hiddenValue') < 0);
28-
assert.ok(propertyNames.indexOf('readwriteAccessor1') < 0);
29-
assert.ok(propertyNames.indexOf('readwriteAccessor2') < 0);
30-
assert.ok(propertyNames.indexOf('readonlyAccessor1') < 0);
31-
assert.ok(propertyNames.indexOf('readonlyAccessor2') < 0);
24+
assert.ok(propertyNames.includes('echo'));
25+
assert.ok(propertyNames.includes('readwriteValue'));
26+
assert.ok(propertyNames.includes('readonlyValue'));
27+
assert.ok(!propertyNames.includes('hiddenValue'));
28+
assert.ok(!propertyNames.includes('readwriteAccessor1'));
29+
assert.ok(!propertyNames.includes('readwriteAccessor2'));
30+
assert.ok(!propertyNames.includes('readonlyAccessor1'));
31+
assert.ok(!propertyNames.includes('readonlyAccessor2'));
3232

3333
// The napi_writable attribute should be ignored for accessors.
3434
test_object.readwriteAccessor1 = 1;

test/inspector/test-inspector.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function checkBadPath(err, response) {
3535
function expectMainScriptSource(result) {
3636
const expected = helper.mainScriptSource();
3737
const source = result['scriptSource'];
38-
assert(source && (source.indexOf(expected) >= 0),
38+
assert(source && (source.includes(expected)),
3939
'Script source is wrong: ' + source);
4040
}
4141

test/parallel/test-child-process-default-options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ child.stdout.on('data', function(chunk) {
4444
});
4545

4646
process.on('exit', function() {
47-
assert.ok(response.indexOf('HELLO=WORLD') >= 0,
47+
assert.ok(response.includes('HELLO=WORLD'),
4848
'spawn did not use process.env as default');
4949
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ child.stdout.on('data', function(chunk) {
5050
});
5151

5252
process.on('exit', function() {
53-
assert.ok(response.indexOf('HELLO=WORLD') >= 0);
54-
assert.ok(response.indexOf('FOO=BAR') >= 0);
53+
assert.ok(response.includes('HELLO=WORLD'));
54+
assert.ok(response.includes('FOO=BAR'));
5555
});

test/parallel/test-child-process-exec-env.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,5 @@ process.on('exit', function() {
5656
console.log('response: ', response);
5757
assert.strictEqual(1, success_count);
5858
assert.strictEqual(0, error_count);
59-
assert.ok(response.indexOf('HELLO=WORLD') >= 0);
59+
assert.ok(response.includes('HELLO=WORLD'));
6060
});

test/parallel/test-child-process-spawnsync-input.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function verifyBufOutput(ret) {
5252
assert.deepStrictEqual(ret.stderr, msgErrBuf);
5353
}
5454

55-
if (process.argv.indexOf('spawnchild') !== -1) {
55+
if (process.argv.includes('spawnchild')) {
5656
switch (process.argv[3]) {
5757
case '1':
5858
ret = spawnSync(process.execPath, args, { stdio: 'inherit' });

test/parallel/test-domain-throw-error-then-throw-from-uncaught-exception-handler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const RAN_UNCAUGHT_EXCEPTION_HANDLER_EXIT_CODE = 42;
1717

1818
if (process.argv[2] === 'child') {
1919
process.on('uncaughtException', common.mustCall(function onUncaught() {
20-
if (process.execArgv.indexOf('--abort-on-uncaught-exception') !== -1) {
20+
if (process.execArgv.includes('--abort-on-uncaught-exception')) {
2121
// When passing --abort-on-uncaught-exception to the child process,
2222
// we want to make sure that this handler (the process' uncaughtException
2323
// event handler) wasn't called. Unfortunately we can't parse the child

test/parallel/test-domain-with-abort-on-uncaught-exception.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ if (process.argv[2] === 'child') {
4343
d.on('error', function(err) {
4444
// Swallowing the error on purpose if 'throwInDomainErrHandler' is not
4545
// set
46-
if (process.argv.indexOf('throwInDomainErrHandler') !== -1) {
46+
if (process.argv.includes('throwInDomainErrHandler')) {
4747
// If useTryCatch is set, wrap the throw in a try/catch block.
4848
// This is to make sure that a caught exception does not trigger
4949
// an abort.
50-
if (process.argv.indexOf('useTryCatch') !== -1) {
50+
if (process.argv.includes('useTryCatch')) {
5151
try {
5252
throw new Error(domainErrHandlerExMessage);
5353
} catch (e) {

test/parallel/test-error-reporting.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function errExec(script, callback) {
3636
assert.ok(stderr.split('\n').length > 2);
3737

3838
// Assert the script is mentioned in error output.
39-
assert.ok(stderr.indexOf(script) >= 0);
39+
assert.ok(stderr.includes(script));
4040

4141
// Proxy the args for more tests.
4242
callback(err, stdout, stderr);

0 commit comments

Comments
 (0)