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/read_dir_test.ts | 48 +++++++++++------------------------------ 1 file changed, 12 insertions(+), 36 deletions(-) (limited to 'cli/tests/unit/read_dir_test.ts') diff --git a/cli/tests/unit/read_dir_test.ts b/cli/tests/unit/read_dir_test.ts index d9a5244c7..de4b217bf 100644 --- a/cli/tests/unit/read_dir_test.ts +++ b/cli/tests/unit/read_dir_test.ts @@ -3,6 +3,8 @@ import { unitTest, assert, assertEquals, + assertThrows, + assertThrowsAsync, pathToAbsoluteFileUrl, } from "./test_util.ts"; @@ -30,42 +32,21 @@ unitTest({ perms: { read: true } }, function readDirSyncWithUrl(): void { }); unitTest({ perms: { read: false } }, function readDirSyncPerm(): void { - let caughtError = false; - try { + assertThrows(() => { Deno.readDirSync("tests/"); - } catch (e) { - caughtError = true; - assert(e instanceof Deno.errors.PermissionDenied); - } - assert(caughtError); + }, Deno.errors.PermissionDenied); }); unitTest({ perms: { read: true } }, function readDirSyncNotDir(): void { - let caughtError = false; - let src; - - try { - src = Deno.readDirSync("cli/tests/fixture.json"); - } catch (err) { - caughtError = true; - assert(err instanceof Error); - } - assert(caughtError); - assertEquals(src, undefined); + assertThrows(() => { + Deno.readDirSync("cli/tests/fixture.json"); + }, Error); }); unitTest({ perms: { read: true } }, function readDirSyncNotFound(): void { - let caughtError = false; - let src; - - try { - src = Deno.readDirSync("bad_dir_name"); - } catch (err) { - caughtError = true; - assert(err instanceof Deno.errors.NotFound); - } - assert(caughtError); - assertEquals(src, undefined); + assertThrows(() => { + Deno.readDirSync("bad_dir_name"); + }, Deno.errors.NotFound); }); unitTest({ perms: { read: true } }, async function readDirSuccess(): Promise< @@ -93,12 +74,7 @@ unitTest({ perms: { read: true } }, async function readDirWithUrl(): Promise< unitTest({ perms: { read: false } }, async function readDirPerm(): Promise< void > { - let caughtError = false; - try { + await assertThrowsAsync(async () => { await Deno.readDir("tests/")[Symbol.asyncIterator]().next(); - } catch (e) { - caughtError = true; - assert(e instanceof Deno.errors.PermissionDenied); - } - assert(caughtError); + }, Deno.errors.PermissionDenied); }); -- cgit v1.2.3