Tillitsdone
down Scroll to discover

Writing Your First Jest Test: A Beginner's Guide

Learn how to write your first JavaScript test using Jest! This beginner-friendly guide walks you through the basics of testing, from setup to best practices, with clear examples.
thumbnail

Writing Your First Jest Test

A modern minimalist workspace corner with floating wooden shelves against a white wall bathed in natural sunlight coming from large windows organized with simple geometric shapes and clean lines. Emerald green indoor plants add life to the scene color palette: crisp white natural wood tones and emerald green accents shot from a 45-degree angle high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

As developers, we all know the importance of testing our code. But if you’re new to testing, getting started can feel overwhelming. Today, I’ll walk you through writing your first test using Jest, one of the most popular testing frameworks for JavaScript.

Why Jest?

Think of Jest as your coding safety net. It’s like having a friend double-check your work, but automated! Facebook maintains Jest, and it comes with everything you need right out of the box. No extra setup, no additional libraries - just clean, simple testing.

A geometric abstract composition featuring interconnected cubes and spheres floating in space representing structure and organization. Colors: warm yellow primary elements with subtle orange highlights against a soft blue background photographed straight on with subtle perspective high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

Setting Up Your First Test

Let’s start with something super simple. Imagine we have a function that adds two numbers together. Basic, right? But it’s perfect for learning the testing fundamentals.

First, create a new file called math.js:

function sum(a, b) {
return a + b;
}
module.exports = sum;

Now, let’s create our test file math.test.js:

const sum = require('./math');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});

An abstract architectural space with clean lines and natural light streaming through geometric openings. Color scheme: natural warm tones with touches of yellow ochre modern concrete textures captured from a low angle perspective looking up high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

Understanding the Test Structure

Breaking down what we just wrote:

  • test() is Jest’s function for creating a test
  • The first argument is a description of what we’re testing
  • The second argument is a function containing our test logic
  • expect() creates an assertion
  • toBe() is a matcher that checks if our result equals the expected value

When you run npm test, Jest will execute this test and tell you if it passed or failed. If it passes, you’ll see a lovely green check mark. If it fails, Jest will tell you exactly what went wrong and where to look.

Best Practices to Remember

  1. Keep your tests simple and focused
  2. Use descriptive test names that explain what you’re testing
  3. Test both successful cases and edge cases
  4. Write your test before writing the actual code (Test-Driven Development)

Remember, testing isn’t about catching every possible bug - it’s about building confidence in your code. Start small, and gradually add more tests as you get comfortable with the process.

A serene landscape composition with layered geometric mountains silhouettes against a bright sky with streaming light rays through clouds. Color palette: vibrant orange mountains transitioning to natural sky blues photographed from a wide landscape perspective high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.