@@ -23,28 +23,65 @@ 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 an array with a single number, should return that number" , ( ) => {
34+ expect ( calculateMean ( [ 54 ] ) ) . toEqual ( 54 ) ;
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 or decimal numbers, should return the correct mean" , ( ) => {
41+ expect ( calculateMean ( [ - 3 , 3.6 , - 10 , 2.2 ] ) ) . toEqual ( - 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 ( "given an empty array,should throw Error(calculateMean requires a non-empty array)" , ( ) => {
48+ expect ( ( ) => calculateMean ( [ ] ) ) . toThrow (
49+ "calculateMean requires a non-empty array"
50+ ) ;
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 ( "given a value that is not an array,should throw new Error(calculateMean requires an array of numbers)" , ( ) => {
57+ expect ( ( ) => calculateMean ( "banana" ) ) . toThrow (
58+ "calculateMean requires an array of numbers"
59+ ) ;
60+ expect ( ( ) => calculateMean ( 42 ) ) . toThrow (
61+ "calculateMean requires an array of numbers"
62+ ) ;
63+ expect ( ( ) => calculateMean ( null ) ) . toThrow (
64+ "calculateMean requires an array of numbers"
65+ ) ;
66+ expect ( ( ) => calculateMean ( { } ) ) . toThrow (
67+ "calculateMean requires an array of numbers"
68+ ) ;
69+ } ) ;
4370
4471// Given no argument at all
4572// When passed to calculateMean
4673// Then it should throw Error("calculateMean requires an array of numbers")
74+ test ( "given no argument at all, should throw Error(calculateMean requires an array of numbers)" , ( ) => {
75+ expect ( ( ) => calculateMean ( ) ) . toThrow (
76+ "calculateMean requires an array of numbers"
77+ ) ;
78+ } ) ;
4779
4880// Given an array containing a non-number value, e.g. [1, "2", 3]
4981// When passed to calculateMean
5082// Then it should throw Error("calculateMean requires an array of numbers")
83+ test ( "given an array containing a non-number value, should throw Error(calculateMean requires an array of numbers)" , ( ) => {
84+ expect ( ( ) => calculateMean ( [ 1 , 5 , "13" ] ) ) . toThrow (
85+ "calculateMean requires an array of numbers"
86+ ) ;
87+ } ) ;
0 commit comments