summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYasser A.Idrissi <spookyframework@gmail.com>2020-10-26 16:46:38 +0100
committerGitHub <noreply@github.com>2020-10-26 16:46:38 +0100
commit35caa160bf9b781d20364d77f53fee6e8f9899c3 (patch)
tree888852814fa920c0423332cb675ccf16980238fb
parentae86cbb551f7b88f83d73a447411f753485e49e2 (diff)
feat(std/testing): Add assertExists assertion (#7874)
-rw-r--r--std/testing/asserts.ts17
-rw-r--r--std/testing/asserts_test.ts29
2 files changed, 46 insertions, 0 deletions
diff --git a/std/testing/asserts.ts b/std/testing/asserts.ts
index d46b26ded..55c647d04 100644
--- a/std/testing/asserts.ts
+++ b/std/testing/asserts.ts
@@ -330,6 +330,23 @@ export function assertNotStrictEquals(
}
/**
+ * Make an assertion that actual is not null or undefined. If not
+ * then thrown.
+ */
+export function assertExists(
+ actual: unknown,
+ msg?: string,
+): void {
+ if (actual === undefined || actual === null) {
+ if (!msg) {
+ msg =
+ `actual: "${actual}" expected to match anything but null or undefined`;
+ }
+ throw new AssertionError(msg);
+ }
+}
+
+/**
* Make an assertion that actual includes expected. If not
* then thrown.
*/
diff --git a/std/testing/asserts_test.ts b/std/testing/asserts_test.ts
index d357a761b..4f153e78c 100644
--- a/std/testing/asserts_test.ts
+++ b/std/testing/asserts_test.ts
@@ -4,6 +4,7 @@ import {
assert,
assertArrayIncludes,
assertEquals,
+ assertExists,
AssertionError,
assertMatch,
assertNotEquals,
@@ -160,6 +161,34 @@ Deno.test("testingNotEquals", function (): void {
assertEquals(didThrow, true);
});
+Deno.test("testingAssertExists", function (): void {
+ assertExists("Denosaurus");
+ assertExists(false);
+ assertExists(0);
+ assertExists("");
+ assertExists(-0);
+ assertExists(0);
+ assertExists(NaN);
+ let didThrow;
+ try {
+ assertExists(undefined);
+ didThrow = false;
+ } catch (e) {
+ assert(e instanceof AssertionError);
+ didThrow = true;
+ }
+ assertEquals(didThrow, true);
+ didThrow = false;
+ try {
+ assertExists(null);
+ didThrow = false;
+ } catch (e) {
+ assert(e instanceof AssertionError);
+ didThrow = true;
+ }
+ assertEquals(didThrow, true);
+});
+
Deno.test("testingAssertStringContains", function (): void {
assertStringIncludes("Denosaurus", "saur");
assertStringIncludes("Denosaurus", "Deno");