diff --git a/src/index.ts b/src/index.ts index b00ec59..7ead61f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ export function fibonacci(num: number): number | null { if (num < 0) { + console.log(`Error: the number ${num} is a negative value`); return null; } @@ -13,6 +14,6 @@ export function fibonacci(num: number): number | null { num--; } + console.log(`return value is ${b}`); return b; } -console.log("Hello"); \ No newline at end of file diff --git a/tests/index.test.ts b/tests/index.test.ts index 41f4e3b..642df47 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -1,5 +1,23 @@ -import { fibonacci } from "../src/index" +import { fibonacci } from "../src/index"; -it("should return '1' for 1", () => { - expect(fibonacci(1)).toBe(1); -}) +describe("fibonacci ", () => { + const consoleLogSpy = jest.spyOn(console, "log"); + + afterEach(() => { + jest.resetAllMocks(); + }); + + it("should return '1' for 1", () => { + expect(fibonacci(1)).toBe(1); + expect(consoleLogSpy).toHaveBeenCalledTimes(1); + expect(consoleLogSpy).toHaveBeenCalledWith("return value is 1"); + }); + + it("should return null for -1", () => { + expect(fibonacci(-1)).toBe(null); + expect(consoleLogSpy).toHaveBeenCalledTimes(1); + expect(consoleLogSpy).toHaveBeenCalledWith( + "Error: the number -1 is a negative value" + ); + }); +});