0
0
Node.jsframework~20 mins

Why testing matters in Node.js - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Node.js Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why write tests for your Node.js code?

Which of the following is the main reason to write tests for your Node.js applications?

ATo make the code run faster in production
BTo catch bugs early and ensure the code works as expected
CTo reduce the file size of your application
DTo avoid writing documentation for your code
Attempts:
2 left
💡 Hint

Think about what testing helps you find before users do.

component_behavior
intermediate
2:00remaining
What happens when a test fails in Node.js?

Consider a Node.js test suite using a testing framework like Jest or Mocha. What is the typical behavior when a test fails?

AThe test runner deletes the failing test from the code
BThe test runner stops immediately and does not run other tests
CThe test runner automatically fixes the failing code
DThe test runner reports the failure but continues running other tests
Attempts:
2 left
💡 Hint

Think about how testing frameworks help you find all problems in one run.

📝 Syntax
advanced
2:00remaining
Identify the error in this Node.js test code snippet

What error will this Node.js test code produce?

import assert from 'assert';

describe('sum function', () => {
  it('adds numbers', () => {
    const result = sum(2, 3);
    assert.equal(result, 5);
  });
});
ASyntaxError: Unexpected token
BTypeError: assert.equal is not a function
CReferenceError: sum is not defined
DNo error, test passes
Attempts:
2 left
💡 Hint

Check if the function sum is declared or imported.

state_output
advanced
2:00remaining
What is the output of this asynchronous test in Node.js?

Given this test code using async/await, what will be the test result?

import { strict as assert } from 'assert';

async function fetchData() {
  return new Promise(resolve => setTimeout(() => resolve(42), 100));
}

describe('fetchData', () => {
  it('returns 42', async () => {
    const data = await fetchData();
    assert.equal(data, 42);
  });
});
ATest passes successfully
BSyntaxError due to async function
CTest times out and fails
DTest fails with AssertionError
Attempts:
2 left
💡 Hint

Consider what fetchData returns and how await works.

🔧 Debug
expert
2:00remaining
Why does this test never complete in Node.js?

Examine this test code. Why does the test runner hang and never finish?

describe('delayed test', () => {
  it('waits 1 second', () => {
    setTimeout(() => {
      console.log('done');
    }, 1000);
  });
});
AThe test never calls a done callback or returns a promise, so the runner waits forever
BThe setTimeout duration is too short to complete
Cconsole.log causes the test to hang
DThe test syntax is invalid and causes a syntax error
Attempts:
2 left
💡 Hint

Think about how asynchronous tests signal completion to the test runner.