Skip to content

Commit d42b02b

Browse files
committed
Add tests for calculateMean function to validate various scenarios
1 parent eb74897 commit d42b02b

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,56 @@ const calculateMean = require("./mean.js");
2323
// When passed to calculateMean
2424
// Then it should return their mean
2525
// Delete this test.todo and replace it with a test.
26-
test.todo("given [1, 2, 6], returns 3");
26+
test("given [1, 2, 6], returns 3", () => {
27+
expect(calculateMean([1, 2, 6])).toEqual(3);
28+
});
2729

2830
// Given an array with a single number
2931
// When passed to calculateMean
3032
// Then it should return that number
33+
test("given [1], returns 1", () => {
34+
expect(calculateMean([1])).toEqual(1);
35+
});
3136

3237
// Given an array containing negative or decimal numbers
3338
// When passed to calculateMean
3439
// Then it should return the correct mean
40+
test("given [1, 2, 6], returns 3", () => {
41+
expect(calculateMean([-2, 2.8, 4.6])).toBeCloseTo(1.8);
42+
});
3543

3644
// Given an empty array
3745
// When passed to calculateMean
3846
// Then it should throw Error("calculateMean requires a non-empty array")
47+
test("throws an error when given empty array", () => {
48+
expect(() => {
49+
calculateMean([]);
50+
}).toThrow(new Error("calculateMean requires a non-empty array"));
51+
});
3952

4053
// Given a value that isn't an array, e.g. "banana", 42, null or {}
4154
// When passed to calculateMean
4255
// Then it should throw Error("calculateMean requires an array of numbers")
56+
test("throws an error when not given an array", () => {
57+
expect(() => {
58+
calculateMean("hello");
59+
}).toThrow(new Error("calculateMean requires an array of numbers"));
60+
});
4361

4462
// Given no argument at all
4563
// When passed to calculateMean
4664
// Then it should throw Error("calculateMean requires an array of numbers")
65+
test("throws an error when no argument is given", () => {
66+
expect(() => {
67+
calculateMean();
68+
}).toThrow(new Error("calculateMean requires an array of numbers"));
69+
});
4770

4871
// Given an array containing a non-number value, e.g. [1, "2", 3]
4972
// When passed to calculateMean
5073
// Then it should throw Error("calculateMean requires an array of numbers")
74+
test("throws an error when an array contains non-number values", () => {
75+
expect(() => {
76+
calculateMean([1, "2", 3]);
77+
}).toThrow(new Error("calculateMean requires an array of numbers"));
78+
});

0 commit comments

Comments
 (0)