๐Ÿงช
Websites

Unit Testing: Automatically Checking Your Code with PHPUnit and Jest

08.08.2025
โ† All articles

Almost every developer knows the feeling of making one tiny change and unexpectedly breaking a completely different part of the application. The most reliable way to protect yourself from these surprises is unit testing. A unit test is a small program that automatically verifies the smallest logical piece of your code, usually a single function or method, in isolation from the rest of the system. You feed the function specific inputs and declare in advance what it should return; if the actual result does not match the expected one, the test fails and warns you immediately.

Why bother writing unit tests

The first and most important benefit of unit tests is catching bugs as early as possible. Logical defects that are easy to miss during manual checking are caught by automated tests long before the code reaches production, which cuts the cost of fixing them many times over. The second benefit is confidence in the code: when hundreds of tests glow green, you know for certain that the core mechanisms of the system behave as intended. The third and often most valuable benefit shows up during refactoring, where tests act as a safety net, because if you accidentally break something while rewriting, they turn red instantly and tell you exactly what stopped working.

Core concepts: assertions and the AAA pattern

At the heart of every unit test lies an assertion. An assertion is a claim of the form "I expect this value to be exactly this"; for example, you assert that an addition function, given 2 and 3, returns 5. If the assertion is not satisfied, the test fails. Well-structured tests usually follow the AAA pattern: in the Arrange step you prepare the required data and objects, in the Act step you call the function under test, and in the Assert step you compare the result against the expected value. Clearly separating these three steps makes a test far more readable and much easier to understand months later when you have forgotten the details.

Testing PHP code with PHPUnit

In the PHP world the most widely used testing tool is PHPUnit. It is installed through Composer, and tests are typically kept in a dedicated tests folder. In the example below we look at a simple Calculator class and a test for its add method, where the AAA pattern is especially visible.

<?php
use PHPUnit\Framework\TestCase;

class Calculator {
    public function add(int $a, int $b): int {
        return $a + $b;
    }
}

class CalculatorTest extends TestCase {
    public function testAddReturnsSum(): void {
        // Arrange
        $calc = new Calculator();
        // Act
        $result = $calc->add(2, 3);
        // Assert
        $this->assertSame(5, $result);
    }
}

To run this test you type vendor/bin/phpunit tests/ in the terminal. If everything is fine, PHPUnit shows a green result and the number of passing tests; if there is a defect in the method, it reports in detail which line expected which value and what was actually returned, which dramatically speeds up tracking down the cause.

Testing JavaScript functions with Jest

In frontend and Node.js projects the most popular testing tool is Jest. It stands out for its simple syntax and high speed, and it requires almost no configuration to get started. In the example below we see a JavaScript function and its Jest test; here the describe block groups related tests together, while the test function describes a single case.

// math.js
function add(a, b) {
  return a + b;
}
module.exports = { add };

// math.test.js
const { add } = require('./math');

describe('add function', () => {
  test('adds two positive numbers', () => {
    // Arrange & Act
    const result = add(2, 3);
    // Assert
    expect(result).toBe(5);
  });
});

You run the tests with the npx jest command. Jest automatically discovers every file ending in .test.js, executes them and prints the results with colour highlighting. The expect and toBe pair plays exactly the same role here as an assertion does in PHPUnit, so moving between the two tools feels intuitive.

Mocks: replacing external dependencies

In real projects functions often reach out to a database, an external API or a payment system. Running such dependencies for real in every test would be slow and unreliable, so a mock, or fake, is substituted in their place. A mock is a controlled object that looks like the real dependency but returns predetermined results. For example, by mocking the method that fetches a user from the database, you guarantee that it always returns the same test user; the test then verifies only its own logic and does not depend on the state of an external system. In Jest this is done with jest.fn(), and in PHPUnit with createMock().

Coverage and a brief look at TDD

Code coverage is a metric that shows what percentage of your code lines were executed during the tests. PHPUnit generates such a report with the --coverage-html flag and Jest with the --coverage flag, and both clearly highlight which lines were never tested at all. It is worth stressing that a high coverage percentage does not by itself guarantee quality, but low coverage reliably exposes dangerous untested zones. Test-Driven Development (TDD) is an approach in which you first write a test, watch it fail as expected, then write the minimal code that makes it pass, and only afterwards clean the code up. This cycle forces you to design code that is simple and easy to test right from the start.

When and how to actually write tests

You do not need to test absolutely everything, and trying to is often counterproductive. You should first test functions with complex business logic, many conditions and calculations, because that is exactly where bugs appear most frequently. If you have found and fixed a bug, it is extremely valuable to write a test that reproduces that exact bug, since it guarantees the bug will not silently return in the future. Most importantly, turn writing tests into a habit: if you write a test alongside each new function you create, over time your project develops a dependable safety net and every later change becomes far less stressful.

Related articles

๐ŸŒพ Agriculture and Agribusiness Website: Product Catalog and B2B Sales โค๏ธ Charity Foundation Website: Transparent Fundraising and Donor Trust ๐ŸŽ‰ Wedding Venue and Banquet Hall Website: Event Planning and Online Booking ๐Ÿš™ Car Rental Website: Vehicle Catalog, Price Calculator, and Online Booking
๐ŸŒ Language
๐Ÿ‡บ๐Ÿ‡ฟ O'zbek ๐Ÿ‡บ๐Ÿ‡ฟ ะŽะทะฑะตะบ ๐Ÿ‡ท๐Ÿ‡บ ะ ัƒััะบะธะน ๐Ÿ‡ฌ๐Ÿ‡ง English โœ“