Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
83 lines (59 loc) · 2.68 KB
/
Copy pathscript.js
File metadata and controls
83 lines (59 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
Write your implementation of testing framework and assertion. Only after completing this go forward.
Write two tests for the following functions using test framework assertion
Add two number
1. Write a function that takes two input `numA` and `numB` and returns the sum of both numbers.
2. After writing the function write 5 tests for above function with different values
3. Throw an error when the arguments passed in not a number.
4. Make second test fail and see if the
Multiply two numbers
1. Write a function that takes two input `numA` and `numB` and returns the multiplication of both numbers.
2. After writing the function write 5 tests for above function with different values
3. Throw an error when the arguments passed in not a number.
4. Make second test fail and see if the
// Testing frameWork
function test(message, callback) {
try {
callback();
console.log(`✅`, message);
} catch (error) {
console.error(error);
console.log(`❌`, message);
}
};
// Assertion Library
function expect(actual) {
return {
toEqual: function (expected) {
if (actual !== expected) {
throw new Error(`${actual} is not equal to ${expected}`);
}
},
toBeEqual: function (expected) {
if (typeof actual !== expected) {
throw new Error(`${typeof actual} is not equal to ${expected}`);
}
},
};
}
// add function
function add(numA = 0, numB = 0) {
return numA + numB;
};
// Tests for add function
test("Add 5 + 9 to be equal to 14", () => expect(add(5, 9)).toEqual(15));
test("Add 14 + 20 to be equal to 34", () => expect(add(14, 20)).toEqual(34));
test("Add 32 + 18 to be equal to 50", () => expect(add(32, 18)).toEqual(50));
test("Add 57 + 28 to be equal to 85", () => expect(add(57, 28)).toEqual(85));
test("Add 103 + 77 to be equal to 180", () => expect(add(103, 77)).toEqual(180));
test("The type of value to be number", () => expect(add(10, "abc")).toBeEqual("number"));
// multiply function
function multiply(numA = 0, numB = 0) {
return numA * numB;
};
// Tests for add function
test("multiply 5 + 9 to be equal to 45", () => expect(multiply(5, 9)).toEqual(46));
test("multiply 14 * 20 to be equal to 280", () => expect(multiply(14, 20)).toEqual(280));
test("multiply 3 * 18 to be equal to 54", () => expect(multiply(3, 18)).toEqual(54));
test("multiply 7 * 8 to be equal to 56", () => expect(multiply(7, 8)).toEqual(56));
test("multiply 10 * 7 to be equal to 70", () => expect(multiply(10, 7)).toEqual(70));
test("The type of value to be number", () => expect(multiply(10, 0)).toBeEqual("number"));