From ab10b3903a37a5ddfd734b0f7afcd8de001fe595 Mon Sep 17 00:00:00 2001 From: Liviu Burcusel Date: Mon, 12 Jan 2026 14:22:03 +0100 Subject: [PATCH] Improved code coverage --- tests/shared/utils/auth-client.test.ts | 23 ++++++++++++++++ tests/shared/utils/env.test.ts | 36 ++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 tests/shared/utils/auth-client.test.ts create mode 100644 tests/shared/utils/env.test.ts diff --git a/tests/shared/utils/auth-client.test.ts b/tests/shared/utils/auth-client.test.ts new file mode 100644 index 0000000..2678c11 --- /dev/null +++ b/tests/shared/utils/auth-client.test.ts @@ -0,0 +1,23 @@ +import { describe, expect, it, vi } from "vitest"; + +import { authClient } from "#shared/utils/auth-client"; +import { createAuthClient } from "better-auth/vue"; + +const vars = vi.hoisted(() => ({ + mockAuthClient: { + signIn: vi.fn(), + signUp: vi.fn(), + signOut: vi.fn(), + }, +})); + +vi.mock("better-auth/vue", () => ({ + createAuthClient: vi.fn(() => vars.mockAuthClient), +})); + +describe("auth-client utility", () => { + it("should create and export the auth client", () => { + expect(createAuthClient).toHaveBeenCalled(); + expect(authClient).toBe(vars.mockAuthClient); + }); +}); diff --git a/tests/shared/utils/env.test.ts b/tests/shared/utils/env.test.ts new file mode 100644 index 0000000..a7fcced --- /dev/null +++ b/tests/shared/utils/env.test.ts @@ -0,0 +1,36 @@ +/* eslint-disable node/no-process-env */ +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; +import { z } from "zod"; + +describe("shared/utils/env", () => { + const originalEnv = process.env; + + beforeEach(() => { + vi.resetModules(); + process.env = { ...originalEnv }; + process.env.NODE_ENV = "test"; + process.env.DATABASE_URL = "postgres://localhost:5432/db"; + process.env.BETTER_AUTH_SECRET = "secret"; + process.env.BETTER_AUTH_URL = "http://localhost:3000"; + }); + + afterEach(() => { + process.env = originalEnv; + }); + + it("should validate and export variables when all required variables are present", async () => { + const env = (await import("#shared/utils/env")).default; + + expect(env).toEqual({ + NODE_ENV: "test", + DATABASE_URL: "postgres://localhost:5432/db", + BETTER_AUTH_SECRET: "secret", + BETTER_AUTH_URL: "http://localhost:3000", + }); + }); + + it("should throw an error if NODE_ENV is missing", async () => { + delete process.env.NODE_ENV; + await expect(import("#shared/utils/env")).rejects.toThrow(z.ZodError); + }); +});