The assertions module provides 10 assertions:
assert(expr: unknown, msg = ""): asserts expr
assertEquals(actual: unknown, expected: unknown, msg?: string): void
assertNotEquals(actual: unknown, expected: unknown, msg?: string): void
assertStrictEquals(actual: unknown, expected: unknown, msg?: string): void
assertStringContains(actual: string, expected: string, msg?: string): void
assertArrayContains(actual: unknown[], expected: unknown[], msg?: string): void
assertMatch(actual: string, expected: RegExp, msg?: string): void
assertNotMatch(actual: string, expected: RegExp, msg?: string): void
assertThrows(fn: () => void, ErrorClass?: Constructor, msgIncludes = "", msg?: string): Error
assertThrowsAsync(fn: () => Promise<void>, ErrorClass?: Constructor, msgIncludes = "", msg?: string): Promise<Error>
The assert method is a simple ‘truthy’ assertion and can be used to assert any value which can be inferred as true.
Deno.test("Test Assert", () => {
assert(1);
assert("Hello");
assert(true);
});
Equality
There are three equality assertions available, assertEquals()
, assertNotEquals()
and assertStrictEquals()
.
The assertEquals()
and assertNotEquals()
methods provide a general equality check and are capable of asserting equality between primitive types and objects.
Deno.test("Test Assert Equals", () => {
assertEquals(1, 1);
assertEquals("Hello", "Hello");
assertEquals(true, true);
assertEquals(undefined, undefined);
assertEquals(new Date(), new Date());
assertEquals(new RegExp("abc"), new RegExp("abc"));
class Foo {}
const foo1 = new Foo();
assertEquals(foo1, foo2);
});
Deno.test("Test Assert Not Equals", () => {
assertNotEquals(1, 2);
assertNotEquals("Hello", "World");
assertNotEquals(true, false);
assertNotEquals(undefined, "");
assertNotEquals(new Date(), Date.now());
assertNotEquals(new RegExp("abc"), new RegExp("def"));
});
The assertStrictEquals()
assertion is best used when you wish to make a precise check against two primitive types.
There are two methods available to assert a value contains a value, assertStringContains()
and assertArrayContains()
.
The assertStringContains()
assertion does a simple includes check on a string to see if it contains the expected string.
Deno.test("Test Assert String Contains", () => {
assertStringContains("Hello World", "Hello");
});
The assertArrayContains()
assertion is slightly more advanced and can find both a value within an array and an array of values within an array.
Deno.test("Test Assert Array Contains", () => {
assertArrayContains([1, 2, 3], [1]);
assertArrayContains([1, 2, 3], [1, 2]);
assertArrayContains(Array.from("Hello World"), Array.from("Hello"));
});
Regex
There are two ways to assert whether something throws an error in Deno, assertThrows()
and assertThrowsAsync()
. Both assertions allow you to check an has been thrown, the type of error thrown and what the message was.
The difference between the two assertions is accepts a standard function and assertThrowsAsync()
accepts a function which returns a Promise.
The assertThrows()
assertion will check an error has been thrown, and optionally will check the thrown error is of the correct type, and assert the error message is as expected.
assertThrows(
() => {
throw new Error("Panic!");
},
Error,
"Panic!",
);
});
The assertThrowsAsync()
assertion is a little more complicated, mainly because it deals with Promises. But basically it will catch thrown errors or rejections in Promises. You can also optionally check for the error type and error message.
Deno.test("Test Assert Throws Async", () => {
assertThrowsAsync(
() => {
return new Promise(() => {
throw new Error("Panic! Threw Error");
});
},
Error,
"Panic! Threw Error",
);
assertThrowsAsync(
() => {
return Promise.reject(new Error("Panic! Reject Error"));
},
Error,
"Panic! Reject Error",
);