Skip to main content
Home
Works with
This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score58%
Downloads1/wk
Published4 months ago (0.7.1)

A universal testing module that works seamlessly across Deno, Node.js, and Bun runtimes. Write tests once, run them anywhere - from mobile to desktop, and 3D/spatial environments!

Assert

In testing, assert is a tool that helps you check if your code works correctly. Think of it like a safety net that catches problems before they reach your users.

Getting Started

First, import what you need:

import { test, assert } from "@in/test";

Basic Testing

There are two main ways to write tests: using assert or using expect. But we are going to focus on assert in this documentation.

Using Assert

test({
  name: "check if numbers add correctly",
  fn: () => {
    const result = 2 + 2;
    assertEquals(result, 4);
  }
});

Main Features

Checking Values (Assertions)

  • assertEquals(actual, expected): Check if two things are equal
  • assertNotEquals(actual, expected): Check if two things are different
  • assertExists(value): Check if something exists (not null/undefined)
  • assertMatch(text, pattern): Check if text matches a pattern

Comparing Numbers

  • assertGreater(a, b): Check if a is bigger than b
  • assertLess(a, b): Check if a is smaller than b
  • assertGreaterOrEqual(a, b): Check if a is bigger or equal to b
  • assertLessOrEqual(a, b): Check if a is smaller or equal to b

Working with Objects

  • assertInstanceOf(object, type): Check if something is a specific type
  • assertObjectMatch(actual, expected): Check if objects have matching properties

Testing Errors

  • assertThrows(fn): Check if code throws an error
  • assertRejects(fn): Check if a promise fails

Special Cases

  • assertFail(): Make a test fail on purpose
  • assertUnimplemented(): Mark code as not finished
  • assertUnreachable(): Mark code that should never run
NOTE: Writing Good Tests
  • Give your tests clear names that explain what they check
  • Test one thing at a time
  • Use descriptive variable names
  • Add helpful error messages
Terminology: Common Testing Terms
  • Test: A piece of code that checks if another piece of code works correctly
  • Assert: To check if something is true
  • Expect: See Expect Module for another way to write tests that reads more like English
  • Mock: See Mock Module for creating fake versions of things for testing

Examples

Testing a Simple Function

function add(a: number, b: number): number {
  return a + b;
}

test({
  name: "add function should work correctly",
  fn: () => {
    assertEquals(add(2, 2), 4);
    assertEquals(add(-1, 1), 0);
    assertEquals(add(0, 0), 0);
  }
});

Testing Error Cases

function divide(a: number, b: number): number {
  if (b === 0) throw new Error("Cannot divide by zero");
  return a / b;
}

test({
  name: "divide function should handle errors",
  fn: () => {
    // Test normal case
    assertEquals(divide(10, 2), 5);

    // Test error case
    assertThrows(
      () => divide(10, 0),
      Error,
      "Cannot divide by zero"
    );
  }
});

Testing Async Code

async function fetchUser(id: string) {
  if (!id) throw new Error("ID required");
  return { id, name: "Test User" };
}

test({
  name: "fetchUser should work with valid ID",
  fn: async () => {
    const user = await fetchUser("123");
    assertEquals(user.name, "Test User");

    await assertRejects(
      () => fetchUser(""),
      Error,
      "ID required"
    );
  }
});

Classes

Functions

f
assert

The assert function checks if something is true. If it's not true, it stops your code and tells you there's a problem.

f
assertAlmostEquals

This function checks if two numbers are almost equal.

f
assertArrayIncludes

This function, assertArrayIncludes, checks if all elements of one array are present in another array.

f
assertEqual

Checks if two things are equal.

f
assertEquals

This function checks if two values are the same.

f
assertExists

This function, assertExists, checks if a value is present, meaning it is not null or undefined.

f
assertFail

This function fails a test by throwing an error.

f
assertFalse

This function, assertFalse, checks if a given expression is false.

f
assertGreater

This function checks if one value is greater than another.

f
assertGreaterOrEqual

This function checks if one value is greater than or equal to another value.

f
assertHTMLEquals

This function checks if an HTML element matches an expected HTML string.

f
assertInstanceOf

This function checks if a given value is an instance of a specific type.

f
assertIsError

This function checks if a given value is an error. It is useful when you want to test if your code correctly handles errors.

f
assertLess

Checks if something is less than a certain value.

f
assertLessOrEqual

This function checks if one value is less than or equal to another value.

f
assertMatch

This function checks if a given text matches a specified pattern.

f
assertNotEquals

This function checks if two values are not the same.

f
assertNotInstanceOf

This function checks if a given object is not an instance of a specific type.

f
assertNotMatch

This function checks if a given string does not match a specified pattern.

f
assertNotStrictEquals

This function, assertNotStrictEquals, checks if two values are not strictly equal.

f
assertObjectMatch

This function checks if two objects are equal by comparing their properties.

f
assertRejects

This function, assertRejects, checks if a function that returns a promise rejects with a specific error.

f
assertStrictEquals

This function checks if two values are exactly the same.

f
assertStringIncludes

This function checks if a string contains a specific substring.

f
assertThrows

This function checks if a function throws a specific error.

f
assertUnimplemented

This function throws an error to indicate that a function is not implemented.

f
assertUnreachable

This function throws an error to indicate that a piece of code should never be reached.

f
getAssertionState

return an instance of AssertionState

Type Aliases

T
AssertAnyConstructorProp

A type that represents any class or constructor. Think of it as a blueprint for creating things.

T
AssertArrayLikeArgProp

A type for anything that works like a list (has a length and numbered items). This could be an array, string, or any other list-like thing.

T
AssertAssertionErrorProp

The type of error that happens when a test check fails. This helps you identify when and why a test didn't work.

T
AssertFalsyProp

Values that JavaScript considers as "false-like". These are: false, 0, "", null, undefined

T
AssertGetConstructorTypeProp

Gets the type of thing a class creates. Helps you work with the result of creating a new thing from a class.

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@in/test

Import symbol

import * as mod from "@in/test/assert";
or

Import directly with a jsr specifier

import * as mod from "jsr:@in/test/assert";

Add Package

pnpm i jsr:@in/test
or (using pnpm 10.8 or older)
pnpm dlx jsr add @in/test

Import symbol

import * as mod from "@in/test/assert";

Add Package

yarn add jsr:@in/test
or (using Yarn 4.8 or older)
yarn dlx jsr add @in/test

Import symbol

import * as mod from "@in/test/assert";

Add Package

vlt install jsr:@in/test

Import symbol

import * as mod from "@in/test/assert";

Add Package

npx jsr add @in/test

Import symbol

import * as mod from "@in/test/assert";

Add Package

bunx jsr add @in/test

Import symbol

import * as mod from "@in/test/assert";