NojeJs How to remember Assertions

The Mental Model: "Action vs Comparison"

1. Comparison Assertions - Compare two values

These take two values (actualexpected)

javascript

// Pattern: assert.method(ACTUAL, EXPECTED)
assert.equal(actual, expected)
assert.strictEqual(actual, expected)
assert.deepEqual(actual, expected)
assert.deepStrictEqual(actual, expected)

Memory trick: "Equal" in the name → needs two things to compare.

2. Action Assertions - Test behavior/actions

These take a function that performs an action

javascript

// Pattern: assert.method(ACTION_FUNCTION)
assert.throws(() => { /* action */ })
assert.doesNotThrow(() => { /* action */ })
assert.rejects(async () => { /* action */ })

Memory trick: "Behavior words" (throws/rejects) → test actions, not values.

Quick Reference Chart

Assertion TypePatternExamples
Comparisonassert.method(ACTUAL, EXPECTED)equal()strictEqual()deepEqual()
Actionassert.method(ACTION_FUNCTION)throws()doesNotThrow()rejects()
Truthinessassert.method(VALUE)ok()fail()

Simple Decision Tree

When writing an assertion, ask yourself:

text

Are you testing...
┌─ If something HAPPENS (like throwing an error)?
│  → Use ACTION pattern: assert.throws(() => yourCode())
│
├─ If two values are EQUAL?
│  → Use COMPARISON pattern: assert.equal(actual, expected)
│
└─ If a single value is TRUTHY?
   → Use SINGLE VALUE: assert.ok(value)

Examples Side-by-Side

Comparison Assertions (2 parameters)

javascript

// Testing VALUES - need actual vs expected
assert.equal(5, 5);
assert.strictEqual("hello", "hello");
assert.deepEqual({a: 1}, {a: 1});

Action Assertions (function parameter)

javascript

// Testing BEHAVIOR - need function wrapper
assert.throws(() => throw new Error());
assert.doesNotThrow(() => normalFunction());
assert.rejects(() => asyncFunctionThatFails());

Practical Memory Tricks

Trick 1: The "What vs How" Test

  • What is the result? → Comparison (2 parameters)
  • How does it behave? → Action (function parameter)

Trick 2: The "Word Clue"

  • If the method name describes a state (equal, deep, strict) → 2 parameters
  • If the method name describes an action (throws, rejects) → function parameter

Trick 3: Simple Rule of Thumb

javascript

// When you see these words, use FUNCTION:
// throws, rejects, doesNotThrow → () => yourCode()

// When you see these words, use VALUES:
// equal, strictEqual, deepEqual → actual, expected

Practice with Examples

javascript

// ❌ CONFUSING: Mixing patterns
assert.throws(multiply(5, "2")); // Wrong!

// ✅ CLEAR: Separate in your mind
// Comparison: Test VALUES
assert.equal(multiply(5, 2), 10);

// Action: Test BEHAVIOR  
assert.throws(() => multiply(5, "2"));

Real-World Analogies

Think of it like this:

  • Comparison assertions are like a scale - you put two things on it to compare
  • Action assertions are like a camera - you record an action to see what happens

You wouldn't put an action on a scale, and you wouldn't compare two values with a camera!

Quick Cheat Sheet

Keep this mental checklist:

  1. Am I comparing two things? → Use assert.equal(a, b)
  2. Am I testing if something happens? → Use assert.throws(() => code())
  3. Am I checking if something exists/is truthy? → Use assert.ok(value)

This way, you don't need to memorize each method individually - you just follow the pattern based on what you're trying to test!

Leave a Reply

Your email address will not be published. Required fields are marked *