0
0
Node.jsframework~20 mins

Node.js built-in test runner in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Node.js Test Runner Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
});
ARuntimeError because 'assert' module is not imported.
BTest fails with an AssertionError because 2 + 2 is not equal to 4.
CTest passes with no output except a summary line indicating 1 test passed.
DSyntaxError due to missing parentheses in the test function.
Attempts:
2 left
💡 Hint
Check what assert.strictEqual does when the values are equal.
assertion
intermediate
2: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');
}
Aassert.equal(willThrow(), 'fail');
Bassert.doesNotThrow(() => willThrow());
Cassert.strictEqual(willThrow(), Error);
Dassert.throws(() => willThrow(), Error);
Attempts:
2 left
💡 Hint
Use the assertion that expects a function to throw an error.
🔧 Debug
advanced
2: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');
});
Aassert.strictEqual cannot compare strings.
BThe test does not await the async function, so result is a Promise, not 'done'.
CThe async function throws an error that is not caught.
DThe test function must be named differently to support async.
Attempts:
2 left
💡 Hint
Remember what async functions return.
framework
advanced
2: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?
Atest.beforeAll()
Btest.beforeEach()
Ctest.setup()
Dtest.before()
Attempts:
2 left
💡 Hint
Look for the hook that runs once before all tests.
🧠 Conceptual
expert
2: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);
});
AThe report shows 2 tests run: 1 passed, 1 failed with AssertionError details.
BThe report shows 2 tests run: both passed.
CThe report shows 1 test run: the first test only.
DThe report crashes and shows no output.
Attempts:
2 left
💡 Hint
Check how the test runner handles multiple tests with failures.