diff options
| author | Kitson Kelly <me@kitsonkelly.com> | 2019-01-22 16:15:25 +1000 |
|---|---|---|
| committer | Ryan Dahl <ry@tinyclouds.org> | 2019-01-22 08:34:35 -0600 |
| commit | b13b2cd883d438a177bacd3d43a3adf170104991 (patch) | |
| tree | 230d03f8f3e53a87add3088d756ee2ac8c9a2b91 /testing/mod.ts | |
| parent | 8d474a7911d6b36aec222f08ef18d88baabe2dad (diff) | |
Add assert.throwsAsync()
Original: https://github.com/denoland/deno_std/commit/83bb7e99b6ff00011d4ac63a265644f657282c06
Diffstat (limited to 'testing/mod.ts')
| -rw-r--r-- | testing/mod.ts | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/testing/mod.ts b/testing/mod.ts index 0ac2c4ea4..42f3939c5 100644 --- a/testing/mod.ts +++ b/testing/mod.ts @@ -108,6 +108,38 @@ const assertions = { msg = `Expected function to throw${msg ? `: ${msg}` : "."}`; throw new Error(msg); } + }, + + async throwsAsync( + fn: () => Promise<void>, + ErrorClass?: Constructor, + msgIncludes = "", + msg = "" + ): Promise<void> { + let doesThrow = false; + try { + await fn(); + } catch (e) { + if (ErrorClass && !(Object.getPrototypeOf(e) === ErrorClass.prototype)) { + msg = `Expected error to be instance of "${ErrorClass.name}"${ + msg ? `: ${msg}` : "." + }`; + throw new Error(msg); + } + if (msgIncludes) { + if (!e.message.includes(msgIncludes)) { + msg = `Expected error message to include "${msgIncludes}", but got "${ + e.message + }"${msg ? `: ${msg}` : "."}`; + throw new Error(msg); + } + } + doesThrow = true; + } + if (!doesThrow) { + msg = `Expected function to throw${msg ? `: ${msg}` : "."}`; + throw new Error(msg); + } } }; |
