Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tatilimongi/jest_basics
This repository contains basic JavaScript functions with Jest unit tests, covering operations like addition, subtraction, and string manipulation, along with error handling.
https://github.com/tatilimongi/jest_basics
automation-testing error-handling functions javascript jest tdd-javascript unit-test
Last synced: 22 days ago
JSON representation
This repository contains basic JavaScript functions with Jest unit tests, covering operations like addition, subtraction, and string manipulation, along with error handling.
- Host: GitHub
- URL: https://github.com/tatilimongi/jest_basics
- Owner: tatilimongi
- License: mit
- Created: 2024-09-18T00:25:00.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2024-09-18T00:37:21.000Z (4 months ago)
- Last Synced: 2024-10-25T02:42:10.363Z (2 months ago)
- Topics: automation-testing, error-handling, functions, javascript, jest, tdd-javascript, unit-test
- Language: JavaScript
- Homepage:
- Size: 49.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Functions with Unit Tests using Jest
This repository contains a set of basic JavaScript functions, along with unit tests written using **Jest** to ensure these functions behave as expected.
## File Structure
### 1. `funcoes.js`
This file contains several simple functions that perform basic mathematical operations and string manipulations:
- `somar(x, y)`: Returns the sum of two numbers.
- `obterNumeros()`: Returns an array of predefined numbers.
- `validarNumeroPositivo(num)`: Checks if a number is positive, throwing an error if it's negative.
- `subtrair(x, y)`: Returns the subtraction of two numbers.
- `multiplicar(x, y)`: Returns the multiplication of two numbers.
- `divisao(x, y)`: Returns the division of two numbers, throwing an error if the divisor is zero.
- `ehPar(num)`: Checks if a number is even.
- `contarPalavras(frase)`: Returns the number of words in a string.### 2. `funcoes.test.js`
This file contains the unit tests for the functions above, using the **Jest** framework. Each function is tested with different scenarios to ensure they return the expected results or throw errors appropriately.
## How to Run the Tests
1. Make sure you have **Node.js** installed on your machine.
2. Clone this repository.
3. Install the dependencies (Jest) by running:```bash
npm install
```4. To run the tests, use the command:
```bash
npm test
```## Implemented Tests
The tests cover the following cases:
- Sum of two numbers.
- Verification of an array of numbers returned by the `obterNumeros` function.
- Checking elements within the array.
- Validation of positive numbers and error throwing.
- Comparison of `undefined`, `null`, and boolean (`truthy`, `falsy`) values.
- Numeric comparisons (`greaterThan`, `lessThan`).
- Subtraction, multiplication, and division of numbers.
- Error checking when dividing by zero.
- Checking if a number is even.
- Counting words in a sentence.## Example Tests
```javascript
test("Should correctly add two numbers", () => {
expect(somar(2, 3)).toBe(5);
});test("Should throw an error when trying to divide a number by zero", () => {
expect(() => divisao(5, 0).toThrow("Cannot divide by zero"));
});
```## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## Contributions
Contributions are welcome! Feel free to fork this repository and open a pull request.