Challenge - 5 Problems
Master of Writing Test Cases
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Jest test case?
Consider this simple function and its test case. What will be the test result when running Jest?
Node.js
function add(a, b) {
return a + b;
}
test('adds 2 + 3 to equal 5', () => {
expect(add(2, 3)).toBe(5);
});Attempts:
2 left
💡 Hint
Look at what the add function returns and what the test expects.
✗ Incorrect
The add function returns the sum of 2 and 3, which is 5. The test expects 5, so it passes.
📝 Syntax
intermediate2:00remaining
Which Jest test case has a syntax error?
Identify the test case that will cause a syntax error when running Jest.
Attempts:
2 left
💡 Hint
Look for missing brackets or parentheses.
✗ Incorrect
Option A is missing the closing parenthesis and curly brace, causing a syntax error.
🔧 Debug
advanced2:00remaining
Why does this Jest test fail?
Given the following code, why does the test fail?
Node.js
function multiply(a, b) {
return a * b;
}
test('multiply 2 and 3 equals 5', () => {
expect(multiply(2, 3)).toBe(5);
});Attempts:
2 left
💡 Hint
Check the expected value versus the actual function output.
✗ Incorrect
multiply(2, 3) returns 6, but the test expects 5, so it fails.
❓ state_output
advanced2:00remaining
What is the value of 'result' after this Jest test runs?
Consider this test case with a variable 'result'. What is its value after the test completes?
Node.js
let result = 0; function increment() { result += 1; } test('increment increases result', () => { increment(); expect(result).toBe(1); });
Attempts:
2 left
💡 Hint
Look at how the increment function changes result.
✗ Incorrect
The increment function adds 1 to result, so after calling it once, result is 1.
🧠 Conceptual
expert3:00remaining
Which Jest test case correctly tests an async function?
Given an async function fetchData that returns a promise resolving to 'data', which test case correctly waits for the promise to resolve?
Node.js
async function fetchData() { return 'data'; }
Attempts:
2 left
💡 Hint
Think about how to wait for async code in Jest tests.
✗ Incorrect
Option B uses async/await properly to wait for fetchData to resolve before asserting.