Unit and Integration Testing
Unit tests verify small pieces of logic in isolation. Integration tests verify interactions between components such as APIs, databases, queues, and external services.
A good test suite balances speed, coverage, and confidence. Not every line requires a unit test; important business behavior and integration boundaries deserve particular attention.
Node.js Test Example
import assert from 'node:assert/strict';
import test from 'node:test';
function add(a, b) {
return a + b;
}
test('adds two numbers', () => {
assert.equal(add(2, 3), 5);
});💡 Node.js includes a built-in test runner that can be used for straightforward unit tests.
Key Takeaways
- Unit tests focus on isolated behavior.
- Integration tests validate collaboration between real components.
- Tests should verify behavior rather than implementation details.
Knowledge Check
What is an integration test primarily concerned with?