Skip to content

Commit

Permalink
test: use assert.rejects() and assert.throws()
Browse files Browse the repository at this point in the history
Replace try-catch blocks in tests with `assert.rejects()` and
`assert.throws()`.

PR-URL: #27207
Fixes: #27198
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Ben Coe <bencoe@gmail.com>
  • Loading branch information
richardlau authored and MylesBorins committed May 16, 2019
1 parent 5cd9299 commit 5716f23
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 40 deletions.
51 changes: 27 additions & 24 deletions test/parallel/test-fs-mkdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,15 @@ function nextdir() {
fs.mkdirSync(path.dirname(pathname));
fs.writeFileSync(pathname, '', 'utf8');

try {
fs.mkdirSync(pathname, { recursive: true });
throw new Error('unreachable');
} catch (err) {
assert.notStrictEqual(err.message, 'unreachable');
assert.strictEqual(err.code, 'EEXIST');
assert.strictEqual(err.syscall, 'mkdir');
}
assert.throws(
() => { fs.mkdirSync(pathname, { recursive: true }); },
{
code: 'EEXIST',
message: /EEXIST: .*mkdir/,
name: 'Error',
syscall: 'mkdir',
}
);
}

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

try {
fs.mkdirSync(pathname, { recursive: true });
throw new Error('unreachable');
} catch (err) {
assert.notStrictEqual(err.message, 'unreachable');
assert.strictEqual(err.code, 'ENOTDIR');
assert.strictEqual(err.syscall, 'mkdir');
}
assert.throws(
() => { fs.mkdirSync(pathname, { recursive: true }); },
{
code: 'ENOTDIR',
message: /ENOTDIR: .*mkdir/,
name: 'Error',
syscall: 'mkdir',
}
);
}

// `mkdirp` when folder does not yet exist.
Expand Down Expand Up @@ -195,14 +197,15 @@ if (common.isMainThread && (common.isLinux || common.isOSX)) {
fs.mkdirSync(pathname);
process.chdir(pathname);
fs.rmdirSync(pathname);
try {
fs.mkdirSync('X', { recursive: true });
throw new Error('unreachable');
} catch (err) {
assert.notStrictEqual(err.message, 'unreachable');
assert.strictEqual(err.code, 'ENOENT');
assert.strictEqual(err.syscall, 'mkdir');
}
assert.throws(
() => { fs.mkdirSync('X', { recursive: true }); },
{
code: 'ENOENT',
message: /ENOENT: .*mkdir/,
name: 'Error',
syscall: 'mkdir',
}
);
fs.mkdir('X', { recursive: true }, (err) => {
assert.strictEqual(err.code, 'ENOENT');
assert.strictEqual(err.syscall, 'mkdir');
Expand Down
34 changes: 18 additions & 16 deletions test/parallel/test-fs-promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,15 @@ async function getHandle(dest) {
const dir = path.join(tmpDir, nextdir(), nextdir());
await mkdir(path.dirname(dir));
await writeFile(dir);
try {
await mkdir(dir, { recursive: true });
throw new Error('unreachable');
} catch (err) {
assert.notStrictEqual(err.message, 'unreachable');
assert.strictEqual(err.code, 'EEXIST');
assert.strictEqual(err.syscall, 'mkdir');
}
assert.rejects(
mkdir(dir, { recursive: true }),
{
code: 'EEXIST',
message: /EEXIST: .*mkdir/,
name: 'Error',
syscall: 'mkdir',
}
);
}

// `mkdirp` when part of the path is a file.
Expand All @@ -316,14 +317,15 @@ async function getHandle(dest) {
const dir = path.join(file, nextdir(), nextdir());
await mkdir(path.dirname(file));
await writeFile(file);
try {
await mkdir(dir, { recursive: true });
throw new Error('unreachable');
} catch (err) {
assert.notStrictEqual(err.message, 'unreachable');
assert.strictEqual(err.code, 'ENOTDIR');
assert.strictEqual(err.syscall, 'mkdir');
}
assert.rejects(
mkdir(dir, { recursive: true }),
{
code: 'ENOTDIR',
message: /ENOTDIR: .*mkdir/,
name: 'Error',
syscall: 'mkdir',
}
);
}

// mkdirp ./
Expand Down

0 comments on commit 5716f23

Please sign in to comment.