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 committed Apr 16, 2019
1 parent 2400cbc commit c6c37e9
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
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
Expand Up @@ -299,14 +299,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 @@ -315,14 +316,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 c6c37e9

Please sign in to comment.