Node.js Course
Node.js
/
Beginner

Unit Testing

Definition

A software testing method where individual units or components of a software are tested in total isolation from the rest of the application.

Explain Like I'm New

Testing a single puzzle piece. If you have a function that calculates taxes, a Unit Test only runs that specific function. It intentionally disconnects from the database and the network to prove that the raw math logic works perfectly on its own.

Real World Example

Writing a test for `calculateDiscount(price, code)`. You feed it 100 and 'SAVE20', and expect it to return 80. You don't test if the web server can receive the request, just the math.

Common Use Cases

  • •Validating core business logic
  • •Ensuring pure functions behave correctly

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • Should a Unit Test interact with a real Database?

intermediate

  • What is a 'Pure Function' in relation to unit testing?

Flash Cards

Question

Should it interact with a DB?

Click to reveal answer
Answer

NEVER. If a test touches a database, hard drive, or network, it is no longer a Unit Test (it is an Integration test). Unit tests must run in milliseconds and be completely isolated.

Question

What is a Pure Function?

Click to reveal answer
Answer

A function that always produces the exact same output given the exact same input, and causes no side effects (like modifying global variables). Pure functions are incredibly easy to unit test.