DEV Community

TSDC Company
TSDC Company

Posted on

Unit Testing in Software Development: How to Test Code Before It Breaks

Writing code is only one part of software development. Making sure that code behaves correctly is equally important.

This is where unit testing becomes valuable.

Unit testing is a software testing technique in which individual functions, methods, or small components are tested independently.

Instead of waiting for users to discover problems, developers can detect many defects during development.

1. What is a unit test?

Imagine an application contains a function that calculates the total price of an order.

function calculateTotal(price, quantity) {
  return price * quantity;
}
Enter fullscreen mode Exit fullscreen mode

For a price of 50 and quantity of 3, the expected result is 150.

A unit test checks whether the function produces that expected result.

2. Writing a simple test with Node.js

Node.js includes a built-in test runner that can be used without installing an external testing framework.

Create a file named calculator.js:

function calculateTotal(price, quantity) {
  return price * quantity;
}

module.exports = { calculateTotal };
Enter fullscreen mode Exit fullscreen mode

Create another file named calculator.test.js:

const test = require("node:test");
const assert = require("node:assert/strict");

const { calculateTotal } = require("./calculator");

test("calculates the total price", () => {
  assert.equal(calculateTotal(50, 3), 150);
});

test("returns zero when quantity is zero", () => {
  assert.equal(calculateTotal(50, 0), 0);
});
Enter fullscreen mode Exit fullscreen mode

Run the tests:

node --test
Enter fullscreen mode Exit fullscreen mode

Node.js will execute the test cases and report whether they passed or failed.

3. What makes a good unit test?

A useful unit test should be:

Independent: It should not rely on unrelated tests running first.

Repeatable: Running it multiple times with the same conditions should produce consistent results.

Focused: It should test a specific behavior rather than many unrelated behaviors.

Understandable: Developers should be able to identify what failed and why.

4. What should developers test?

Consider testing:

  • Normal input and expected output.
  • Empty or missing values.
  • Invalid data.
  • Boundary conditions.
  • Error handling.
  • Important business rules.

For example, a discount calculator should be tested with zero discounts, maximum discounts, invalid percentages, and unexpected input.

5. Unit testing vs. integration testing

These testing approaches serve different purposes.

Unit Testing Integration Testing
Tests individual units Tests interactions between components
Usually isolates dependencies Often uses real or test dependencies
Helps locate specific defects Helps identify connection and workflow problems
Usually faster Can take more time

Both approaches contribute to software quality.

6. Common mistakes

Avoid these testing mistakes:

  • Testing only the successful scenario.
  • Writing tests that simply repeat the implementation.
  • Ignoring edge cases.
  • Making tests depend on execution order.
  • Treating a passing test suite as proof that the entire application is bug-free.

Tests reduce risk, but they cannot guarantee that software has no defects.

Conclusion

Unit testing helps developers build confidence in individual parts of an application.

It also makes future changes safer because existing behavior can be checked automatically.

At Technology Software Development Company (TSDC), we consider testing an important engineering practice for creating maintainable and dependable software.

Discussion: Which JavaScript testing tool do you prefer: Node.js Test Runner, Jest, or Vitest?

SoftwareTesting #UnitTesting #JavaScript #NodeJS #SoftwareEngineering #TSDC

Top comments (0)