Bird
Raised Fist0
Node.jsframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the assert module in Node.js?
easy
A. To check if values meet expected conditions and throw errors if not
B. To handle HTTP requests and responses
C. To manage file system operations
D. To create and manage database connections

Solution

  1. Step 1: Understand the role of the assert module

    The assert module is designed to verify conditions in code and throw errors when conditions fail.
  2. Step 2: Compare with other options

    Options B, C, and D relate to other Node.js modules like http, fs, and database libraries, not assert.
  3. Final Answer:

    To check if values meet expected conditions and throw errors if not -> Option A
  4. Quick Check:

    assert module = condition checks [OK]
Hint: Assert checks conditions and stops code if false [OK]
Common Mistakes:
  • Confusing assert with modules for HTTP or file operations
  • Thinking assert manages asynchronous events
  • Assuming assert modifies data instead of checking it
2. Which of the following is the correct way to import the assert module in Node.js?
easy
A. const assert = require('assert');
B. import assert from 'assert';
C. const assert = import('assert');
D. require assert from 'assert';

Solution

  1. Step 1: Recall Node.js CommonJS import syntax

    Node.js traditionally uses const module = require('module') to import modules.
  2. Step 2: Check each option

    const assert = require('assert'); uses correct CommonJS syntax. import assert from 'assert'; is ES module syntax, which requires special setup. Options A and C are invalid syntax.
  3. Final Answer:

    const assert = require('assert'); -> Option A
  4. Quick Check:

    CommonJS import = require() [OK]
Hint: Use require() to import assert in Node.js [OK]
Common Mistakes:
  • Using ES module import without enabling it
  • Mixing import and require syntax
  • Writing invalid require statements
3. What will be the output of the following code snippet?
const assert = require('assert');
assert.strictEqual(5, '5');
console.log('Test passed');
medium
A. Test passed
B. TypeError: Cannot compare number and string
C. No output, program crashes silently
D. AssertionError: Expected values to be strictly equal

Solution

  1. Step 1: Understand assert.strictEqual behavior

    assert.strictEqual checks if two values are strictly equal (===). Number 5 and string '5' are not strictly equal.
  2. Step 2: Predict the result

    Since the values differ in type, assert.strictEqual throws an AssertionError and stops execution before console.log.
  3. Final Answer:

    AssertionError: Expected values to be strictly equal -> Option D
  4. Quick Check:

    strictEqual fails if types differ [OK]
Hint: strictEqual needs same type and value [OK]
Common Mistakes:
  • Thinking '5' and 5 are equal with strictEqual
  • Expecting console.log to run after failed assert
  • Confusing strictEqual with loose equality
4. Identify the error in the following code using the assert module:
const assert = require('assert');
assert.equal(10, 10);
assert.equal(5, '5');
assert.strictEqual(3, 3);
assert.strictEqual(4, '4');
medium
A. assert.equal(5, '5') will throw an error
B. assert.strictEqual(4, '4') will throw an error
C. assert.equal(10, 10) will throw an error
D. No errors in the code

Solution

  1. Step 1: Review assert.equal vs assert.strictEqual

    assert.equal uses loose equality (==), so 5 and '5' are equal. assert.strictEqual uses strict equality (===), so 4 and '4' differ in type.
  2. Step 2: Identify which assertion fails

    assert.strictEqual(4, '4') fails and throws an AssertionError. Others pass without error.
  3. Final Answer:

    assert.strictEqual(4, '4') will throw an error -> Option B
  4. Quick Check:

    strictEqual fails on type mismatch [OK]
Hint: strictEqual fails if types differ, equal allows type coercion [OK]
Common Mistakes:
  • Assuming assert.equal throws error on 5 and '5'
  • Thinking assert.strictEqual allows type coercion
  • Ignoring that assert.equal passes with loose equality
5. You want to verify that a function getUserName() returns a non-empty string. Which assert statement correctly checks this condition?
hard
A. assert.strictEqual(typeof getUserName(), 'string'); assert.ok(getUserName().length > 0);
B. assert.equal(getUserName(), 'string'); assert.ok(getUserName() !== '');
C. assert.ok(typeof getUserName() === 'string' && getUserName().length > 0);
D. assert.strictEqual(getUserName().length, 'string');

Solution

  1. Step 1: Understand the condition to check

    The function must return a string type and the string must not be empty (length > 0).
  2. Step 2: Evaluate each option

    assert.strictEqual(typeof getUserName(), 'string'); assert.ok(getUserName().length > 0); calls getUserName() twice, which may differ and is less efficient. assert.equal(getUserName(), 'string'); assert.ok(getUserName() !== ''); incorrectly compares value to string 'string'. assert.strictEqual(getUserName().length, 'string'); compares length to string 'string', which is invalid. assert.ok(typeof getUserName() === 'string' && getUserName().length > 0); correctly checks both conditions in one assert.ok call.
  3. Final Answer:

    assert.ok(typeof getUserName() === 'string' && getUserName().length > 0); -> Option C
  4. Quick Check:

    Use assert.ok with combined condition [OK]
Hint: Combine type and length check in one assert.ok [OK]
Common Mistakes:
  • Comparing value to string 'string' instead of typeof
  • Calling function multiple times causing inconsistent results
  • Comparing length to a string instead of a number
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard