0
0
Node.jsframework~20 mins

Assert module for assertions in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Assert Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this assertion code?
Consider the following Node.js code using the assert module. What happens when it runs?
Node.js
import assert from 'assert';

const value = 5;
assert.strictEqual(value, 5);
console.log('Assertion passed');
ALogs nothing and silently fails.
BThrows an AssertionError because values are not strictly equal.
CThrows a ReferenceError because assert is not imported correctly.
DLogs 'Assertion passed' to the console without errors.
Attempts:
2 left
💡 Hint
Check if the values compared by assert.strictEqual are exactly the same.
Predict Output
intermediate
2:00remaining
What error does this assertion code raise?
Look at this Node.js code snippet using the assert module. What error will it throw?
Node.js
import assert from 'assert';

const obj1 = { a: 1 };
const obj2 = { a: 1 };
assert.strictEqual(obj1, obj2);
AThrows AssertionError because obj1 and obj2 are different objects.
BNo error, assertion passes because objects have same properties.
CThrows TypeError because objects cannot be compared.
DThrows ReferenceError because assert is not defined.
Attempts:
2 left
💡 Hint
Remember that strictEqual compares object references, not their content.
component_behavior
advanced
2:00remaining
What happens when assert.throws is used incorrectly?
Given this code using assert.throws, what is the behavior?
Node.js
import assert from 'assert';

function testFunc() {
  return 42;
}

assert.throws(() => testFunc(), Error);
AThrows an AssertionError because testFunc does not throw an error.
BThrows no error and logs 'Test passed'.
CThrows a TypeError because assert.throws expects a promise.
DPasses silently because testFunc returns a value.
Attempts:
2 left
💡 Hint
assert.throws expects the function to throw an error to pass.
📝 Syntax
advanced
2:00remaining
Which option correctly uses assert.deepStrictEqual?
Which code snippet correctly asserts that two objects with nested properties are deeply equal?
Aassert.strictEqual({a: {b: 1}}, {a: {b: 1}});
Bassert.deepStrictEqual({a: {b: 1}}, {a: {b: 1}});
Cassert.deepEqual({a: {b: 1}}, {a: {b: 1}});
Dassert.equal({a: {b: 1}}, {a: {b: 1}});
Attempts:
2 left
💡 Hint
Use deepStrictEqual to compare nested objects for exact equality.
🔧 Debug
expert
2:00remaining
Why does this assertion code fail unexpectedly?
Examine this code snippet. Why does the assertion fail even though the objects look identical?
Node.js
import assert from 'assert';

const arr1 = [1, 2, 3];
const arr2 = [1, 2, '3'];
assert.deepStrictEqual(arr1, arr2);
APasses because values are loosely equal.
BFails because arrays cannot be compared with deepStrictEqual.
CFails because the third element in arr2 is a string, not a number.
DFails because assert.deepStrictEqual is deprecated.
Attempts:
2 left
💡 Hint
Check the data types of each element in the arrays.