🔬 Testing

Test-driven development made easy

In Deno, you can only write tests in a dedicated file like, main_test.ts

Deno Testing Examples

file_type_typescript main_test.ts
import { expect } from "jsr:@std/expect";
import { assertEquals, assertNotMatch, assertExists, assertMatch, assertGreater } from "@std/assert";

// Function to Test
function multiply(a: number, b: number) {
    return a * b;
}


// Basic Assertions
Deno.test(function multiplyTest() {
    assertEquals(multiply(2, 2), 4);
    assertEquals(multiply(2, 3), 6);
});

// Jest-style expect
Deno.test("multiply test", () => {
    expect(multiply(2, 3)).toBe(6);
});

// Async Test
Deno.test("mock API call", async () => {
    const mockApi = () => Promise.resolve("mock data");
    const result = await mockApi();
    assertEquals(result, "mock data");
});

// Multi-step Test
Deno.test("database lib", async (t) => {
    // Setup Logic
    const db = new Map()

    await t.step("db exists", () => {
        assertExists(db)
    });

    await t.step("insert user", () => {
        db.set('user', 'jeff');

        assertGreater(db.size, 0)
        assertMatch(db.get('user'), /jeff/)
        assertNotMatch(db.get('user'), /Bob/)

    });

});

Questions? Let's chat

Open Discord