1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00

feat(std/testing): Add assertExists assertion (#7874)

This commit is contained in:
Yasser A.Idrissi 2020-10-26 16:46:38 +01:00 committed by GitHub
parent ae86cbb551
commit 35caa160bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 0 deletions

View file

@ -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.

View file

@ -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");