Skip to content

Commit 55d7528

Browse files
Add tests for sum function to validate various input scenarios and error handling
1 parent 59d9646 commit 55d7528

1 file changed

Lines changed: 48 additions & 2 deletions

File tree

‎Sprint-1/implement/sum.test.js‎

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,73 @@ const sum = require("./sum.js");
1010

1111
// Acceptance Criteria:
1212

13+
14+
describe("sum", () => {
1315
// Given an empty array
1416
// When passed to the sum function
1517
// Then it should return 0
16-
test.todo("given an empty array, returns 0")
18+
19+
it("given an empty array, returns 0", () => {
20+
expect(sum([])).toEqual(0);
21+
});
1722

1823
// Given an array with just one number
1924
// When passed to the sum function
2025
// Then it should return that number
2126

27+
it("given an array with just one number, returns that number", () => {
28+
expect(sum([42])).toEqual(42);
29+
});
30+
2231
// Given an array containing negative numbers
2332
// When passed to the sum function
2433
// Then it should still return the correct total sum
2534

35+
it("given an array containing negative numbers, returns the correct total sum", () => {
36+
expect(sum([10, -20, 30])).toEqual(20);
37+
});
38+
2639
// Given an array with decimal/float numbers
2740
// When passed to the sum function
2841
// Then it should return the correct total sum
2942

30-
// Given an array containing a value that isn't a number
43+
it("given an array with decimal/float numbers, returns the correct total sum", () => {
44+
expect(sum([1.5, 2.5, 3.0])).toEqual(7);
45+
});
46+
47+
// Given an array containing a value that isn't a number
3148
// When passed to the sum function
3249
// Then it should throw Error("sum requires an array of numbers")
3350

51+
it("given an array containing a value that isn't a number, throws an error", () => {
52+
expect(() => sum(["hey", 10, "hi", 60, 10])).toThrow(
53+
new Error("sum requires an array of numbers")
54+
);
55+
});
56+
3457
// Given something that isn't an array at all, such as "hey", 42 or no argument
3558
// When passed to the sum function
3659
// Then it should throw Error("sum requires an array of numbers")
60+
61+
it("given something that isn't an array at all, throws an error", () => {
62+
expect(() => sum("hey")).toThrow(
63+
new Error("sum requires an array of numbers")
64+
);
65+
expect(() => sum(42)).toThrow(
66+
new Error("sum requires an array of numbers")
67+
);
68+
expect(() => sum()).toThrow(
69+
new Error("sum requires an array of numbers")
70+
);
71+
})
72+
})
73+
74+
75+
76+
77+
78+
79+
80+
81+
82+

0 commit comments

Comments
 (0)