Bird
Raised Fist0
Denoframework~10 mins

Assertions and matchers in Deno - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Assertions and matchers
Write test code
↓
Run test with assertions
↓
Assertion checks value
↓
Pass
↓
Continue
↓
Fix code or test
↩Back to Run test
This flow shows how test code with assertions runs, checks values, and either passes or fails with messages.
Execution Sample
Deno
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

Deno.test("sum test", () => {
  const result = 2 + 3;
  assertEquals(result, 5);
});
This test checks if 2 + 3 equals 5 using an assertion.
Execution Table
StepActionValue CheckedExpectedAssertion ResultOutput
1Calculate result2 + 3N/AN/Aresult = 5
2Call assertEqualsresult5PassNo error, test passes
3Test endsN/AN/APassTest suite continues
💡 Test ends because assertion passed without error
Variable Tracker
VariableStartAfter Step 1After Step 2Final
resultundefined555
Key Moments - 2 Insights
What happens if the assertion fails?
If the assertion fails, like if result was not 5, the test stops and shows an error message at step 2 in the execution_table.
Why do we use assertEquals instead of just comparing values?
assertEquals automatically checks and reports if values differ, making it easier to find errors, as shown in step 2 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'result' after step 1?
Aundefined
B5
C2 + 3
DN/A
💡 Hint
Check the 'Value Checked' and 'Output' columns in row for step 1.
At which step does the assertion check happen?
AStep 1
BStep 3
CStep 2
DNo assertion check
💡 Hint
Look at the 'Action' column to find when assertEquals is called.
If the expected value was 4 instead of 5, what would happen at step 2?
ATest stops with error
BValue changes to 4
CAssertion passes
DTest ignores assertion
💡 Hint
Refer to the 'Assertion Result' and 'Output' columns for failure cases.
Concept Snapshot
Assertions check if values meet expectations in tests.
Use matchers like assertEquals(value, expected) to compare.
If assertion passes, test continues silently.
If it fails, test stops and shows error.
This helps catch bugs early and clearly.
Full Transcript
In Deno testing, assertions are used to check if code behaves as expected. For example, assertEquals compares two values. The test runs code, calculates results, then uses assertions to verify correctness. If the assertion passes, the test continues without error. If it fails, the test stops and shows an error message. This process helps developers find mistakes quickly. The execution table shows each step: calculating values, checking assertions, and test outcomes. Variables like 'result' change as code runs and are tracked to understand test flow. Beginners often wonder what happens on failure or why assertions are used; the key is that assertions automate checks and provide clear feedback. Visual quizzes help reinforce understanding by asking about values at specific steps and outcomes of assertion checks.

Practice

(1/5)
1. What is the main purpose of assertions in Deno testing?
easy
A. To create new variables automatically
B. To check if the code behaves as expected
C. To speed up the execution of code
D. To format the output of the program

Solution

  1. Step 1: Understand what assertions do

    Assertions are used to verify that the code produces the expected results during tests.
  2. Step 2: Identify the purpose in testing

    They help catch errors by comparing actual output with expected output.
  3. Final Answer:

    To check if the code behaves as expected -> Option B
  4. Quick Check:

    Assertions verify behavior = D [OK]
Hint: Assertions verify expected behavior in tests [OK]
Common Mistakes:
  • Thinking assertions format output
  • Believing assertions speed up code
  • Confusing assertions with variable creation
2. Which of the following is the correct syntax to assert equality in Deno tests?
easy
A. assertEquals(actual, expected);
B. assertEqual(actual, expected);
C. assert.equal(actual, expected);
D. assertEquals(expected, actual);

Solution

  1. Step 1: Recall Deno's assertion function names

    Deno provides an assertion function called assertEquals to check equality.
  2. Step 2: Check parameter order and spelling

    The correct order is assertEquals(actual, expected), matching assertEquals(actual, expected); exactly.
  3. Final Answer:

    assertEquals(actual, expected); -> Option A
  4. Quick Check:

    Correct function and order = A [OK]
Hint: Use assertEquals(actual, expected) exactly [OK]
Common Mistakes:
  • Swapping actual and expected parameters
  • Using assertEqual instead of assertEquals
  • Using dot notation incorrectly
3. What will be the result of this Deno test code?
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

const result = 5 + 3;
assertEquals(result, 8);
medium
A. Runtime error because result is undefined
B. Test fails with an assertion error
C. Syntax error due to import statement
D. Test passes without errors

Solution

  1. Step 1: Evaluate the calculation

    The variable result is assigned 5 + 3, which equals 8.
  2. Step 2: Check the assertion

    The assertion compares result (8) with expected value 8, so it matches and passes.
  3. Final Answer:

    Test passes without errors -> Option D
  4. Quick Check:

    5 + 3 = 8 matches expected = A [OK]
Hint: Check calculation matches expected value exactly [OK]
Common Mistakes:
  • Assuming import causes syntax error
  • Thinking assertion fails when values match
  • Confusing runtime error with assertion failure
4. Identify the error in this Deno test code:
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

const value = "hello";
assertEquals(value, Hello);
medium
A. Missing quotes around Hello causing a ReferenceError
B. Incorrect import URL for assertEquals
C. assertEquals requires three arguments
D. Variable value is not defined

Solution

  1. Step 1: Check the assertion parameters

    The second parameter is Hello without quotes, so JavaScript treats it as a variable.
  2. Step 2: Identify the error caused

    Since Hello is not defined, this causes a ReferenceError at runtime.
  3. Final Answer:

    Missing quotes around Hello causing a ReferenceError -> Option A
  4. Quick Check:

    String literals need quotes = C [OK]
Hint: Always quote string literals in assertions [OK]
Common Mistakes:
  • Forgetting quotes around strings
  • Assuming import URL is wrong
  • Thinking assertEquals needs more arguments
5. You want to test if a function returns an array containing the number 10. Which assertion is best to use in Deno?
hard
A. assert(result == [10]);
B. assertEquals(result, 10);
C. assertEquals(result.includes(10), true);
D. assert(result.contains(10));

Solution

  1. Step 1: Understand the goal

    You want to check if the array contains the number 10, not if it equals 10 or a single-element array.
  2. Step 2: Evaluate each option

    assertEquals(result.includes(10), true); uses result.includes(10) which returns true if 10 is in the array, then asserts it equals true. This is correct.
    assert(result == [10]); compares array to [10] with == which fails because arrays are reference types.
    assertEquals(result, 10); compares array to number 10, which is incorrect.
    assert(result.contains(10)); uses contains which is not a valid JavaScript method.
  3. Final Answer:

    assertEquals(result.includes(10), true); -> Option C
  4. Quick Check:

    Use includes() to check array contents = B [OK]
Hint: Use includes() method inside assertEquals for array checks [OK]
Common Mistakes:
  • Using == to compare arrays
  • Using non-existent contains() method
  • Comparing array directly to a number
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard