From e7b2ca69ee736e511fa9b3fb2a50912696fcf298 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Tue, 7 May 2024 15:54:29 +1000 Subject: [PATCH] fix(collections): correct error class when `chunk()` throws (#4682) refactor(collections): correct error class when `chunk()` throws --- collections/chunk.ts | 2 +- collections/chunk_test.ts | 30 +++++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/collections/chunk.ts b/collections/chunk.ts index 95a075a1e..3e22ba51c 100644 --- a/collections/chunk.ts +++ b/collections/chunk.ts @@ -39,7 +39,7 @@ */ export function chunk(array: readonly T[], size: number): T[][] { if (size <= 0 || !Number.isInteger(size)) { - throw new Error( + throw new RangeError( `Expected size to be an integer greater than 0 but found ${size}`, ); } diff --git a/collections/chunk_test.ts b/collections/chunk_test.ts index 786d0128b..d33bf35ca 100644 --- a/collections/chunk_test.ts +++ b/collections/chunk_test.ts @@ -27,11 +27,31 @@ Deno.test({ Deno.test({ name: "chunk() throws on non naturals", fn() { - assertThrows(() => chunk([], +.5)); - assertThrows(() => chunk([], -4.7)); - assertThrows(() => chunk([], -2)); - assertThrows(() => chunk([], +0)); - assertThrows(() => chunk([], -0)); + assertThrows( + () => chunk([], +.5), + RangeError, + "Expected size to be an integer greater than 0 but found 0.5", + ); + assertThrows( + () => chunk([], -4.7), + RangeError, + "Expected size to be an integer greater than 0 but found -4.7", + ); + assertThrows( + () => chunk([], -2), + RangeError, + "Expected size to be an integer greater than 0 but found -2", + ); + assertThrows( + () => chunk([], +0), + RangeError, + "Expected size to be an integer greater than 0 but found 0", + ); + assertThrows( + () => chunk([], -0), + RangeError, + "Expected size to be an integer greater than 0 but found 0", + ); }, });