Which of the following is the main reason to write tests for your Node.js applications?
Think about what testing helps you find before users do.
Testing helps catch bugs early and confirms that your code behaves as expected, improving reliability.
Consider a Node.js test suite using a testing framework like Jest or Mocha. What is the typical behavior when a test fails?
Think about how testing frameworks help you find all problems in one run.
Most test runners report failures but continue running other tests so you can see all issues at once.
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);
});
});Check if the function sum is declared or imported.
The code calls sum without defining or importing it, causing a ReferenceError.
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);
});
});Consider what fetchData returns and how await works.
The async function resolves with 42 after 100ms, so the assertion passes.
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);
});
});Think about how asynchronous tests signal completion to the test runner.
The test is asynchronous but does not signal completion by returning a promise or calling done, so the runner waits indefinitely.