|
| 1 | +import { findMean } from '../FindMean.js' |
| 2 | + |
| 3 | +describe('findMean', () => { |
| 4 | + test('findMean([1, 2, 3]) returns 2', () => { |
| 5 | + expect(findMean([1, 2, 3])).toBe(2) |
| 6 | + }) |
| 7 | + |
| 8 | + test('findMean([1, 2, 3, 4]) returns 2.5', () => { |
| 9 | + expect(findMean([1, 2, 3, 4])).toBe(2.5) |
| 10 | + }) |
| 11 | + |
| 12 | + test('findMean([10, 20, 30]) returns 20', () => { |
| 13 | + expect(findMean([10, 20, 30])).toBe(20) |
| 14 | + }) |
| 15 | + |
| 16 | + test('findMean single element returns that element', () => { |
| 17 | + expect(findMean([42])).toBe(42) |
| 18 | + }) |
| 19 | + |
| 20 | + test('findMean works with negative numbers', () => { |
| 21 | + expect(findMean([-3, -1, -2])).toBe(-2) |
| 22 | + }) |
| 23 | + |
| 24 | + test('findMean works with decimals', () => { |
| 25 | + expect(findMean([1.5, 2.5, 3.0])).toBeCloseTo(2.333) |
| 26 | + }) |
| 27 | + |
| 28 | + test('findMean([0, 0, 0]) returns 0', () => { |
| 29 | + expect(findMean([0, 0, 0])).toBe(0) |
| 30 | + }) |
| 31 | + |
| 32 | + test('findMean does not mutate original array', () => { |
| 33 | + const arr = [3, 1, 2] |
| 34 | + findMean(arr) |
| 35 | + expect(arr).toEqual([3, 1, 2]) |
| 36 | + }) |
| 37 | + |
| 38 | + test('throws TypeError for empty array', () => { |
| 39 | + expect(() => findMean([])).toThrow(TypeError) |
| 40 | + }) |
| 41 | + |
| 42 | + test('throws TypeError for non-array input', () => { |
| 43 | + expect(() => findMean('123')).toThrow(TypeError) |
| 44 | + }) |
| 45 | + |
| 46 | + test('throws TypeError for array with non-numbers', () => { |
| 47 | + expect(() => findMean([1, 'two', 3])).toThrow(TypeError) |
| 48 | + }) |
| 49 | +}) |
0 commit comments