JavaScript - Testing with Jest
Overview
Estimated time: 10–15 minutes
Jest is a popular JavaScript testing framework for unit and integration tests.
Learning Objectives
- Write and run tests using Jest.
- Understand basic Jest features and assertions.
Prerequisites
Jest Example
// sum.js
function sum(a, b) {
return a + b;
}
module.exports = sum;
// sum.test.js
const sum = require('./sum');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
Common Pitfalls
- Tests should be isolated and repeatable.
Summary
Jest makes testing JavaScript code fast and easy. Use it for reliable, maintainable code.