diff --git a/Makefile b/Makefile index 3fcc48b..8060b47 100644 --- a/Makefile +++ b/Makefile @@ -2,4 +2,4 @@ run: deno run --watch --unstable src/index.ts tests: - deno test \ No newline at end of file + deno test --coverage --unstable \ No newline at end of file diff --git a/src/index.test.ts b/src/index.test.ts index 4111cac..66c6250 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -1,5 +1,8 @@ -import { assertStrictEquals } from "https://deno.land/std@0.87.0/testing/asserts.ts"; -import { sayHello } from "./index.ts"; +import { + assertStrictEquals, + assertThrows, +} from "https://deno.land/std@0.87.0/testing/asserts.ts"; +import { sayHello, ValidationError } from "./index.ts"; Deno.test("Hello test", () => { const actual = sayHello(); @@ -7,10 +10,15 @@ Deno.test("Hello test", () => { assertStrictEquals(actual, expected); }); -Deno.test("Random Hello", () => { +Deno.test("Specific Hello", () => { const id = 5; const actual = sayHello(id); const expected = "Bonjour le monde"; assertStrictEquals(actual, expected); }); + +Deno.test("Hello fails for non strictly positive values", () => { + const id = -1; + assertThrows(() => sayHello(id), ValidationError, "Invalid index: -1"); +}); diff --git a/src/index.ts b/src/index.ts index b7bf741..2b1c5a6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,10 @@ +export class ValidationError extends Error {} /** * Display the iconic Hello, World */ export const sayHello = (id?: number) => { + if (id && id < 1) { + throw new ValidationError(`Invalid index: ${id}`); + } return (id === 5) ? "Bonjour le monde" : "Hello, World!"; }; - -console.log(sayHello());