We use deno test to check if our code works correctly. It helps find mistakes early.
deno test for testing
import { assertEquals } from "https://deno.land/std@0.203.0/testing/asserts.ts"; deno.test("test name", () => { // test code here assertEquals(actual, expected); });
Use assertEquals to compare actual and expected values.
Each test has a name to describe what it checks.
import { assertEquals } from "https://deno.land/std@0.203.0/testing/asserts.ts"; deno.test("adds numbers", () => { const sum = 1 + 2; assertEquals(sum, 3); });
import { assertEquals } from "https://deno.land/std@0.203.0/testing/asserts.ts"; deno.test("string length", () => { const text = "hello"; assertEquals(text.length, 5); });
This test checks if the greet function returns the right greeting message for the name "Alice".
import { assertEquals } from "https://deno.land/std@0.203.0/testing/asserts.ts"; function greet(name: string): string { return `Hello, ${name}!`; } deno.test("greet returns correct message", () => { const result = greet("Alice"); assertEquals(result, "Hello, Alice!"); });
Run tests by typing deno test in your terminal.
Tests should be small and check one thing at a time.
You can add many tests in one file or split them into multiple files.
deno test helps check if your code works as expected.
Write tests using deno.test and assertions like assertEquals.
Run tests easily with the deno test command.
Practice
deno test in your code?Solution
Step 1: Understand the role of
deno testdeno testis a command used to run tests written in your code to verify correctness.Step 2: Compare with other options
Deploying, formatting, or creating projects are not related to testing functionality.
Final Answer:
To check if your code works as expected by running tests -> Option AQuick Check:
Testing = Check code correctness [OK]
- Confusing testing with deployment
- Thinking
deno testformats code - Assuming it creates projects
Solution
Step 1: Identify correct test syntax in Deno
The correct syntax uses
deno.testas a function with a callback containing assertions.Step 2: Check each option
deno.test(() => { assertEquals(1 + 1, 2); }); correctly uses
deno.test(() => { ... }). Options A, B, and D have syntax errors or incorrect usage.Final Answer:
deno.test(() => { assertEquals(1 + 1, 2); }); -> Option AQuick Check:
Correct test syntax = deno.test(() => { assertEquals(1 + 1, 2); }); [OK]
- Using incorrect keywords like 'test deno'
- Assigning instead of calling deno.test
- Omitting 'deno.' prefix
deno test?
import { assertEquals } from "https://deno.land/std@0.177.0/testing/asserts.ts";
deno.test("sum test", () => {
assertEquals(2 + 2, 4);
});Solution
Step 1: Analyze the test code logic
The test checks if 2 + 2 equals 4 using
assertEquals, which is true.Step 2: Consider syntax and runtime
The import statement is correct, and
deno.testis valid in Deno environment.Final Answer:
Test passes with no errors -> Option CQuick Check:
Correct assertion = Test passes [OK]
- Thinking import causes syntax error
- Confusing assertion failure with success
- Assuming deno.test is undefined
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
deno.test("check", () => {
assertEquals(5, 2 + 2);
});Solution
Step 1: Check the import statement
The import URL lacks a version number, which is required for stable imports in Deno.
Step 2: Verify other parts
The assertion will fail logically, but the question asks for the error in the snippet, and missing version is a syntax/runtime error.
Final Answer:
The import URL is missing the version number -> Option DQuick Check:
Stable imports need version numbers [OK]
- Ignoring missing version in import URL
- Confusing assertion failure with syntax error
- Assuming deno.test needs no name
deno test. Which approach correctly groups tests with descriptive names and runs them all?Solution
Step 1: Understand how to organize multiple tests
Each test should have a unique name and a callback function using
deno.test.Step 2: Evaluate options
Use multiple
deno.testcalls each with a unique name and callback function correctly uses multiple named tests. Write onedeno.testwith multiple assertions but no names lacks test names, making results unclear. Calldeno.testinside a loop without names uses a loop without names, which is not recommended. Usedeno.runTests()to run tests automatically is invalid;deno.runTests()does not exist.Final Answer:
Use multiple deno.test calls each with a unique name and callback function -> Option BQuick Check:
Multiple named tests = Use multipledeno.testcalls each with a unique name and callback function [OK]
- Writing unnamed tests
- Using loops without test names
- Assuming deno.runTests() exists
Ready to prove it?
Pass the test and this pattern moves unknown
