Mastering Jest: Day 2 — Writing Basic Tests

Introduction
Photo by Krishna Pandey on Unsplash
Welcome back to our journey of mastering Jest! Yesterday, we covered the basics of Jest, including installation and writing our first test. Today, we’ll dive deeper into writing and structuring basic test cases. Understanding how to write effective tests is crucial for maintaining code quality and ensuring that your application behaves as expected.
Testing Functions
Testing functions is the cornerstone of any testing strategy. Let’s start by writing tests for simple functions. We’ll use the same sum function from yesterday and add more test cases to cover different scenarios.
// sum.js
function sum(a, b) {
if (typeof a !== 'number' || typeof b !== 'number') {
throw new Error('Arguments must be numbers');
}
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);
});
test('adds -1 + -1 to equal -2', () => {
expect(sum(-1, -1)).toBe(-2);
});
test('throws error when arguments are not numbers', () => {
expect(() => sum(1, 'a')).toThrow('Arguments must be numbers');
});
In these tests, we use Jest’s expect function and various matchers like toBe and toThrow to assert different outcomes.
Matchers
Jest provides a wide range of matchers to help you validate different aspects of your code. Here are a few commonly used matchers:
toBe(value): Checks if a value is equal to another value (uses===).toEqual(value): Checks if a value is equal to another value (checks deeply).toBeTruthy(): Checks if a value is truthy.toBeFalsy(): Checks if a value is falsy.toContain(item): Checks if an array or iterable contains an item.toHaveLength(number): Checks if an array or string has a specific length.
Running Tests
To run your tests, simply use the test script we configured:
npm test
You should see output indicating the results of your tests:
PASS ./sum.test.js
✓ adds 1 + 2 to equal 3 (5ms)
✓ adds -1 + -1 to equal -2
✓ throws error when arguments are not numbers
Test Suites: 1 passed, 1 total
Tests: 3 passed, 3 total
Snapshots: 0 total
Time: 1.242s
Debugging Tests
Debugging failing tests is an essential skill. When a test fails, Jest provides helpful error messages that indicate what went wrong. For example, if we change one of our test cases to fail:
test('adds 1 + 2 to equal 4', () => {
expect(sum(1, 2)).toBe(4);
});
Running the tests will show:
FAIL ./sum.test.js
✕ adds 1 + 2 to equal 4 (5ms)
● adds 1 + 2 to equal 4
expect(received).toBe(expected) // Object.is equality
Expected: 4
Received: 3
4 |
5 | test('adds 1 + 2 to equal 4', () => {
6 | expect(sum(1, 2)).toBe(4);
7 | });
at Object. (sum.test.js:6:19)
The error message clearly indicates that the expected value was 4, but the received value was 3.
Conclusion
Today, we explored writing basic test cases, using matchers, and debugging failing tests. Writing effective tests is a critical skill that helps ensure your code behaves as expected and reduces the likelihood of bugs in your application.
Stay tuned for Day 3, where we’ll dive into testing asynchronous code using Jest. Feel free to share your experiences, ask questions, and discuss any challenges you’ve faced with testing in the comments below!
Engagement
What matchers do you find most useful in Jest? Have you encountered any tricky scenarios while writing tests? Let’s discuss!
Follow me for more updates on mastering Jest and other web development tips!
#JavaScript #Jest #WebDevelopment #Testing #CodeQuality #LearningJourney






