Testing using Jest

This commit is contained in:
Fahim Ahmed 2023-04-14 01:48:15 +01:00
parent 3e530e3f61
commit 9aef736753
24 changed files with 3946 additions and 2237 deletions

View File

@ -1,5 +1,5 @@
const helloWorld = function() {
return ''
return 'Hello, World!'
};
module.exports = helloWorld;

View File

@ -1,5 +1,13 @@
const repeatString = function() {
const repeatString = function(string, num) {
let str = "";
if (num < 0) return "ERROR";
for (let i = 0; i < num; i++) {
str += string;
}
return str;
};
// Do not edit below this line

View File

@ -4,19 +4,19 @@ describe('repeatString', () => {
test('repeats the string', () => {
expect(repeatString('hey', 3)).toEqual('heyheyhey');
});
test.skip('repeats the string many times', () => {
test('repeats the string many times', () => {
expect(repeatString('hey', 10)).toEqual('heyheyheyheyheyheyheyheyheyhey');
});
test.skip('repeats the string 1 times', () => {
test('repeats the string 1 times', () => {
expect(repeatString('hey', 1)).toEqual('hey');
});
test.skip('repeats the string 0 times', () => {
test('repeats the string 0 times', () => {
expect(repeatString('hey', 0)).toEqual('');
});
test.skip('returns ERROR with negative numbers', () => {
test('returns ERROR with negative numbers', () => {
expect(repeatString('hey', -1)).toEqual('ERROR');
});
test.skip('repeats the string a random amount of times', function () {
test('repeats the string a random amount of times', function () {
/*The number is generated by using Math.random to get a value from between
0 to 1, when this is multiplied by 1000 and rounded down with Math.floor it
equals a number between 0 to 999 (this number will change everytime you run
@ -31,7 +31,7 @@ describe('repeatString', () => {
was randomly generated. */
expect(repeatString('hey', number).match(/((hey))/g).length).toEqual(number);
});
test.skip('works with blank strings', () => {
test('works with blank strings', () => {
expect(repeatString('', 10)).toEqual('');
});
});

View File

@ -1,5 +1,5 @@
const reverseString = function() {
const reverseString = function(string) {
return string.split('').reverse().join('');
};
// Do not edit below this line

View File

@ -5,14 +5,14 @@ describe('reverseString', () => {
expect(reverseString('hello')).toEqual('olleh');
});
test.skip('reverses multiple words', () => {
test('reverses multiple words', () => {
expect(reverseString('hello there')).toEqual('ereht olleh')
})
test.skip('works with numbers and punctuation', () => {
test('works with numbers and punctuation', () => {
expect(reverseString('123! abc!')).toEqual('!cba !321')
})
test.skip('works with blank strings', () => {
test('works with blank strings', () => {
expect(reverseString('')).toEqual('')
})
});

View File

@ -1,5 +1,13 @@
const removeFromArray = function() {
const removeFromArray = function(array, ...args) {
const newArray = [];
array.forEach(element => {
if (!args.includes(element)) {
newArray.push(element);
}
});
return newArray;
};
// Do not edit below this line

View File

@ -4,22 +4,22 @@ describe('removeFromArray', () => {
test('removes a single value', () => {
expect(removeFromArray([1, 2, 3, 4], 3)).toEqual([1, 2, 4]);
});
test.skip('removes multiple values', () => {
test('removes multiple values', () => {
expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]);
});
test.skip('ignores non present values', () => {
test('ignores non present values', () => {
expect(removeFromArray([1, 2, 3, 4], 7, "tacos")).toEqual([1, 2, 3, 4]);
});
test.skip('ignores non present values, but still works', () => {
test('ignores non present values, but still works', () => {
expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]);
});
test.skip('can remove all values', () => {
test('can remove all values', () => {
expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]);
});
test.skip('works with strings', () => {
test('works with strings', () => {
expect(removeFromArray(["hey", 2, 3, "ho"], "hey", 3)).toEqual([2, "ho"]);
});
test.skip('only removes same type', () => {
test('only removes same type', () => {
expect(removeFromArray([1, 2, 3], "1", 3)).toEqual([1, 2]);
});
});

View File

@ -1,5 +1,19 @@
const sumAll = function() {
const sumAll = function(num1, num2) {
let final = 0;
if (num1 < 0 || num2 < 0 || typeof num1 != 'number' || typeof num2 != 'number') {
return "ERROR";
} else if (num2 > num1) {
for (let i = num1; i <= num2; i++) {
final += i;
}
} else if (num1 > num2) {
for (let i = num2; i <= num1; i++) {
final += i;
}
}
return final;
};
// Do not edit below this line

View File

@ -4,19 +4,19 @@ describe('sumAll', () => {
test('sums numbers within the range', () => {
expect(sumAll(1, 4)).toEqual(10);
});
test.skip('works with large numbers', () => {
test('works with large numbers', () => {
expect(sumAll(1, 4000)).toEqual(8002000);
});
test.skip('works with larger number first', () => {
test('works with larger number first', () => {
expect(sumAll(123, 1)).toEqual(7626);
});
test.skip('returns ERROR with negative numbers', () => {
test('returns ERROR with negative numbers', () => {
expect(sumAll(-10, 4)).toEqual('ERROR');
});
test.skip('returns ERROR with non-number parameters', () => {
test('returns ERROR with non-number parameters', () => {
expect(sumAll(10, "90")).toEqual('ERROR');
});
test.skip('returns ERROR with non-number parameters', () => {
test('returns ERROR with non-number parameters', () => {
expect(sumAll(10, [90, 1])).toEqual('ERROR');
});
});

View File

@ -1,5 +1,11 @@
const leapYears = function() {
const leapYears = function(year) {
if (year % 400 == 0) {
return true
} else if (year % 4 == 0 && year % 100 != 0) {
return true
} else {
return false
}
};
// Do not edit below this line

View File

@ -4,19 +4,19 @@ describe('leapYears', () => {
test('works with non century years', () => {
expect(leapYears(1996)).toBe(true);
});
test.skip('works with non century years', () => {
test('works with non century years', () => {
expect(leapYears(1997)).toBe(false);
});
test.skip('works with ridiculously futuristic non century years', () => {
test('works with ridiculously futuristic non century years', () => {
expect(leapYears(34992)).toBe(true);
});
test.skip('works with century years', () => {
test('works with century years', () => {
expect(leapYears(1900)).toBe(false);
});
test.skip('works with century years', () => {
test('works with century years', () => {
expect(leapYears(1600)).toBe(true);
});
test.skip('works with century years', () => {
test('works with century years', () => {
expect(leapYears(700)).toBe(false);
});
});

View File

@ -1,7 +1,9 @@
const convertToCelsius = function() {
const convertToCelsius = function(num) {
return Math.round(((num - 32) * (5 / 9)) * 10) / 10;
};
const convertToFahrenheit = function() {
const convertToFahrenheit = function(num) {
return Math.round((num * (9 / 5) + 32) * 10) / 10;
};
// Do not edit below this line

View File

@ -4,22 +4,22 @@ describe('convertToCelsius', () => {
test('works', () => {
expect(convertToCelsius(32)).toEqual(0);
});
test.skip('rounds to 1 decimal', () => {
test('rounds to 1 decimal', () => {
expect(convertToCelsius(100)).toEqual(37.8);
});
test.skip('works with negatives', () => {
test('works with negatives', () => {
expect(convertToCelsius(-100)).toEqual(-73.3);
});
});
describe('convertToFahrenheit', () => {
test.skip('works', () => {
test('works', () => {
expect(convertToFahrenheit(0)).toEqual(32);
});
test.skip('rounds to 1 decimal', () => {
test('rounds to 1 decimal', () => {
expect(convertToFahrenheit(73.2)).toEqual(163.8);
});
test.skip('works with negatives', () => {
test('works with negatives', () => {
expect(convertToFahrenheit(-10)).toEqual(14);
});
});

View File

@ -1,25 +1,66 @@
const add = function() {
const add = function(num1, num2) {
return num1 + num2
};
const subtract = function() {
const subtract = function(num1, num2) {
return num1 - num2
};
const sum = function() {
const sum = function(array) {
let sums = 0;
if (array.length == 0) {
return sums
} else if (array.length == 1) {
return array[0]
} else {
array.forEach(element => {
sums += element
});
return sums
}
};
const multiply = function() {
const multiply = function(array) {
let value = 1;
if (array.length == 0) {
return 0
} else if (array.length == 1) {
return array[0]
} else {
array.forEach(element => {
value = value * element
});
return value
}
};
const power = function() {
const power = function(num1, num2) {
let value = 1;
if (num2 == 0) { return 1 }
if (num2 == 1) { return num1 }
for (let i = 0; i < num2; i++) {
value = value * num1
}
return value
};
const factorial = function() {
const factorial = function(num) {
let value = 1;
if (num == 1) { return 1 }
for (let i = 1; i <= num; i ++) {
value = value * i
}
return value
};
// Do not edit below this line

View File

@ -5,73 +5,73 @@ describe('add', () => {
expect(calculator.add(0,0)).toBe(0);
});
test.skip('adds 2 and 2', () => {
test('adds 2 and 2', () => {
expect(calculator.add(2,2)).toBe(4);
});
test.skip('adds positive numbers', () => {
test('adds positive numbers', () => {
expect(calculator.add(2,6)).toBe(8);
});
});
describe('subtract', () => {
test.skip('subtracts numbers', () => {
test('subtracts numbers', () => {
expect(calculator.subtract(10,4)).toBe(6);
});
});
describe('sum', () => {
test.skip('computes the sum of an empty array', () => {
test('computes the sum of an empty array', () => {
expect(calculator.sum([])).toBe(0);
});
test.skip('computes the sum of an array of one number', () => {
test('computes the sum of an array of one number', () => {
expect(calculator.sum([7])).toBe(7);
});
test.skip('computes the sum of an array of two numbers', () => {
test('computes the sum of an array of two numbers', () => {
expect(calculator.sum([7,11])).toBe(18);
});
test.skip('computes the sum of an array of many numbers', () => {
test('computes the sum of an array of many numbers', () => {
expect(calculator.sum([1,3,5,7,9])).toBe(25);
});
});
describe('multiply', () => {
test.skip('multiplies two numbers', () => {
test('multiplies two numbers', () => {
expect(calculator.multiply([2,4])).toBe(8);
});
test.skip('multiplies several numbers', () => {
test('multiplies several numbers', () => {
expect(calculator.multiply([2,4,6,8,10,12,14])).toBe(645120);
});
});
describe('power', () => {
test.skip('raises one number to the power of another number', () => {
test('raises one number to the power of another number', () => {
expect(calculator.power(4,3)).toBe(64); // 4 to third power is 64
});
});
describe('factorial', () => {
test.skip('computes the factorial of 0', () => {
test('computes the factorial of 0', () => {
expect(calculator.factorial(0)).toBe(1); // 0! = 1
});
test.skip('computes the factorial of 1', () => {
test('computes the factorial of 1', () => {
expect(calculator.factorial(1)).toBe(1);
});
test.skip('computes the factorial of 2', () => {
test('computes the factorial of 2', () => {
expect(calculator.factorial(2)).toBe(2);
});
test.skip('computes the factorial of 5', () => {
test('computes the factorial of 5', () => {
expect(calculator.factorial(5)).toBe(120);
});
test.skip('computes the factorial of 10', () => {
test('computes the factorial of 10', () => {
expect(calculator.factorial(10)).toBe(3628800);
});
});

View File

@ -1,5 +1,8 @@
const palindromes = function () {
const palindromes = function (string) {
let str = string.toLowerCase().replace(/[^a-z]/g, "");
let reverse = str.split("").reverse().join("");
return str === reverse;
};
// Do not edit below this line

View File

@ -4,22 +4,22 @@ describe('palindromes', () => {
test('works with single words', () => {
expect(palindromes('racecar')).toBe(true);
});
test.skip('works with punctuation ', () => {
test('works with punctuation ', () => {
expect(palindromes('racecar!')).toBe(true);
});
test.skip('works with upper-case letters ', () => {
test('works with upper-case letters ', () => {
expect(palindromes('Racecar!')).toBe(true);
});
test.skip('works with multiple words', () => {
test('works with multiple words', () => {
expect(palindromes('A car, a man, a maraca.')).toBe(true);
});
test.skip('works with multiple words', () => {
test('works with multiple words', () => {
expect(palindromes('Animal loots foliated detail of stool lamina.')).toBe(true);
});
test.skip('doesn\'t just always return true', () => {
test('doesn\'t just always return true', () => {
expect(palindromes('ZZZZ car, a man, a maracaz.')).toBe(false);
});
test.skip('works with numbers in a string', () => {
test('works with numbers in a string', () => {
expect(palindromes('rac3e3car')).toBe(true);
});
});

View File

@ -1,5 +1,21 @@
const fibonacci = function() {
const fibonacci = function(num) {
if (num < 0) { return "OOPS" }
if (num == 0) { return 0 }
if (num == 1) { return 1 }
if (!isNaN(num)) {
let num1 = 0;
let num2 = 1;
let value = 0;
for (let i = 1; i < num; i++) {
value = num1 + num2;
num1 = num2;
num2 = value;
}
return value
}
};
// Do not edit below this line

View File

@ -4,28 +4,28 @@ describe('fibonacci', () => {
test('4th fibonacci number is 3', () => {
expect(fibonacci(4)).toBe(3);
});
test.skip('6th fibonacci number is 8', () => {
test('6th fibonacci number is 8', () => {
expect(fibonacci(6)).toBe(8);
});
test.skip('10th fibonacci number is 55', () => {
test('10th fibonacci number is 55', () => {
expect(fibonacci(10)).toBe(55);
});
test.skip('15th fibonacci number is 610', () => {
test('15th fibonacci number is 610', () => {
expect(fibonacci(15)).toBe(610);
});
test.skip('25th fibonacci number is 75025', () => {
test('25th fibonacci number is 75025', () => {
expect(fibonacci(25)).toBe(75025);
});
test.skip('doesn\'t accept negatives', () => {
test('doesn\'t accept negatives', () => {
expect(fibonacci(-25)).toBe("OOPS");
});
test.skip('DOES accept strings', () => {
test('DOES accept strings', () => {
expect(fibonacci("1")).toBe(1);
});
test.skip('DOES accept strings', () => {
test('DOES accept strings', () => {
expect(fibonacci("2")).toBe(1);
});
test.skip('DOES accept strings', () => {
test('DOES accept strings', () => {
expect(fibonacci("8")).toBe(21);
});
});

View File

@ -1,5 +1,5 @@
const getTheTitles = function() {
const getTheTitles = function(books) {
return books.map((book) => book.title);
};
// Do not edit below this line

View File

@ -1,5 +1,19 @@
const findTheOldest = function() {
const findTheOldest = function(array) {
return array.reduce((oldest, currentPerson) => {
const oldestAge = getAge(oldest.yearOfBirth, oldest.yearOfDeath);
const currentAge = getAge(
currentPerson.yearOfBirth,
currentPerson.yearOfDeath
);
return oldestAge < currentAge ? currentPerson : oldest;
});
};
const getAge = function(birth, death) {
if (!death) {
death = new Date().getFullYear();
}
return death - birth;
};
// Do not edit below this line

View File

@ -21,7 +21,7 @@ describe('findTheOldest', () => {
]
expect(findTheOldest(people).name).toBe('Ray');
});
test.skip('finds the oldest person if someone is still living', () => {
test('finds the oldest person if someone is still living', () => {
const people = [
{
name: "Carly",
@ -40,7 +40,7 @@ describe('findTheOldest', () => {
]
expect(findTheOldest(people).name).toBe('Ray');
});
test.skip('finds the oldest person if the OLDEST is still living', () => {
test('finds the oldest person if the OLDEST is still living', () => {
const people = [
{
name: "Carly",

4575
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -14,11 +14,11 @@
},
"homepage": "https://github.com/TheOdinProject/javascript-exercises#readme",
"devDependencies": {
"jest": "^26.6.3",
"jest-cli": "^26.6.3",
"eslint": "^7.26.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.22.1"
"eslint-plugin-import": "^2.22.1",
"jest": "^26.6.3",
"jest-cli": "^26.6.3"
},
"scripts": {
"test": "jest"