Challenge - 5 Problems
Node.js Test Runner Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple Node.js test
What is the output when running this Node.js test code using the built-in test runner?
Node.js
import test from 'node:test'; import assert from 'node:assert'; test('simple equality test', () => { assert.strictEqual(2 + 2, 4); });
Attempts:
2 left
💡 Hint
Check what assert.strictEqual does when the values are equal.
✗ Incorrect
The test checks if 2 + 2 equals 4, which is true, so the assertion passes and the test runner reports success.
❓ assertion
intermediate2:00remaining
Choosing the correct assertion for error testing
Which assertion correctly tests that a function throws an error using Node.js built-in test runner?
Node.js
function willThrow() {
throw new Error('fail');
}Attempts:
2 left
💡 Hint
Use the assertion that expects a function to throw an error.
✗ Incorrect
assert.throws expects a function that throws an error; it passes if the error is thrown.
🔧 Debug
advanced2:00remaining
Debugging a failing test with async code
This test is supposed to check if an async function returns 'done', but it fails. What is the cause?
Node.js
import test from 'node:test'; import assert from 'node:assert'; async function asyncFunc() { return 'done'; } test('async test', () => { const result = asyncFunc(); assert.strictEqual(result, 'done'); });
Attempts:
2 left
💡 Hint
Remember what async functions return.
✗ Incorrect
asyncFunc returns a Promise; without await, result is a Promise, so assertion fails.
❓ framework
advanced2:00remaining
Understanding test hooks in Node.js built-in test runner
Which hook runs once before all tests in a file when using Node.js built-in test runner?
Attempts:
2 left
💡 Hint
Look for the hook that runs once before all tests.
✗ Incorrect
test.beforeAll() runs once before all tests; test.beforeEach() runs before each test.
🧠 Conceptual
expert2:00remaining
Test report output for multiple tests with failures
Given this test file with two tests, one passing and one failing, what will the built-in test runner report?
Node.js
import test from 'node:test'; import assert from 'node:assert'; test('test 1', () => { assert.strictEqual(1, 1); }); test('test 2', () => { assert.strictEqual(1, 2); });
Attempts:
2 left
💡 Hint
Check how the test runner handles multiple tests with failures.
✗ Incorrect
The runner executes both tests, reports one pass and one failure with error details.