This repository contains a set of basic JavaScript functions, along with unit tests written using Jest to ensure these functions behave as expected.
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.
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.
- Make sure you have Node.js installed on your machine.
- Clone this repository.
- Install the dependencies (Jest) by running:
npm install
- To run the tests, use the command:
npm test
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.
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"));
});
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Feel free to fork this repository and open a pull request.