The assertions module provides nine 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
    • 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.

    1. Deno.test("Test Assert", () => {
    2. assert(1);
    3. assert("Hello");
    4. assert(true);
    5. });

    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.

    1. Deno.test("Test Assert Equals", () => {
    2. assertEquals(1, 1);
    3. assertEquals("Hello", "Hello");
    4. assertEquals(true, true);
    5. assertEquals(undefined, undefined);
    6. assertEquals(null, null);
    7. assertEquals(new RegExp("abc"), new RegExp("abc"));
    8. class Foo {}
    9. const foo1 = new Foo();
    10. assertEquals(foo1, foo2);
    11. });
    12. Deno.test("Test Assert Not Equals", () => {
    13. assertNotEquals(1, 2);
    14. assertNotEquals("Hello", "World");
    15. assertNotEquals(true, false);
    16. assertNotEquals(undefined, "");
    17. assertNotEquals(new Date(), Date.now());
    18. assertNotEquals(new RegExp("abc"), new RegExp("def"));
    19. });

    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.

    1. Deno.test("Test Assert String Contains", () => {
    2. assertStringContains("Hello World", "Hello");
    3. });

    The assertArrayContains() assertion is slightly more advanced and can find both a value within an array and an array of values within an array.

    1. Deno.test("Test Assert Array Contains", () => {
    2. assertArrayContains([1, 2, 3], [1]);
    3. assertArrayContains([1, 2, 3], [1, 2]);
    4. assertArrayContains(Array.from("Hello World"), Array.from("Hello"));
    5. });

    Regex

    There are two ways to assert whether something throws an error in Deno, assertThrows() and assertAsyncThrows(). 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 assertAsyncThrows() 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.

    1. Deno.test("Test Assert Throws", () => {
    2. () => {
    3. throw new Error("Panic!");
    4. },
    5. Error,
    6. "Panic!",
    7. );
    8. });

    The assertAsyncThrows() 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.

    1. Deno.test("Test Assert Throws Async", () => {
    2. assertThrowsAsync(
    3. () => {
    4. return new Promise(() => {
    5. throw new Error("Panic! Threw Error");
    6. });
    7. },
    8. Error,
    9. "Panic! Threw Error",
    10. );
    11. assertThrowsAsync(
    12. () => {
    13. return Promise.reject(new Error("Panic! Reject Error"));
    14. },
    15. Error,
    16. "Panic! Reject Error",
    17. );

    Custom Messages