Junior — Middle
Can you explain what is meant by modular testing of code?
sobes.tech AI
Answer from AI
Unit testing is the process of verifying individual parts (modules) of a program for correctness. In the context of Node.js, this usually means writing tests for individual functions or classes to ensure they perform their tasks correctly regardless of the rest of the system.
For example, if you have a function that adds two numbers, a unit test will check that it returns the correct result for different input data.
An example in JavaScript using Jest:
function sum(a, b) {
return a + b;
}
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
Thus, unit tests help quickly identify errors and simplify code maintenance.