Challenge - 5 Problems
Deno Assertions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediateIdentify the output of this Deno test assertion
What will be the result when running this Deno test code snippet?
Deno
import { assertEquals } from "https://deno.land/std@0.203.0/testing/asserts.ts"; Deno.test("Check sum", () => { const result = 2 + 2; assertEquals(result, 4); });
Attempts:
2 left
💡 Hint
Check what assertEquals compares and the values used.
✗ Incorrect
The assertion compares result which is 4, to the expected value 4. Since they are equal, the test passes without errors.
❓ component_behavior
intermediateWhat happens if assertThrows is used incorrectly?
Consider this Deno test code. What is the outcome when running it?
Deno
import { assertThrows } from "https://deno.land/std@0.203.0/testing/asserts.ts"; Deno.test("Throw test", () => { assertThrows(() => { const x = 5 + 5; }); });
Attempts:
2 left
💡 Hint
assertThrows expects the function to throw an error.
✗ Incorrect
The function inside assertThrows does not throw any error. Therefore, assertThrows fails with an AssertionError.
❓ state_output
advancedWhat is the output of this test using assertMatch?
Given this test code, what will be the test result?
Deno
import { assertMatch } from "https://deno.land/std@0.203.0/testing/asserts.ts"; Deno.test("Regex match test", () => { const message = "Hello Deno!"; assertMatch(message, /^Hello/); });
Attempts:
2 left
💡 Hint
Check if the string matches the regular expression pattern.
✗ Incorrect
The string "Hello Deno!" starts with "Hello", so the regex /^Hello/ matches and the test passes.
🔧 Debug
advancedWhy does this test fail with TypeError?
Analyze this test code and identify why it fails with a TypeError.
Deno
import { assertEquals } from "https://deno.land/std@0.203.0/testing/asserts.ts"; Deno.test("Type error test", () => { const data = null; assertEquals(data.length, 0); });
Attempts:
2 left
💡 Hint
Check the type of data and what properties it has.
✗ Incorrect
Accessing length on null causes a TypeError because null has no properties.
🧠 Conceptual
expertWhich assertion correctly tests that a function throws a specific error message?
You want to test that calling
failFunc() throws an error with message "Invalid input". Which assertion code is correct?Deno
function failFunc() {
throw new Error("Invalid input");
}Attempts:
2 left
💡 Hint
Check the signature of assertThrows for matching error messages.
✗ Incorrect
In Deno, assertThrows accepts a function, an optional error constructor (e.g. Error), and a regex to match the error message. Option B correctly uses the constructor and regex to verify both the error type and message.
