From 87f8f99c49e62c06f85bb453a7c12b32634c3bef Mon Sep 17 00:00:00 2001 From: Casper Beyer Date: Thu, 25 Jun 2020 06:57:08 +0800 Subject: refactor(cli/tests/unit) to use assertThrows (#6459) --- cli/tests/unit/write_text_file_test.ts | 43 +++++++++++----------------------- 1 file changed, 14 insertions(+), 29 deletions(-) (limited to 'cli/tests/unit/write_text_file_test.ts') diff --git a/cli/tests/unit/write_text_file_test.ts b/cli/tests/unit/write_text_file_test.ts index d72fa722e..fcc8ba728 100644 --- a/cli/tests/unit/write_text_file_test.ts +++ b/cli/tests/unit/write_text_file_test.ts @@ -1,4 +1,9 @@ -import { unitTest, assert, assertEquals } from "./test_util.ts"; +import { + unitTest, + assertEquals, + assertThrows, + assertThrowsAsync, +} from "./test_util.ts"; unitTest( { perms: { read: true, write: true } }, @@ -28,27 +33,17 @@ unitTest( unitTest({ perms: { write: true } }, function writeTextFileSyncFail(): void { const filename = "/baddir/test.txt"; // The following should fail because /baddir doesn't exist (hopefully). - let caughtError = false; - try { + assertThrows(() => { Deno.writeTextFileSync(filename, "hello"); - } catch (e) { - caughtError = true; - assert(e instanceof Deno.errors.NotFound); - } - assert(caughtError); + }, Deno.errors.NotFound); }); unitTest({ perms: { write: false } }, function writeTextFileSyncPerm(): void { const filename = "/baddir/test.txt"; // The following should fail due to no write permission - let caughtError = false; - try { + assertThrows(() => { Deno.writeTextFileSync(filename, "Hello"); - } catch (e) { - caughtError = true; - assert(e instanceof Deno.errors.PermissionDenied); - } - assert(caughtError); + }, Deno.errors.PermissionDenied); }); unitTest( @@ -81,14 +76,9 @@ unitTest( async function writeTextFileNotFound(): Promise { const filename = "/baddir/test.txt"; // The following should fail because /baddir doesn't exist (hopefully). - let caughtError = false; - try { + await assertThrowsAsync(async () => { await Deno.writeTextFile(filename, "Hello"); - } catch (e) { - caughtError = true; - assert(e instanceof Deno.errors.NotFound); - } - assert(caughtError); + }, Deno.errors.NotFound); } ); @@ -97,13 +87,8 @@ unitTest( async function writeTextFilePerm(): Promise { const filename = "/baddir/test.txt"; // The following should fail due to no write permission - let caughtError = false; - try { + await assertThrowsAsync(async () => { await Deno.writeTextFile(filename, "Hello"); - } catch (e) { - caughtError = true; - assert(e instanceof Deno.errors.PermissionDenied); - } - assert(caughtError); + }, Deno.errors.PermissionDenied); } ); -- cgit v1.2.3