Bird
Raised Fist0
Denoframework~10 mins

Writing test functions in Deno - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Writing test functions
Write test function
↓
Import test module
↓
Define test with Deno.test()
↓
Run assertions inside test
↓
Execute tests with deno test
↓
See pass/fail results
This flow shows how you write a test function, run it with Deno's test runner, and see the results.
Execution Sample
Deno
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

Deno.test("addTest", function () {
  const result = 2 + 3;
  assertEquals(result, 5);
});
This code defines a test function named addTest that checks if 2 + 3 equals 5 using Deno's assertEquals.
Execution Table
StepActionEvaluationResult
1Import assertEquals from Deno stdModule importedassertEquals function ready
2Define test function addTestFunction createdTest registered
3Run test addTestCalculate 2 + 3result = 5
4Assert result equals 5assertEquals(5, 5)Pass
5Test runner reportsAll tests passedSuccess message shown
💡 All tests completed, no failures
Variable Tracker
VariableStartAfter Step 3Final
resultundefined55
Key Moments - 3 Insights
Why do we import assertEquals before writing the test?
assertEquals is the function that checks if the test result matches the expected value. Without importing it (see Step 1), the test cannot verify correctness.
What happens if the assertion fails?
If assertEquals finds the values don't match (Step 4), the test runner will mark the test as failed and show an error message instead of 'Pass'.
Why do we wrap the test code inside test(function)?
Wrapping code inside Deno.test() (Step 2) registers it as a test case that Deno can run and report on, instead of running immediately.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'result' after Step 3?
A3
Bundefined
C5
DError
💡 Hint
Check the 'Evaluation' and 'Result' columns in Step 3 of the execution table.
At which step does the test runner confirm all tests passed?
AStep 3
BStep 5
CStep 4
DStep 2
💡 Hint
Look for the step where the test runner reports success in the execution table.
If assertEquals was not imported, what would happen at Step 4?
AAn error would occur because assertEquals is undefined
BTest runner would skip the test
CTest would pass anyway
DThe test would run but not check anything
💡 Hint
Refer to Step 1 and Step 4 in the execution table about importing assertEquals.
Concept Snapshot
Writing test functions in Deno:
- Import assert functions from std/testing
- Use Deno.test(function) to define tests
- Inside test, run code and assert expected results
- Run tests with 'deno test' command
- See pass/fail results in console
Full Transcript
In Deno, you write test functions by importing assertion helpers like assertEquals from the standard library. Then you define a test using Deno.test(), passing a function that contains your test code. Inside this function, you perform operations and use assertEquals to check if the result matches what you expect. When you run 'deno test' in the terminal, Deno executes all registered tests and reports which passed or failed. This process helps you verify your code works as intended.

Practice

(1/5)
1. What is the main purpose of using deno.test in your code?
easy
A. To write test functions that check if code works correctly
B. To run the main application server
C. To format code automatically
D. To deploy code to the cloud

Solution

  1. Step 1: Understand the role of deno.test

    deno.test is used to define test functions that verify code behavior.
  2. Step 2: Differentiate from other tasks

    Running servers, formatting, or deploying are not related to deno.test.
  3. Final Answer:

    To write test functions that check if code works correctly -> Option A
  4. Quick Check:

    Test functions = deno.test usage [OK]
Hint: Tests check code correctness using deno.test [OK]
Common Mistakes:
  • Confusing test functions with running servers
  • Thinking deno.test formats code
  • Assuming deno.test deploys code
2. Which of the following is the correct syntax to define a test function in Deno?
easy
A. test(() => { /* test code */ });
B. test.deno(() => { /* test code */ });
C. deno.runTest(() => { /* test code */ });
D. deno.test(() => { /* test code */ });

Solution

  1. Step 1: Recall correct Deno test syntax

    The correct way to define a test is deno.test(() => { ... }).
  2. Step 2: Identify incorrect options

    Options B, C, and D use wrong method names or missing namespace.
  3. Final Answer:

    deno.test(() => { /* test code */ }); -> Option D
  4. Quick Check:

    Correct test syntax = deno.test [OK]
Hint: Use deno.test() exactly to define tests [OK]
Common Mistakes:
  • Swapping method names like test.deno or deno.runTest
  • Omitting 'deno.' prefix
  • Using just test() without import or context
3. What will be the output when running this test code in Deno?
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
deno.test("sum test", () => {
  const result = 2 + 3;
  assertEquals(result, 5);
});
medium
A. Runtime error: deno.test is undefined
B. Test fails with assertion error
C. Test passes with no errors
D. Syntax error due to import

Solution

  1. Step 1: Analyze the test logic

    The test calculates 2 + 3 and checks if it equals 5, which is true.
  2. Step 2: Check for syntax or runtime issues

    The import and usage of deno.test and assertEquals are correct.
  3. Final Answer:

    Test passes with no errors -> Option C
  4. Quick Check:

    Correct assertion means test passes [OK]
Hint: Correct assertEquals means test passes [OK]
Common Mistakes:
  • Assuming test fails when assertion is true
  • Thinking import causes syntax error
  • Confusing runtime errors with correct usage
4. Identify the error in this Deno test code:
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
deno.test("check value", () => {
  const value = 10;
  assertEquals(value, "10");
});
medium
A. Type mismatch causes assertion failure
B. Missing async keyword in test function
C. Incorrect import URL for assertEquals
D. deno.test requires a name and function separately

Solution

  1. Step 1: Check assertion parameters

    The test compares number 10 with string "10", which are not equal.
  2. Step 2: Confirm other syntax correctness

    Import URL and test function syntax are correct; async is not required here.
  3. Final Answer:

    Type mismatch causes assertion failure -> Option A
  4. Quick Check:

    assertEquals fails if types differ [OK]
Hint: assertEquals fails if types differ exactly [OK]
Common Mistakes:
  • Thinking async is always needed
  • Assuming string and number are equal
  • Believing import URL is wrong
5. You want to write a test in Deno that checks if a function getUserName(id) returns a non-empty string for valid IDs. Which test function is best to write?
hard
A. deno.test("user name test", () => { const name = getUserName(1); assertEquals(typeof name, "number"); });
B. deno.test("user name test", () => { const name = getUserName(1); assert(name.length > 0); });
C. deno.test("user name test", () => { const name = getUserName(1); assertEquals(name, ""); });
D. deno.test("user name test", () => { const name = getUserName(1); assertEquals(name, null); });

Solution

  1. Step 1: Understand the test goal

    The test should verify the returned name is a non-empty string.
  2. Step 2: Evaluate each option

    deno.test("user name test", () => { const name = getUserName(1); assert(name.length > 0); }); checks if length > 0, which matches the goal. Options A, B, and D check for number type, empty string, or null, which are incorrect.
  3. Final Answer:

    deno.test("user name test", () => { const name = getUserName(1); assert(name.length > 0); }); -> Option B
  4. Quick Check:

    Check non-empty string with length > 0 [OK]
Hint: Check string length > 0 to confirm non-empty [OK]
Common Mistakes:
  • Checking for empty string instead of non-empty
  • Checking wrong type like number or null
  • Using assertEquals with wrong expected value
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