Skip to main content

Actions Status

Explicitly

This library extends the Deno assertions module with additional assertions so developers can write more explicit unit tests if they choose to. It works with Deno Test Tools in the same way the built in assertions do.

The principle behind this library is unit tests should focus small units of business logic. Assertions should therefore focus on the assertion of basic types in an explicit manner. This library provides assertions which fulfill this requirement and all the assertions are simple and specific which makes the intent of a unit test clearer.

For more generalised equality checks, better suited to integration or functional tests, please use the built in Deno assertions module.

Installation

Before you use this library make sure you have read and understood how to set up Deno Tests.

To install this library and make use of the assertions in your test suite simply add the following import to the test modules which require it.

import {
  assertTrue,
  assertFalse,
  assertSame,
  assertGreater,
  assertGreaterOrEqual,
  assertLess,
  assertLessOrEqual,
  assertInstanceOf,
  assertTypeOf,
} from "https://raw.githubusercontent.com/RobDWaller/explicitly/0.1.0/mod.ts";

Basic Usage

This assertion library comes with nine basic assertions methods:

  • assertTrue(actual: unknown): void
  • assertFalse(actual: unknown): void
  • assertSame(actual: unknown, expected: unknown): void
  • assertGreater(actual: unknown, expected: unknown): void
  • assertGreaterOrEqual(actual: unknown, expected: unknown): void
  • assertLess(actual: unknown, expected: unknown): void
  • assertLessOrEqual(actual: unknown, expected: unknown): void
  • assertInstanceOf(actual: unknown, expected: any): void
  • assertTypeOf(actual: unknown, expected: string): void

Each of these assertions aims to test a single thing. This means unit tests are explicit and clearer to read.

Assert True Example

A basic example of when you may wish to assert true.

Deno.test("Assert True Example", () => {
  function isOlderThanFive(age: number): boolean {
    return age >= 5;
  }

  const childAge: number = 6;

  const result: boolean = isOlderThanFive(childAge);

  assertTrue(result);
});

Assert Instance Of Example

A more advanced example of when you may wish to assert an instance of. Usually when there is a polymorphic relationship between objects.

Deno.test("Assert Instance Of Example", () => {
  interface Person {
    name: string;
    age: number;
    location: string;
  }

  class Adult implements Person {
    name: string;
    age: number;
    location: string;

    constructor(name: string, age: number, location: string) {
      this.name = name;
      this.age = age;
      this.location = location;
    }
  }

  class Child implements Person {
    name: string;
    age: number;
    location: string;

    constructor(name: string, age: number, location: string) {
      this.name = name;
      this.age = age;
      this.location = location;
    }
  }

  function createPerson(name: string, age: number, location: string): Person {
    if (age < 18) {
      return new Child(name, age, location);
    }

    return new Adult(name, age, location);
  }

  const jenny = createPerson("Jenny Brown", 12, "US");

  assertInstanceOf(jenny, Child);
});