Skip to content

Commit 54decfa

Browse files
gwerMylesBorins
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 <eaglexrlnk@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
1 parent 03adb94 commit 54decfa

21 files changed

Lines changed: 67 additions & 67 deletions

‎test/inspector/test-inspector.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function checkBadPath(err, response) {
3434
functionexpectMainScriptSource(result){
3535
constexpected=helper.mainScriptSource();
3636
constsource=result['scriptSource'];
37-
assert(source&&(source.indexOf(expected)>=0),
37+
assert(source&&(source.includes(expected)),
3838
'Script source is wrong: '+source);
3939
}
4040

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

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

2525
process.on('exit',function(){
26-
assert.ok(response.indexOf('HELLO=WORLD')>=0,
26+
assert.ok(response.includes('HELLO=WORLD'),
2727
'spawn did not use process.env as default');
2828
});

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

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

3131
process.on('exit',function(){
32-
assert.ok(response.indexOf('HELLO=WORLD')>=0);
33-
assert.ok(response.indexOf('FOO=BAR')>=0);
32+
assert.ok(response.includes('HELLO=WORLD'));
33+
assert.ok(response.includes('FOO=BAR'));
3434
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ process.on('exit', function() {
3535
console.log('response: ',response);
3636
assert.strictEqual(1,success_count);
3737
assert.strictEqual(0,error_count);
38-
assert.ok(response.indexOf('HELLO=WORLD')>=0);
38+
assert.ok(response.includes('HELLO=WORLD'));
3939
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function verifyBufOutput(ret) {
3131
assert.deepStrictEqual(ret.stderr,msgErrBuf);
3232
}
3333

34-
if(process.argv.indexOf('spawnchild')!==-1){
34+
if(process.argv.includes('spawnchild')){
3535
switch(process.argv[3]){
3636
case'1':
3737
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(functiononUncaught(){
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
thrownewError(domainErrHandlerExMessage);
5353
}catch(e){

‎test/parallel/test-error-reporting.js‎

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

1717
// Assert the script is mentioned in error output.
18-
assert.ok(stderr.indexOf(script)>=0);
18+
assert.ok(stderr.includes(script));
1919

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

‎test/parallel/test-fs-error-messages.js‎

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,66 +13,66 @@ const existingDir2 = path.join(common.fixturesDir, 'keys');
1313

1414
fs.stat(fn,function(err){
1515
assert.strictEqual(fn,err.path);
16-
assert.ok(0<=err.message.indexOf(fn));
16+
assert.ok(err.message.includes(fn));
1717
});
1818

1919
fs.lstat(fn,function(err){
20-
assert.ok(0<=err.message.indexOf(fn));
20+
assert.ok(err.message.includes(fn));
2121
});
2222

2323
fs.readlink(fn,function(err){
24-
assert.ok(0<=err.message.indexOf(fn));
24+
assert.ok(err.message.includes(fn));
2525
});
2626

2727
fs.link(fn,'foo',function(err){
28-
assert.ok(0<=err.message.indexOf(fn));
28+
assert.ok(err.message.includes(fn));
2929
});
3030

3131
fs.link(existingFile,existingFile2,function(err){
32-
assert.ok(0<=err.message.indexOf(existingFile));
33-
assert.ok(0<=err.message.indexOf(existingFile2));
32+
assert.ok(err.message.includes(existingFile));
33+
assert.ok(err.message.includes(existingFile2));
3434
});
3535

3636
fs.symlink(existingFile,existingFile2,function(err){
37-
assert.ok(0<=err.message.indexOf(existingFile));
38-
assert.ok(0<=err.message.indexOf(existingFile2));
37+
assert.ok(err.message.includes(existingFile));
38+
assert.ok(err.message.includes(existingFile2));
3939
});
4040

4141
fs.unlink(fn,function(err){
42-
assert.ok(0<=err.message.indexOf(fn));
42+
assert.ok(err.message.includes(fn));
4343
});
4444

4545
fs.rename(fn,'foo',function(err){
46-
assert.ok(0<=err.message.indexOf(fn));
46+
assert.ok(err.message.includes(fn));
4747
});
4848

4949
fs.rename(existingDir,existingDir2,function(err){
50-
assert.ok(0<=err.message.indexOf(existingDir));
51-
assert.ok(0<=err.message.indexOf(existingDir2));
50+
assert.ok(err.message.includes(existingDir));
51+
assert.ok(err.message.includes(existingDir2));
5252
});
5353

5454
fs.rmdir(fn,function(err){
55-
assert.ok(0<=err.message.indexOf(fn));
55+
assert.ok(err.message.includes(fn));
5656
});
5757

5858
fs.mkdir(existingFile,0o666,function(err){
59-
assert.ok(0<=err.message.indexOf(existingFile));
59+
assert.ok(err.message.includes(existingFile));
6060
});
6161

6262
fs.rmdir(existingFile,function(err){
63-
assert.ok(0<=err.message.indexOf(existingFile));
63+
assert.ok(err.message.includes(existingFile));
6464
});
6565

6666
fs.chmod(fn,0o666,function(err){
67-
assert.ok(0<=err.message.indexOf(fn));
67+
assert.ok(err.message.includes(fn));
6868
});
6969

7070
fs.open(fn,'r',0o666,function(err){
71-
assert.ok(0<=err.message.indexOf(fn));
71+
assert.ok(err.message.includes(fn));
7272
});
7373

7474
fs.readFile(fn,function(err){
75-
assert.ok(0<=err.message.indexOf(fn));
75+
assert.ok(err.message.includes(fn));
7676
});
7777

7878
// Sync
@@ -85,122 +85,122 @@ try {
8585
fs.statSync(fn);
8686
}catch(err){
8787
errors.push('stat');
88-
assert.ok(0<=err.message.indexOf(fn));
88+
assert.ok(err.message.includes(fn));
8989
}
9090

9191
try{
9292
++expected;
9393
fs.mkdirSync(existingFile,0o666);
9494
}catch(err){
9595
errors.push('mkdir');
96-
assert.ok(0<=err.message.indexOf(existingFile));
96+
assert.ok(err.message.includes(existingFile));
9797
}
9898

9999
try{
100100
++expected;
101101
fs.chmodSync(fn,0o666);
102102
}catch(err){
103103
errors.push('chmod');
104-
assert.ok(0<=err.message.indexOf(fn));
104+
assert.ok(err.message.includes(fn));
105105
}
106106

107107
try{
108108
++expected;
109109
fs.lstatSync(fn);
110110
}catch(err){
111111
errors.push('lstat');
112-
assert.ok(0<=err.message.indexOf(fn));
112+
assert.ok(err.message.includes(fn));
113113
}
114114

115115
try{
116116
++expected;
117117
fs.readlinkSync(fn);
118118
}catch(err){
119119
errors.push('readlink');
120-
assert.ok(0<=err.message.indexOf(fn));
120+
assert.ok(err.message.includes(fn));
121121
}
122122

123123
try{
124124
++expected;
125125
fs.linkSync(fn,'foo');
126126
}catch(err){
127127
errors.push('link');
128-
assert.ok(0<=err.message.indexOf(fn));
128+
assert.ok(err.message.includes(fn));
129129
}
130130

131131
try{
132132
++expected;
133133
fs.linkSync(existingFile,existingFile2);
134134
}catch(err){
135135
errors.push('link');
136-
assert.ok(0<=err.message.indexOf(existingFile));
137-
assert.ok(0<=err.message.indexOf(existingFile2));
136+
assert.ok(err.message.includes(existingFile));
137+
assert.ok(err.message.includes(existingFile2));
138138
}
139139

140140
try{
141141
++expected;
142142
fs.symlinkSync(existingFile,existingFile2);
143143
}catch(err){
144144
errors.push('symlink');
145-
assert.ok(0<=err.message.indexOf(existingFile));
146-
assert.ok(0<=err.message.indexOf(existingFile2));
145+
assert.ok(err.message.includes(existingFile));
146+
assert.ok(err.message.includes(existingFile2));
147147
}
148148

149149
try{
150150
++expected;
151151
fs.unlinkSync(fn);
152152
}catch(err){
153153
errors.push('unlink');
154-
assert.ok(0<=err.message.indexOf(fn));
154+
assert.ok(err.message.includes(fn));
155155
}
156156

157157
try{
158158
++expected;
159159
fs.rmdirSync(fn);
160160
}catch(err){
161161
errors.push('rmdir');
162-
assert.ok(0<=err.message.indexOf(fn));
162+
assert.ok(err.message.includes(fn));
163163
}
164164

165165
try{
166166
++expected;
167167
fs.rmdirSync(existingFile);
168168
}catch(err){
169169
errors.push('rmdir');
170-
assert.ok(0<=err.message.indexOf(existingFile));
170+
assert.ok(err.message.includes(existingFile));
171171
}
172172

173173
try{
174174
++expected;
175175
fs.openSync(fn,'r');
176176
}catch(err){
177177
errors.push('opens');
178-
assert.ok(0<=err.message.indexOf(fn));
178+
assert.ok(err.message.includes(fn));
179179
}
180180

181181
try{
182182
++expected;
183183
fs.renameSync(fn,'foo');
184184
}catch(err){
185185
errors.push('rename');
186-
assert.ok(0<=err.message.indexOf(fn));
186+
assert.ok(err.message.includes(fn));
187187
}
188188

189189
try{
190190
++expected;
191191
fs.renameSync(existingDir,existingDir2);
192192
}catch(err){
193193
errors.push('rename');
194-
assert.ok(0<=err.message.indexOf(existingDir));
195-
assert.ok(0<=err.message.indexOf(existingDir2));
194+
assert.ok(err.message.includes(existingDir));
195+
assert.ok(err.message.includes(existingDir2));
196196
}
197197

198198
try{
199199
++expected;
200200
fs.readdirSync(fn);
201201
}catch(err){
202202
errors.push('readdir');
203-
assert.ok(0<=err.message.indexOf(fn));
203+
assert.ok(err.message.includes(fn));
204204
}
205205

206206
process.on('exit',function(){

‎test/parallel/test-http-client-parse-error.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ net.createServer(function(c) {
2626
path: '/'
2727
}).on('error',function(e){
2828
console.log('got error from client');
29-
assert.ok(e.message.indexOf('Parse Error')>=0);
29+
assert.ok(e.message.includes('Parse Error'));
3030
assert.strictEqual(e.code,'HPE_INVALID_CONSTANT');
3131
parseErrors++;
3232
}).end();

0 commit comments

Comments
 (0)