From c6c37e9e850fa2a30b12a0d0fd0dfe144eda0959 Mon Sep 17 00:00:00 2001 From: Richard Lau Date: Tue, 16 Apr 2019 03:26:28 +0100 Subject: [PATCH] test: use assert.rejects() and assert.throws() Replace try-catch blocks in tests with `assert.rejects()` and `assert.throws()`. PR-URL: https://github.com/nodejs/node/pull/27207 Fixes: https://github.com/nodejs/node/issues/27198 Reviewed-By: Colin Ihrig Reviewed-By: Sakthipriyan Vairamani Reviewed-By: Ben Coe --- test/parallel/test-fs-mkdir.js | 51 ++++++++++++++++--------------- test/parallel/test-fs-promises.js | 34 +++++++++++---------- 2 files changed, 45 insertions(+), 40 deletions(-) diff --git a/test/parallel/test-fs-mkdir.js b/test/parallel/test-fs-mkdir.js index 82f126e4ed1a7d..2cd42f65669e90 100644 --- a/test/parallel/test-fs-mkdir.js +++ b/test/parallel/test-fs-mkdir.js @@ -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. @@ -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. @@ -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'); diff --git a/test/parallel/test-fs-promises.js b/test/parallel/test-fs-promises.js index 3fc88f7d8033bc..28c047e3055a8c 100644 --- a/test/parallel/test-fs-promises.js +++ b/test/parallel/test-fs-promises.js @@ -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. @@ -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 ./