├── .gitignore
├── README.md
├── js
├── calculator.js
├── capitalizeString.js
├── reverseString.js
├── stringLength.js
└── tests
│ ├── calculator.test.js
│ ├── capitalizeString.test.js
│ ├── reverseString.test.js
│ └── stringLength.test.js
├── package-lock.json
└── package.json
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules/
2 | coverage/
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
🧪Jest-Testing🧪
3 |
4 |
5 | I will write a few practical tests for JavaScript functions using the Jest library. I will make sure to follow the AAA pattern to make your tests easier for other developers to read and understand. I will also use the TDD approach in practice.
6 |
7 | ---
8 |
9 | ### Instructions:
10 |
11 | - #### Task 1
12 | > Write a function stringLength(string) that takes any string as an argument and returns its characters count.
13 | > Now write a test for this function.
14 | > Next, expand your function to make it check if the string is at least 1 character long and not longer than 10 characters. Throw errors if those conditions are not met.
15 | > Add tests for the new functionality.
16 | - #### Task 2
17 | > Write a function reverseString(string) function. It should take a string as an argument and return it reversed.
18 | > Write at least one test for this function.
19 | - #### Task 3
20 | > In this task, you will need to write several tests for each tested function. You could write all of your tests directly at the top level, but it's better to group related tests so their output is more readable. Jest has the describe() method just for that. Read about it here and apply it in your tests for this task:
21 | > Write a simple calculator class or object, which will have 4 methods: add, subtract, divide, and multiply.
22 | > Write at least 3 tests for each of the calculator methods.
23 | > Group tests for each method using describe() method.
24 | - #### Task 4
25 |
26 | > In this task we're going to do things differently:
27 |
28 | > Start by writing a test for a capitalize(string) function. Your test should make sure that this function takes a string as an argument and returns that string with the first character capitalized.
29 | > Run your test - it should fail because you don’t have the capitalize(string) function implemented yet.
30 | > Now make your tests green by implementing the capitalize(string) function. Think about what the minimum amount of code is necessary to pass this test and write it.
31 |
32 | ---
33 |
--------------------------------------------------------------------------------
/js/calculator.js:
--------------------------------------------------------------------------------
1 | class Calculator {
2 | isValid(nums) {
3 | return nums.every((num) => typeof num === 'number' && !Number.isNaN(num));
4 | }
5 |
6 | throwError(error = 'invalid input') {
7 | throw new Error(error);
8 | }
9 |
10 | add(...nums) {
11 | if (this.isValid(nums)) {
12 | return nums.reduce((a, b) => a + b, 0);
13 | }
14 | return this.throwError();
15 | }
16 |
17 | substract(...nums) {
18 | if (this.isValid(nums)) {
19 | return nums.reduce((a, b) => a - b);
20 | }
21 | return this.throwError();
22 | }
23 |
24 | divide(num1, num2) {
25 | if (this.isValid([num1, num2])) {
26 | if (num2 > 0) {
27 | return num1 / num2;
28 | }
29 | return this.throwError('Can not divide by 0');
30 | }
31 | return this.throwError();
32 | }
33 |
34 | multiply(num1, num2) {
35 | if (this.isValid([num1, num2])) {
36 | return num1 * num2;
37 | }
38 | return this.throwError();
39 | }
40 | }
41 |
42 | module.exports = Calculator;
43 |
--------------------------------------------------------------------------------
/js/capitalizeString.js:
--------------------------------------------------------------------------------
1 | const capitalizeString = (str) => {
2 | if (typeof str !== 'string') {
3 | throw new Error('Input must be a string');
4 | }
5 |
6 | const splittedString = str.split('');
7 | if (splittedString.length <= 1) {
8 | throw new Error('String must be longer than 1 char');
9 | }
10 |
11 | let [firstChar, ...rest] = splittedString;
12 | firstChar = firstChar.toUpperCase();
13 | const capitalizeString = [firstChar, ...rest].join('');
14 | return capitalizeString;
15 | };
16 |
17 | module.exports = capitalizeString;
18 |
--------------------------------------------------------------------------------
/js/reverseString.js:
--------------------------------------------------------------------------------
1 | const reverseString = (str) => {
2 | const strArray = str.split('');
3 | if (strArray.length <= 1) {
4 | throw new Error('String must be at least two chars long');
5 | }
6 | return strArray.reverse().join('');
7 | };
8 |
9 | module.exports = reverseString;
10 |
--------------------------------------------------------------------------------
/js/stringLength.js:
--------------------------------------------------------------------------------
1 | const stringLength = (str) => {
2 | const strLength = str.split('').length;
3 | if (strLength < 1 || strLength > 10) {
4 | throw new Error('String is too short / long');
5 | }
6 | return strLength;
7 | };
8 |
9 | module.exports = stringLength;
10 |
--------------------------------------------------------------------------------
/js/tests/calculator.test.js:
--------------------------------------------------------------------------------
1 | const Calculator = require('../calculator.js');
2 |
3 | describe('#add method', () => {
4 | test('Should return the sum of two integers', () => {
5 |
6 | const result = new Calculator().add(1, 2);
7 |
8 | expect(result).toBe(3);
9 | });
10 |
11 | test('Should return the sum of more than two numbers', () => {
12 |
13 | const result = new Calculator().add(1, 2, 3, 4, 5, 6);
14 |
15 | expect(result).toBe(21);
16 | });
17 |
18 | test('Should throw an error if input is not a valid number', () => {
19 |
20 | const calculator = new Calculator();
21 |
22 | expect(() => {
23 | calculator.add('im not a number', 5);
24 | }).toThrow('invalid input');
25 | });
26 | });
27 |
28 | describe('#substract method', () => {
29 | test('Should return the substraction of two numbers', () => {
30 |
31 | const result = new Calculator().substract(50, 25);
32 |
33 | expect(result).toBe(25);
34 | });
35 |
36 | test('Should return the substraction of multiple numbers', () => {
37 |
38 | const result = new Calculator().substract(50, 25, 30, 5);
39 |
40 | expect(result).toBe(-10);
41 | });
42 |
43 | test('Should throw error if input is not a valid number', () => {
44 |
45 | const calculator = new Calculator();
46 |
47 | expect(() => {
48 | calculator.substract(10, 'i am not an number');
49 | }).toThrow('invalid input');
50 | });
51 |
52 | test('Should throw error if one of the input is not a valid number', () => {
53 |
54 | const calculator = new Calculator();
55 |
56 | expect(() => {
57 | calculator.substract(20, 10, 'i am not an number', 5);
58 | }).toThrow('invalid input');
59 | });
60 | });
61 |
62 | describe('#divide method', () => {
63 | test('Should return the division between two integers', () => {
64 |
65 | const result = new Calculator().divide(25, 5);
66 |
67 | expect(result).toBe(5);
68 | });
69 |
70 | test('Should throw error if input is not a number', () => {
71 |
72 | const calculator = new Calculator();
73 |
74 | expect(() => {
75 | calculator.divide('25', 5);
76 | }).toThrow('invalid input');
77 | });
78 |
79 | test('Should throw error if second parameter is 0, therefore can not divide by 0', () => {
80 |
81 | const calculator = new Calculator();
82 |
83 | expect(() => {
84 | calculator.divide(25, 0);
85 | }).toThrow('Can not divide by 0');
86 | });
87 | });
88 |
89 | describe('#multiply method', () => {
90 | test('Should return the multiplication of two numbers', () => {
91 |
92 | const result = new Calculator().multiply(5, 5);
93 |
94 | expect(result).toBe(25);
95 | });
96 |
97 | test('Should return the multiplication of negative numbers', () => {
98 |
99 | const result = new Calculator().multiply(-10, -25);
100 |
101 | expect(result).toBe(250);
102 | });
103 |
104 | test('Should throw an error if input is not a valid numbert', () => {
105 |
106 | const calculator = new Calculator();
107 |
108 | expect(() => {
109 | calculator.multiply(2, 'im not a number');
110 | }).toThrow('invalid input');
111 | });
112 | });
113 |
--------------------------------------------------------------------------------
/js/tests/capitalizeString.test.js:
--------------------------------------------------------------------------------
1 | const capitalizeString = require('../capitalizeString.js');
2 |
3 | describe('Capitalize String', () => {
4 | test('Should return a capitalized string', () => {
5 | const result = capitalizeString('arthur');
6 |
7 | expect(result).toBe('Arthur');
8 | });
9 |
10 | test('Should throw error if string length is <= 1', () => {
11 | expect(() => {
12 | capitalizeString('a');
13 | }).toThrow('String must be longer than 1 char');
14 | });
15 |
16 | test('Should throw error if input is not a string', () => {
17 | expect(() => {
18 | capitalizeString(1);
19 | }).toThrow('Input must be a string');
20 | });
21 | });
22 |
--------------------------------------------------------------------------------
/js/tests/reverseString.test.js:
--------------------------------------------------------------------------------
1 | const reverseString = require('../reverseString.js');
2 |
3 | describe('#get string reversed', () => {
4 | test('should return the reversed string', () => {
5 | const reversedString = reverseString('Microverse');
6 |
7 | expect(reversedString).toBe('esrevorciM');
8 | });
9 |
10 | test('should throw error if string length is <= 1', () => {
11 | expect(() => {
12 | reverseString('t');
13 | }).toThrow('String must be at least two chars long');
14 | });
15 | });
16 |
--------------------------------------------------------------------------------
/js/tests/stringLength.test.js:
--------------------------------------------------------------------------------
1 | const stringLength = require('../stringLength.js');
2 |
3 | describe('#get [input] string length count', () => {
4 | test('should return correct count', () => {
5 | const value = stringLength('Testing');
6 |
7 | expect(value).toBe(7);
8 | });
9 |
10 | test('should throw error when count < 1', () => {
11 | expect(() => {
12 | stringLength('');
13 | }).toThrow('String is too short / long');
14 | });
15 |
16 | test('should throw error when count > 10', () => {
17 | expect(() => {
18 | stringLength('This is a long string');
19 | }).toThrow('String is too short / long');
20 | });
21 | });
22 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "jest-testing",
3 | "version": "1.0.0",
4 | "description": "I will write a few practical tests for JavaScript functions using the Jest library. I will make sure to follow the AAA pattern to make your tests easier for other developers to read and understand. I will also use the TDD approach in practice.",
5 | "main": "index.js",
6 | "scripts": {
7 | "test": "jest",
8 | "watch": "jest --watch *.js",
9 | "coverage": "jest --coverage"
10 | },
11 | "repository": {
12 | "type": "git",
13 | "url": "git+https://github.com/ITurres/Jest-Testing.git"
14 | },
15 | "keywords": [],
16 | "author": "",
17 | "license": "ISC",
18 | "bugs": {
19 | "url": "https://github.com/ITurres/Jest-Testing/issues"
20 | },
21 | "homepage": "https://github.com/ITurres/Jest-Testing#readme",
22 | "devDependencies": {
23 | "jest": "^29.5.0"
24 | }
25 | }
26 |
--------------------------------------------------------------------------------