From 38d85623bd52320296fb0adc488c83d947f1e0ed 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 3f421b557f65a3..1ddd2295a9ba81 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 e4289b1aed9f98..2e45854dbae7b4 100644 --- a/test/parallel/test-fs-promises.js +++ b/test/parallel/test-fs-promises.js @@ -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. @@ -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 ./