Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/edegan-furb/alura-react-tdd

Test-driven development
https://github.com/edegan-furb/alura-react-tdd

jest react recoil testing-library

Last synced: 14 days ago
JSON representation

Test-driven development

Awesome Lists containing this project

README

        

# Test-Driven Development: Testing Your Components

## Implementing Tests with Jest:

Jest is a widely used JavaScript testing framework known for its simplicity and power. Here's a basic example of how you can use it to test a simple function that adds two numbers:

```
// file: sum.js
function sum(a, b) {
return a + b;
}
module.exports = sum;
```
```
// file: sum.test.js
const sum = require('./sum');

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

In this example, test is a function from Jest that defines a test case. expect is a function that allows making assertions about the expected behavior of the code. toBe is an assertion method that checks if the result of the sum function is equal to 3.

### Test-Driven Development (TDD) Technique:

TDD is a software development approach that follows three steps:

1. Write a test: Before writing any production code, write a test that describes the expected behavior of the component or function.
2. Run the test: Execute all existing tests to ensure the new test fails, indicating that the expected behavior has not yet been implemented.
3. Write production code: Write the minimal code necessary for the test to pass. Don't worry about optimization or elegance at this stage.
4. Refactor: After the test passes, you can refactor the code to make it cleaner, more efficient, and more readable.

This cycle is repeated for each new feature or code change.

### Types of Tests:

There are various types of tests that can be applied at different levels of software development:

1. Unit Tests: Test individual units of code, such as functions or methods, isolated from their dependencies. They are quick and easy to write and help identify bugs quickly.
2. Integration Tests: Test how different parts of the system work together. They ensure that components communicate correctly and maintain the integrity of the system as a whole.
3. User Acceptance Tests (UAT): Tests conducted by end-users to ensure that the software meets user requirements and expectations.
4. Regression Tests: Run to ensure that code changes do not introduce new bugs or break existing behavior.

### Benefits of Testing:

1. Early bug identification: Testing helps identify bugs early in the development process, reducing the time and costs associated with fixing bugs later.
2. Living documentation: Tests serve as documentation of the expected behavior of the software, making it easier for other developers to understand the code.
3. Safe refactoring: Tests ensure that changes to the code do not break existing behavior, allowing for safe and confident refactoring.
4. Improved code quality: Writing tests promotes writing modular, decoupled, and testable code, which often results in higher-quality code.