📖 Scenario: You are creating simple tests for a calculator module in Deno. You want to check if your functions return the correct results using assertions and matchers.
🎯 Goal: Build a Deno test file that uses assertions and matchers to verify calculator functions work correctly.
📋 What You'll Learn
Create a calculator object with add and subtract functions
Set up a test configuration variable for a tolerance value
Write tests using Deno's assertEquals and assertAlmostEquals matchers
Complete the test suite with proper test definitions and imports
💡 Why This Matters
🌍 Real World
Testing code is essential in real projects to catch bugs early and ensure your functions work as expected.
💼 Career
Knowing how to write tests with assertions and matchers is a key skill for software developers to maintain code quality.
Progress0 / 4 steps
1
Create the calculator object with add and subtract functions
Create a constant called calculator that is an object with two functions: add which takes a and b and returns their sum, and subtract which takes a and b and returns their difference.
Deno
Hint
Use an object literal with arrow functions for add and subtract.
2
Add a tolerance variable for approximate equality
Create a constant called tolerance and set it to 0.001 to use for approximate comparisons in tests.
Deno
Hint
Just create a constant with the exact name and value.
3
Write tests using Deno assertions and matchers
Import assertEquals and assertAlmostEquals from "https://deno.land/std@0.203.0/testing/asserts.ts". Then write two tests: one named "add test" that checks calculator.add(2, 3) equals 5 using assertEquals, and another named "subtract test" that checks calculator.subtract(5, 2.999) is approximately equal to 2.001 using assertAlmostEquals with the tolerance.
Deno
Hint
Use Deno.test to define tests and call the correct assertion functions with the exact arguments.
4
Complete the test file with export and proper structure
Ensure the test file is complete by exporting nothing (default for Deno tests) and that all tests are defined at the top level. Confirm the import statement is at the top and no extra code is outside the tests.
Deno
Hint
No extra exports or code needed. Just keep the test file clean and structured.
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
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 B
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
Step 1: Recall Deno's assertion function names
Deno provides an assertion function called assertEquals to check equality.
Step 2: Check parameter order and spelling
The correct order is assertEquals(actual, expected), matching assertEquals(actual, expected); exactly.
Final Answer:
assertEquals(actual, expected); -> Option A
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
Step 1: Evaluate the calculation
The variable result is assigned 5 + 3, which equals 8.
Step 2: Check the assertion
The assertion compares result (8) with expected value 8, so it matches and passes.
Final Answer:
Test passes without errors -> Option D
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
Step 1: Check the assertion parameters
The second parameter is Hello without quotes, so JavaScript treats it as a variable.
Step 2: Identify the error caused
Since Hello is not defined, this causes a ReferenceError at runtime.
Final Answer:
Missing quotes around Hello causing a ReferenceError -> Option A
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
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); 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.
Final Answer:
assertEquals(result.includes(10), true); -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.