How to Use Assertions in Deno for Testing and Validation
In Deno, use
assert functions from the std/testing/asserts.ts module to check conditions in your code. Import functions like assert, assertEquals, or assertThrows and call them with the values you want to verify. If an assertion fails, Deno throws an error, helping you catch bugs early.Syntax
Deno provides several assertion functions in the std/testing/asserts.ts module. You import the needed functions and call them with the values to check.
assert(condition, message?): Checks ifconditionis true.assertEquals(actual, expected, message?): Checks ifactualequalsexpected.assertThrows(fn, ErrorClass?, message?): Checks iffnthrows an error.
typescript
import { assert, assertEquals, assertThrows } from "https://deno.land/std@0.203.0/testing/asserts.ts"; // Basic assertion assert(condition, "Optional failure message"); // Equality assertion assertEquals(actualValue, expectedValue, "Optional failure message"); // Assert function throws error assertThrows(() => { throw new Error("fail"); }, Error, "Optional failure message");
Example
This example shows how to use assert to check a condition, assertEquals to compare values, and assertThrows to verify an error is thrown.
typescript
import { assert, assertEquals, assertThrows } from "https://deno.land/std@0.203.0/testing/asserts.ts"; // Check if a number is positive const number = 5; assert(number > 0, "Number should be positive"); // Check if two values are equal const result = 2 + 3; assertEquals(result, 5, "Sum should be 5"); // Check if a function throws an error function fail() { throw new Error("This is an error"); } assertThrows(() => fail(), Error, "Function should throw an error"); console.log("All assertions passed.");
Output
All assertions passed.
Common Pitfalls
Common mistakes when using assertions in Deno include:
- Not importing assertion functions from
std/testing/asserts.ts. - Using
assertEqualswith different types or structures without understanding deep equality. - Not wrapping the function inside
assertThrowsas a callback, causing it to run immediately. - Ignoring assertion failure messages, which help debug issues.
typescript
import { assertThrows } from "https://deno.land/std@0.203.0/testing/asserts.ts"; // Wrong: calling function immediately // assertThrows(fail(), Error); // This runs fail() immediately and throws before assertThrows // Right: pass function as callback assertThrows(() => fail(), Error);
Quick Reference
| Assertion Function | Purpose | Usage Example |
|---|---|---|
| assert | Check if condition is true | assert(value > 0, "Must be positive") |
| assertEquals | Check if two values are equal | assertEquals(actual, expected) |
| assertThrows | Check if function throws error | assertThrows(() => fn(), Error) |
| assertNotEquals | Check if two values are not equal | assertNotEquals(a, b) |
| assertStringIncludes | Check if string contains substring | assertStringIncludes(text, "hello") |
Key Takeaways
Import assertion functions from Deno's standard library before use.
Use assert functions to verify conditions and catch bugs early.
Wrap functions in callbacks when using assertThrows to test errors.
Assertion failure messages help identify problems quickly.
Deno assertions support many checks like equality, error throwing, and string inclusion.