@@ -22,29 +22,65 @@ const calculateMean = require("./mean.js");
2222// Given an array of numbers
2323// When passed to calculateMean
2424// Then it should return their mean
25- // Delete this test.todo and replace it with a test.
26- test . todo ( "given [1, 2, 6], returns 3" ) ;
25+
26+ test ( "Given an array of numbers, should return their mean" , ( ) => {
27+ expect ( calculateMean ( [ 3 , 7 , 50 , 40 ] ) ) . toEqual ( 25 ) ;
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 an array with a single number, should return that number" , ( ) => {
34+ expect ( calculateMean ( [ 10 ] ) ) . toEqual ( 10 ) ;
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 an array containing negative numbers, should return the correct mean" , ( ) => {
41+ expect ( calculateMean ( [ - 3 , - 50 , - 7 , - 40 ] ) ) . toEqual ( - 25 ) ;
42+ } ) ;
43+
44+ test ( "Given an array containing decimal numbers, should return the correct mean" , ( ) => {
45+ expect ( calculateMean ( [ 2.2 , 1.9 , 3.5 , 4.1 ] ) ) . toEqual ( 2.925 ) ;
46+ } ) ;
47+
48+ test ( "Given an array containing both negative and decimal numbers, should return the correct mean" , ( ) => {
49+ expect ( calculateMean ( [ 2.2 , - 1.9 , 3.5 , - 4.1 ] ) ) . toEqual ( - 0.07499999999999984 ) ;
50+ } ) ;
3551
3652// Given an empty array
3753// When passed to calculateMean
3854// Then it should throw Error("calculateMean requires a non-empty array")
55+ test ( "Throws an error when given an empty array" , ( ) => {
56+ expect ( ( ) => calculateMean ( [ ] ) ) . toThrow (
57+ new Error ( "calculateMean requires a non-empty array" )
58+ ) ;
59+ } ) ;
3960
4061// Given a value that isn't an array, e.g. "banana", 42, null or {}
4162// When passed to calculateMean
4263// Then it should throw Error("calculateMean requires an array of numbers")
64+ test ( "Throws an error when given a value that isn't an array" , ( ) => {
65+ expect ( ( ) => calculateMean ( [ "banana" ] ) ) . toThrow (
66+ new Error ( "calculateMean requires an array of numbers" )
67+ ) ;
68+ } ) ;
4369
4470// Given no argument at all
4571// When passed to calculateMean
4672// Then it should throw Error("calculateMean requires an array of numbers")
73+ test ( "Throws an error when given no argument at all" , ( ) => {
74+ expect ( ( ) => calculateMean ( ) ) . toThrow (
75+ new Error ( "calculateMean requires an array of numbers" )
76+ ) ;
77+ } ) ;
4778
4879// Given an array containing a non-number value, e.g. [1, "2", 3]
4980// When passed to calculateMean
5081// Then it should throw Error("calculateMean requires an array of numbers")
82+ test ( "Throws an error when given an array containing a non-number value" , ( ) => {
83+ expect ( ( ) => calculateMean ( [ 1 , "2" , 3 ] ) ) . toThrow (
84+ new Error ( "calculateMean requires an array of numbers" )
85+ ) ;
86+ } ) ;
0 commit comments