What if your code could tell you exactly when and why it breaks, without guesswork?
Why Assertions and matchers in Deno? - Purpose & Use Cases
Imagine you write code and want to check if it works correctly by manually printing values and reading logs every time.
You have to guess if the output is right or not, and fix bugs by trial and error.
Manually checking code results is slow and tiring.
You can easily miss mistakes or misunderstand what went wrong.
It's hard to keep track of many checks and repeat them after every change.
Assertions and matchers let you write clear checks that automatically verify your code's behavior.
They tell you exactly what failed and why, so you can fix bugs faster.
This makes testing reliable, repeatable, and easy to understand.
if (result !== expected) { console.log('Oops, something is wrong'); }
assertEquals(result, expected);
You can confidently change code knowing tests will catch mistakes instantly.
When building a calculator app, assertions check if adding or subtracting numbers always gives the right answer, so users never get wrong results.
Manual checks are slow and error-prone.
Assertions and matchers automate and clarify testing.
They help catch bugs early and improve code quality.
Practice
Solution
Step 1: Understand what assertions do
Assertions are used to verify that the code produces the expected results during tests.Step 2: Identify the purpose in testing
They help catch errors by comparing actual output with expected output.Final Answer:
To check if the code behaves as expected -> Option BQuick Check:
Assertions verify behavior = D [OK]
- Thinking assertions format output
- Believing assertions speed up code
- Confusing assertions with variable creation
Solution
Step 1: Recall Deno's assertion function names
Deno provides an assertion function calledassertEqualsto check equality.Step 2: Check parameter order and spelling
The correct order isassertEquals(actual, expected), matching assertEquals(actual, expected); exactly.Final Answer:
assertEquals(actual, expected); -> Option AQuick Check:
Correct function and order = A [OK]
- Swapping actual and expected parameters
- Using assertEqual instead of assertEquals
- Using dot notation incorrectly
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
const result = 5 + 3;
assertEquals(result, 8);Solution
Step 1: Evaluate the calculation
The variableresultis assigned 5 + 3, which equals 8.Step 2: Check the assertion
The assertion comparesresult(8) with expected value 8, so it matches and passes.Final Answer:
Test passes without errors -> Option DQuick Check:
5 + 3 = 8 matches expected = A [OK]
- Assuming import causes syntax error
- Thinking assertion fails when values match
- Confusing runtime error with assertion failure
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
const value = "hello";
assertEquals(value, Hello);Solution
Step 1: Check the assertion parameters
The second parameter isHellowithout quotes, so JavaScript treats it as a variable.Step 2: Identify the error caused
SinceHellois not defined, this causes a ReferenceError at runtime.Final Answer:
Missing quotes around Hello causing a ReferenceError -> Option AQuick Check:
String literals need quotes = C [OK]
- Forgetting quotes around strings
- Assuming import URL is wrong
- Thinking assertEquals needs more arguments
Solution
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.Step 2: Evaluate each option
assertEquals(result.includes(10), true); usesresult.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)); usescontainswhich is not a valid JavaScript method.Final Answer:
assertEquals(result.includes(10), true); -> Option CQuick Check:
Use includes() to check array contents = B [OK]
- Using == to compare arrays
- Using non-existent contains() method
- Comparing array directly to a number
Ready to prove it?
Pass the test and this pattern moves unknown
