0
0
DenoHow-ToBeginner ยท 3 min read

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 if condition is true.
  • assertEquals(actual, expected, message?): Checks if actual equals expected.
  • assertThrows(fn, ErrorClass?, message?): Checks if fn throws 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 assertEquals with different types or structures without understanding deep equality.
  • Not wrapping the function inside assertThrows as 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 FunctionPurposeUsage Example
assertCheck if condition is trueassert(value > 0, "Must be positive")
assertEqualsCheck if two values are equalassertEquals(actual, expected)
assertThrowsCheck if function throws errorassertThrows(() => fn(), Error)
assertNotEqualsCheck if two values are not equalassertNotEquals(a, b)
assertStringIncludesCheck if string contains substringassertStringIncludes(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.