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 equalassertNotEquals(actual, expected): Check if two things are differentassertExists(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 bassertLess(a, b): Check if a is smaller than bassertGreaterOrEqual(a, b): Check if a is bigger or equal to bassertLessOrEqual(a, b): Check if a is smaller or equal to b
Working with Objects
assertInstanceOf(object, type): Check if something is a specific typeassertObjectMatch(actual, expected): Check if objects have matching properties
Testing Errors
assertThrows(fn): Check if code throws an errorassertRejects(fn): Check if a promise fails
Special Cases
assertFail(): Make a test fail on purposeassertUnimplemented(): Mark code as not finishedassertUnreachable(): 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" ); } });
This class represents an error that occurs when an assertion fails.
The assert function checks if something is true.
If it's not true, it stops your code and tells you there's a problem.
This function checks if two numbers are almost equal.
This function, assertArrayIncludes, checks if all elements of one array are present in another array.
Checks if two things are equal.
This function checks if two values are the same.
This function, assertExists, checks if a value is present, meaning it is not null or undefined.
This function fails a test by throwing an error.
This function, assertFalse, checks if a given expression is false.
This function checks if one value is greater than another.
This function checks if one value is greater than or equal to another value.
This function checks if an HTML element matches an expected HTML string.
This function checks if a given value is an instance of a specific type.
This function checks if a given value is an error. It is useful when you want to test if your code correctly handles errors.
Checks if something is less than a certain value.
This function checks if one value is less than or equal to another value.
This function checks if a given text matches a specified pattern.
This function checks if two values are not the same.
This function checks if a given object is not an instance of a specific type.
This function checks if a given string does not match a specified pattern.
This function, assertNotStrictEquals, checks if two values are not strictly equal.
This function checks if two objects are equal by comparing their properties.
This function, assertRejects, checks if a function that returns a promise rejects with a specific error.
This function checks if two values are exactly the same.
This function checks if a string contains a specific substring.
This function checks if a function throws a specific error.
This function throws an error to indicate that a function is not implemented.
This function throws an error to indicate that a piece of code should never be reached.
return an instance of AssertionState
A type that represents any class or constructor. Think of it as a blueprint for creating things.
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.
The type of error that happens when a test check fails. This helps you identify when and why a test didn't work.
Values that JavaScript considers as "false-like". These are: false, 0, "", null, undefined
Gets the type of thing a class creates. Helps you work with the result of creating a new thing from a class.
Add Package
deno add jsr:@in/test
Import symbol
import * as mod from "@in/test/assert";
Import directly with a jsr specifier
import * as mod from "jsr:@in/test/assert";
Add Package
pnpm i jsr:@in/test
pnpm dlx jsr add @in/test
Import symbol
import * as mod from "@in/test/assert";
Add Package
yarn add jsr:@in/test
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";