Skip to content

Commit 5716f23

Browse files
richardlauMylesBorins
authored andcommitted
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 5cd9299 commit 5716f23

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
@@ -300,14 +300,15 @@ async function getHandle(dest) {
300300
const dir = path.join(tmpDir, nextdir(), nextdir());
301301
await mkdir(path.dirname(dir));
302302
await writeFile(dir);
303-
try {
304-
await mkdir(dir, { recursive: true });
305-
throw new Error('unreachable');
306-
} catch (err) {
307-
assert.notStrictEqual(err.message, 'unreachable');
308-
assert.strictEqual(err.code, 'EEXIST');
309-
assert.strictEqual(err.syscall, 'mkdir');
310-
}
303+
assert.rejects(
304+
mkdir(dir, { recursive: true }),
305+
{
306+
code: 'EEXIST',
307+
message: /EEXIST: .*mkdir/,
308+
name: 'Error',
309+
syscall: 'mkdir',
310+
}
311+
);
311312
}
312313

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

329331
// mkdirp ./

0 commit comments

Comments
 (0)