From b13b2cd883d438a177bacd3d43a3adf170104991 Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Tue, 22 Jan 2019 16:15:25 +1000 Subject: [PATCH] Add assert.throwsAsync() Original: https://github.com/denoland/deno_std/commit/83bb7e99b6ff00011d4ac63a265644f657282c06 --- testing/README.md | 35 ++++++++++++++ testing/mod.ts | 32 +++++++++++++ testing/test.ts | 120 ++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 183 insertions(+), 4 deletions(-) diff --git a/testing/README.md b/testing/README.md index 49a6526fef..70613e0e1f 100644 --- a/testing/README.md +++ b/testing/README.md @@ -28,6 +28,11 @@ functions: this function does. Also compares any errors thrown to an optional expected `Error` class and checks that the error `.message` includes an optional string. +- `assert.throwsAsync()` - Expects the passed `fn` to be async and throw (or + return a `Promise` that rejects). If the `fn` does not throw or reject, this + function will throw asynchronously. Also compares any errors thrown to an + optional expected `Error` class and checks that the error `.message` includes + an optional string. `assertEqual()` is the same as `assert.equal()` but maintained for backwards compatibility. @@ -106,3 +111,33 @@ test(function fails() { }); }); ``` + +Using `assert.throwsAsync()`: + +```ts +test(async function doesThrow() { + assert.throwsAsync(async () => { + throw new TypeError("hello world!"); + }); + assert.throwsAsync(async () => { + throw new TypeError("hello world!"); + }, TypeError); + assert.throwsAsync( + async () => { + throw new TypeError("hello world!"); + }, + TypeError, + "hello" + ); + assert.throwsAsync(async () => { + return Promise.reject(new Error()); + }); +}); + +// This test will not pass +test(async function fails() { + assert.throwsAsync(async () => { + console.log("Hello world"); + }); +}); +``` diff --git a/testing/mod.ts b/testing/mod.ts index 0ac2c4ea44..42f3939c53 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, + ErrorClass?: Constructor, + msgIncludes = "", + msg = "" + ): Promise { + 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); + } } }; diff --git a/testing/test.ts b/testing/test.ts index 34b2762b48..1765fd6b6b 100644 --- a/testing/test.ts +++ b/testing/test.ts @@ -27,15 +27,15 @@ test(function testingEqual() { test(function testingAssertEqual() { const a = Object.create(null); a.b = "foo"; - assertEqual(a, a); - assert(assert.equal === assertEqual); + assert.equal(a, a); + assert(assertEqual === assert.equal); }); test(function testingAssertEqualActualUncoercable() { let didThrow = false; const a = Object.create(null); try { - assertEqual(a, "bar"); + assert.equal(a, "bar"); } catch (e) { didThrow = true; console.log(e.message); @@ -48,7 +48,7 @@ test(function testingAssertEqualExpectedUncoercable() { let didThrow = false; const a = Object.create(null); try { - assertEqual("bar", a); + assert.equal("bar", a); } catch (e) { didThrow = true; console.log(e.message); @@ -161,3 +161,115 @@ test(function testingThrowsMsgNotIncludes() { assert(count === 1); assert(didThrow); }); + +test(async function testingDoesThrowAsync() { + let count = 0; + await assert.throwsAsync(async () => { + count++; + throw new Error(); + }); + assert(count === 1); +}); + +test(async function testingDoesReject() { + let count = 0; + await assert.throwsAsync(() => { + count++; + return Promise.reject(new Error()); + }); + assert(count === 1); +}); + +test(async function testingDoesNotThrowAsync() { + let count = 0; + let didThrow = false; + try { + await assert.throwsAsync(async () => { + count++; + console.log("Hello world"); + }); + } catch (e) { + assert(e.message === "Expected function to throw."); + didThrow = true; + } + assert(count === 1); + assert(didThrow); +}); + +test(async function testingDoesNotRejectAsync() { + let count = 0; + let didThrow = false; + try { + await assert.throwsAsync(() => { + count++; + console.log("Hello world"); + return Promise.resolve(); + }); + } catch (e) { + assert(e.message === "Expected function to throw."); + didThrow = true; + } + assert(count === 1); + assert(didThrow); +}); + +test(async function testingThrowsAsyncErrorType() { + let count = 0; + await assert.throwsAsync(async () => { + count++; + throw new TypeError(); + }, TypeError); + assert(count === 1); +}); + +test(async function testingThrowsAsyncNotErrorType() { + let count = 0; + let didThrow = false; + try { + await assert.throwsAsync(async () => { + count++; + throw new TypeError(); + }, RangeError); + } catch (e) { + assert(e.message === `Expected error to be instance of "RangeError".`); + didThrow = true; + } + assert(count === 1); + assert(didThrow); +}); + +test(async function testingThrowsAsyncMsgIncludes() { + let count = 0; + await assert.throwsAsync( + async () => { + count++; + throw new TypeError("Hello world!"); + }, + TypeError, + "world" + ); + assert(count === 1); +}); + +test(async function testingThrowsMsgNotIncludes() { + let count = 0; + let didThrow = false; + try { + await assert.throwsAsync( + async () => { + count++; + throw new TypeError("Hello world!"); + }, + TypeError, + "foobar" + ); + } catch (e) { + assert( + e.message === + `Expected error message to include "foobar", but got "Hello world!".` + ); + didThrow = true; + } + assert(count === 1); + assert(didThrow); +});