Skip to content

Commit c6c37e9

Browse files
committed
test: use assert.rejects() and assert.throws()
Replace try-catch blocks in tests with `assert.rejects()` and `assert.throws()`. PR-URL: #27207 Fixes: #27198 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Ben Coe <[email protected]>
1 parent 2400cbc commit c6c37e9

File tree

2 files changed

+45
-40
lines changed

2 files changed

+45
-40
lines changed

test/parallel/test-fs-mkdir.js

+27-24
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,15 @@ function nextdir() {
122122
fs.mkdirSync(path.dirname(pathname));
123123
fs.writeFileSync(pathname, '', 'utf8');
124124

125-
try {
126-
fs.mkdirSync(pathname, { recursive: true });
127-
throw new Error('unreachable');
128-
} catch (err) {
129-
assert.notStrictEqual(err.message, 'unreachable');
130-
assert.strictEqual(err.code, 'EEXIST');
131-
assert.strictEqual(err.syscall, 'mkdir');
132-
}
125+
assert.throws(
126+
() => { fs.mkdirSync(pathname, { recursive: true }); },
127+
{
128+
code: 'EEXIST',
129+
message: /EEXIST: .*mkdir/,
130+
name: 'Error',
131+
syscall: 'mkdir',
132+
}
133+
);
133134
}
134135

135136
// mkdirpSync when part of the path is a file.
@@ -140,14 +141,15 @@ function nextdir() {
140141
fs.mkdirSync(path.dirname(filename));
141142
fs.writeFileSync(filename, '', 'utf8');
142143

143-
try {
144-
fs.mkdirSync(pathname, { recursive: true });
145-
throw new Error('unreachable');
146-
} catch (err) {
147-
assert.notStrictEqual(err.message, 'unreachable');
148-
assert.strictEqual(err.code, 'ENOTDIR');
149-
assert.strictEqual(err.syscall, 'mkdir');
150-
}
144+
assert.throws(
145+
() => { fs.mkdirSync(pathname, { recursive: true }); },
146+
{
147+
code: 'ENOTDIR',
148+
message: /ENOTDIR: .*mkdir/,
149+
name: 'Error',
150+
syscall: 'mkdir',
151+
}
152+
);
151153
}
152154

153155
// `mkdirp` when folder does not yet exist.
@@ -195,14 +197,15 @@ if (common.isMainThread && (common.isLinux || common.isOSX)) {
195197
fs.mkdirSync(pathname);
196198
process.chdir(pathname);
197199
fs.rmdirSync(pathname);
198-
try {
199-
fs.mkdirSync('X', { recursive: true });
200-
throw new Error('unreachable');
201-
} catch (err) {
202-
assert.notStrictEqual(err.message, 'unreachable');
203-
assert.strictEqual(err.code, 'ENOENT');
204-
assert.strictEqual(err.syscall, 'mkdir');
205-
}
200+
assert.throws(
201+
() => { fs.mkdirSync('X', { recursive: true }); },
202+
{
203+
code: 'ENOENT',
204+
message: /ENOENT: .*mkdir/,
205+
name: 'Error',
206+
syscall: 'mkdir',
207+
}
208+
);
206209
fs.mkdir('X', { recursive: true }, (err) => {
207210
assert.strictEqual(err.code, 'ENOENT');
208211
assert.strictEqual(err.syscall, 'mkdir');

test/parallel/test-fs-promises.js

+18-16
Original file line numberDiff line numberDiff line change
@@ -299,14 +299,15 @@ async function getHandle(dest) {
299299
const dir = path.join(tmpDir, nextdir(), nextdir());
300300
await mkdir(path.dirname(dir));
301301
await writeFile(dir);
302-
try {
303-
await mkdir(dir, { recursive: true });
304-
throw new Error('unreachable');
305-
} catch (err) {
306-
assert.notStrictEqual(err.message, 'unreachable');
307-
assert.strictEqual(err.code, 'EEXIST');
308-
assert.strictEqual(err.syscall, 'mkdir');
309-
}
302+
assert.rejects(
303+
mkdir(dir, { recursive: true }),
304+
{
305+
code: 'EEXIST',
306+
message: /EEXIST: .*mkdir/,
307+
name: 'Error',
308+
syscall: 'mkdir',
309+
}
310+
);
310311
}
311312

312313
// `mkdirp` when part of the path is a file.
@@ -315,14 +316,15 @@ async function getHandle(dest) {
315316
const dir = path.join(file, nextdir(), nextdir());
316317
await mkdir(path.dirname(file));
317318
await writeFile(file);
318-
try {
319-
await mkdir(dir, { recursive: true });
320-
throw new Error('unreachable');
321-
} catch (err) {
322-
assert.notStrictEqual(err.message, 'unreachable');
323-
assert.strictEqual(err.code, 'ENOTDIR');
324-
assert.strictEqual(err.syscall, 'mkdir');
325-
}
319+
assert.rejects(
320+
mkdir(dir, { recursive: true }),
321+
{
322+
code: 'ENOTDIR',
323+
message: /ENOTDIR: .*mkdir/,
324+
name: 'Error',
325+
syscall: 'mkdir',
326+
}
327+
);
326328
}
327329

328330
// mkdirp ./

0 commit comments

Comments
 (0)