summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKitson Kelly <me@kitsonkelly.com>2019-01-22 16:15:25 +1000
committerRyan Dahl <ry@tinyclouds.org>2019-01-22 08:34:35 -0600
commitb13b2cd883d438a177bacd3d43a3adf170104991 (patch)
tree230d03f8f3e53a87add3088d756ee2ac8c9a2b91
parent8d474a7911d6b36aec222f08ef18d88baabe2dad (diff)
Add assert.throwsAsync()
Original: https://github.com/denoland/deno_std/commit/83bb7e99b6ff00011d4ac63a265644f657282c06
-rw-r--r--testing/README.md35
-rw-r--r--testing/mod.ts32
-rw-r--r--testing/test.ts120
3 files changed, 183 insertions, 4 deletions
diff --git a/testing/README.md b/testing/README.md
index 49a6526fe..70613e0e1 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 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);
+ }
}
};
diff --git a/testing/test.ts b/testing/test.ts
index 34b2762b4..1765fd6b6 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);
+});