Skip to content

Commit

Permalink
feat(testing): Add assertExists assertion (denoland/deno#7874)
Browse files Browse the repository at this point in the history
  • Loading branch information
getspooky authored Oct 26, 2020
1 parent 5d440ae commit b74bd42
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
17 changes: 17 additions & 0 deletions testing/asserts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,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.
Expand Down
29 changes: 29 additions & 0 deletions testing/asserts_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
assert,
assertArrayIncludes,
assertEquals,
assertExists,
AssertionError,
assertMatch,
assertNotEquals,
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit b74bd42

Please sign in to comment.