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:
parent
ae86cbb551
commit
35caa160bf
2 changed files with 46 additions and 0 deletions
|
@ -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.
|
||||
|
|
|
@ -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");
|
||||
|
|
Loading…
Reference in a new issue