@@ -23,7 +23,6 @@ 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" ) ;
2726
2827// Given an array with a single number
2928// When passed to calculateMean
@@ -48,3 +47,61 @@ test.todo("given [1, 2, 6], returns 3");
4847// Given an array containing a non-number value, e.g. [1, "2", 3]
4948// When passed to calculateMean
5049// Then it should throw Error("calculateMean requires an array of numbers")
50+
51+ test ( "given [1, 2, 6], returns 3" , ( ) => {
52+ expect ( calculateMean ( [ 1 , 2 , 6 ] ) ) . toEqual ( 3 ) ;
53+ } ) ;
54+
55+ test ( "given an array with a single number, returns that number" , ( ) => {
56+ expect ( calculateMean ( [ 7 ] ) ) . toEqual ( 7 ) ;
57+ } ) ;
58+
59+ test ( "given negative numbers, returns the correct mean" , ( ) => {
60+ expect ( calculateMean ( [ - 4 , 2 , - 1 , 3 ] ) ) . toEqual ( 0 ) ;
61+ } ) ;
62+
63+ test ( "given decimal numbers, returns the correct mean" , ( ) => {
64+ expect ( calculateMean ( [ 1.5 , 2.5 , 3.5 ] ) ) . toEqual ( 2.5 ) ;
65+ } ) ;
66+
67+ test ( "throws when given an empty array" , ( ) => {
68+ expect ( ( ) => calculateMean ( [ ] ) ) . toThrow (
69+ new Error ( "calculateMean requires a non-empty array" )
70+ ) ;
71+ } ) ;
72+
73+ test ( "throws when given a string" , ( ) => {
74+ expect ( ( ) => calculateMean ( "banana" ) ) . toThrow (
75+ new Error ( "calculateMean requires an array of numbers" )
76+ ) ;
77+ } ) ;
78+
79+ test ( "throws when given a number" , ( ) => {
80+ expect ( ( ) => calculateMean ( 42 ) ) . toThrow (
81+ new Error ( "calculateMean requires an array of numbers" )
82+ ) ;
83+ } ) ;
84+
85+ test ( "throws when given null" , ( ) => {
86+ expect ( ( ) => calculateMean ( null ) ) . toThrow (
87+ new Error ( "calculateMean requires an array of numbers" )
88+ ) ;
89+ } ) ;
90+
91+ test ( "throws when given an object" , ( ) => {
92+ expect ( ( ) => calculateMean ( { } ) ) . toThrow (
93+ new Error ( "calculateMean requires an array of numbers" )
94+ ) ;
95+ } ) ;
96+
97+ test ( "throws when called with no argument" , ( ) => {
98+ expect ( ( ) => calculateMean ( ) ) . toThrow (
99+ new Error ( "calculateMean requires an array of numbers" )
100+ ) ;
101+ } ) ;
102+
103+ test ( "throws when the array contains a non-number value" , ( ) => {
104+ expect ( ( ) => calculateMean ( [ 1 , "2" , 3 ] ) ) . toThrow (
105+ new Error ( "calculateMean requires an array of numbers" )
106+ ) ;
107+ } ) ;
0 commit comments