# Testing Basics

> Stop verifying your code by manually clicking around the browser. Learn the foundations of automated unit testing and the Arrange-Act-Assert pattern.

Subject: JavaScript | Lesson 61 of 63 | 7 min read
Interactive lesson: https://codewithanimation.in/javascript/testing-basics
Previous: [Debugging & the Console](https://codewithanimation.in/javascript/debugging-and-console.md) | Next: [Data Structures & Big-O](https://codewithanimation.in/javascript/data-structures-and-big-o.md)

## Automated Verification

As your application grows, it becomes impossible to manually test every single feature after every single code change. Automated Testing is the practice of writing code that tests your code.

A **Unit Test** isolates a specific function or component and verifies that it behaves correctly given a set of controlled inputs.

## Fearless Refactoring

Without tests, developers are often terrified to change old code for fear of breaking something unrelated. A robust test suite acts as a safety net.

If you rewrite a function to make it faster, and all your tests still pass, you have mathematical proof that you didn't break the application.

## Arrange, Act, Assert

Almost all tests follow the AAA pattern:

1. **Arrange:** Set up the test data.
2. **Act:** Call the function being tested.
3. **Assert:** Verify the output matches expectations.

```javascript
test('calculates total with tax', () => {
  // 1. Arrange
  const cart = [10, 20];
  const taxRate = 0.1;

  // 2. Act
  const total = calculateTotal(cart, taxRate);

  // 3. Assert
  expect(total).toBe(33);
});
```

## Common mistake: The Flaky Test

A test should be deterministic-meaning if the code hasn't changed, the test should always pass or always fail.

Tests that randomly fail (often due to timing issues, relying on real network requests, or shared global state) are called 'Flaky Tests'. They are worse than having no tests at all because they train developers to ignore failures.

## Tip: Test-Driven Development (TDD)

TDD is a workflow where you write the test *before* you write the code. It follows a strict loop: 1) Write a test that fails (Red). 2) Write the minimum code required to make it pass (Green). 3) Clean up the code (Refactor).

## Try it: The Red-Green Cycle

Examine the testing architecture below. Notice how a broken implementation triggers a loud, specific assertion failure (Red), providing an exact target for the developer to fix and turn the suite Green.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/testing-basics

## Check yourself

### Question 1

What are the three steps of the AAA testing pattern?

- A. Arrange, Act, Assert
- B. Assemble, Analyze, Approve
- C. Assign, Async, Await

Correct answer: A

You Arrange the data, Act on the function, and Assert that the result matches your expectations.

- Why not B: Incorrect. The steps relate to setting up data, running it, and checking the result.
- Why not C: Incorrect. These are related to JavaScript promises and variables, not testing structure.

### Question 2

What is a 'Unit Test'?

- A. A test that isolates and verifies a small, specific piece of code, like a single function.
- B. A test that clicks through the entire application UI automatically.
- C. A test that measures how fast a function runs.

Correct answer: A

Unit tests are the foundation of a test suite. They are fast, highly specific, and test individual 'units' of code in isolation.

- Why not B: Incorrect. That is called an End-to-End (E2E) test.
- Why not C: Incorrect. That is a performance benchmark.

### Question 3

What does the 'Red' step in Test-Driven Development (TDD) represent?

- A. Writing a test for a feature that doesn't exist yet, causing the test runner to fail.
- B. Deleting old code.
- C. Deploying the code to a staging server.

Correct answer: A

In TDD, you must prove the test works by watching it fail first. A test that always passes, even when the code is broken, is useless.

- Why not B: Incorrect. Refactoring happens after the test passes.
- Why not C: Incorrect. TDD is a local development workflow.

## Remember this

- Automated testing allows you to confidently refactor code without fear of breaking existing features.
- The **AAA Pattern** stands for Arrange (setup), Act (execute), and Assert (verify).
- A **Unit Test** focuses on a small, isolated piece of logic (like a function).
- **TDD (Test-Driven Development)** is a workflow consisting of three stages: Red (failing test), Green (passing test), and Refactor.
- A test must be deterministic. Flaky tests that randomly pass/fail destroy trust in the test suite.
