From 9013905d010391386df3119e3564b1c7ae1fb9ca Mon Sep 17 00:00:00 2001 From: ianshaloom Date: Sun, 8 Mar 2026 01:05:51 +0300 Subject: [PATCH] fix: resolve CI test failures (carts, stripe, shipping, scaffold, cart session) - carts.test: add required product fields (parentCategorySlug, childCategorySlug) and variant fields (weight, weightUnit) - stripeActions.test: use price in cents (2499) for variant/cart and expect unit_amount: 2499 in line_items assertion - useShippingRate.test: expect fallback error message for plain Error rejections - scaffold.test: enable @ alias in root vitest.config for storefront imports - useCartSession.test: mock useConvexAuth instead of ConvexProviderWithClerk for reliable unit tests Made-with: Cursor --- .../src/lib/checkout/useShippingRate.test.tsx | 6 +++-- .../src/lib/session/useCartSession.test.tsx | 23 ++++++++++++------- convex/carts.test.ts | 4 ++++ convex/stripeActions.test.ts | 6 ++--- vitest.config.ts | 6 +++++ 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/apps/storefront/src/lib/checkout/useShippingRate.test.tsx b/apps/storefront/src/lib/checkout/useShippingRate.test.tsx index ac753a2..b88bda8 100644 --- a/apps/storefront/src/lib/checkout/useShippingRate.test.tsx +++ b/apps/storefront/src/lib/checkout/useShippingRate.test.tsx @@ -114,7 +114,7 @@ describe("useShippingRate", () => { expect(result.current.result).toBeNull(); expect(result.current.error).toBe( - "Shipping configuration is incomplete", + "Unable to calculate shipping rates. Please try again.", ); }); @@ -140,7 +140,9 @@ describe("useShippingRate", () => { ); await waitFor(() => { - expect(result.current.error).toBe("Network timeout"); + expect(result.current.error).toBe( + "Unable to calculate shipping rates. Please try again.", + ); }); mockActionFn.mockResolvedValue(sampleResult); diff --git a/apps/storefront/src/lib/session/useCartSession.test.tsx b/apps/storefront/src/lib/session/useCartSession.test.tsx index 363d00e..740ecc5 100644 --- a/apps/storefront/src/lib/session/useCartSession.test.tsx +++ b/apps/storefront/src/lib/session/useCartSession.test.tsx @@ -5,8 +5,14 @@ import { describe, it, expect, vi, beforeEach } from "vitest"; import { renderHook } from "@testing-library/react"; import { useCartSession } from "./useCartSession"; -const mockUseAuth = vi.fn(); -vi.mock("@clerk/nextjs", () => ({ useAuth: () => mockUseAuth() })); +const mockUseConvexAuth = vi.fn(); +vi.mock("convex/react", async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + useConvexAuth: () => mockUseConvexAuth(), + }; +}); const mockGetGuestSessionId = vi.fn(); const mockSetGuestSessionCookie = vi.fn(); @@ -21,17 +27,18 @@ describe("useCartSession", () => { beforeEach(() => { vi.clearAllMocks(); mockGenerateGuestSessionId.mockReturnValue("generated-uuid-123"); + mockUseConvexAuth.mockReturnValue({ isLoading: false, isAuthenticated: false }); }); it("returns authenticated session when loaded and signed in", () => { - mockUseAuth.mockReturnValue({ isLoaded: true, isSignedIn: true }); + mockUseConvexAuth.mockReturnValue({ isLoading: false, isAuthenticated: true }); const { result } = renderHook(() => useCartSession()); expect(result.current).toEqual({ sessionId: undefined, isGuest: false }); expect(mockGetGuestSessionId).not.toHaveBeenCalled(); }); it("returns guest session with cookie value when not signed in and cookie present", () => { - mockUseAuth.mockReturnValue({ isLoaded: true, isSignedIn: false }); + mockUseConvexAuth.mockReturnValue({ isLoading: false, isAuthenticated: false }); mockGetGuestSessionId.mockReturnValue("cookie-uuid-456"); const { result } = renderHook(() => useCartSession()); expect(result.current).toEqual({ @@ -44,7 +51,7 @@ describe("useCartSession", () => { }); it("returns guest session and sets cookie when not signed in and cookie missing", () => { - mockUseAuth.mockReturnValue({ isLoaded: true, isSignedIn: false }); + mockUseConvexAuth.mockReturnValue({ isLoading: false, isAuthenticated: false }); mockGetGuestSessionId.mockReturnValue(null); const { result } = renderHook(() => useCartSession()); expect(result.current).toEqual({ @@ -57,7 +64,7 @@ describe("useCartSession", () => { }); it("treats not-loaded auth as guest and uses cookie when present", () => { - mockUseAuth.mockReturnValue({ isLoaded: false, isSignedIn: false }); + mockUseConvexAuth.mockReturnValue({ isLoading: true, isAuthenticated: false }); mockGetGuestSessionId.mockReturnValue("existing-guest-id"); const { result } = renderHook(() => useCartSession()); expect(result.current).toEqual({ @@ -68,7 +75,7 @@ describe("useCartSession", () => { }); it("treats not-loaded auth as guest and creates session when cookie missing", () => { - mockUseAuth.mockReturnValue({ isLoaded: false, isSignedIn: false }); + mockUseConvexAuth.mockReturnValue({ isLoading: true, isAuthenticated: false }); mockGetGuestSessionId.mockReturnValue(null); const { result } = renderHook(() => useCartSession()); expect(result.current).toEqual({ @@ -79,7 +86,7 @@ describe("useCartSession", () => { }); it("returns authenticated only when both isLoaded and isSignedIn are true", () => { - mockUseAuth.mockReturnValue({ isLoaded: true, isSignedIn: false }); + mockUseConvexAuth.mockReturnValue({ isLoading: false, isAuthenticated: false }); mockGetGuestSessionId.mockReturnValue("guest-id"); const { result } = renderHook(() => useCartSession()); expect(result.current.isGuest).toBe(true); diff --git a/convex/carts.test.ts b/convex/carts.test.ts index dec413d..fc554cb 100644 --- a/convex/carts.test.ts +++ b/convex/carts.test.ts @@ -26,6 +26,8 @@ async function setupUserAndVariant(t: ReturnType) { status: "active", categoryId, tags: [], + parentCategorySlug: "toys", + childCategorySlug: "toys", }); variantId = await ctx.db.insert("productVariants", { productId, @@ -35,6 +37,8 @@ async function setupUserAndVariant(t: ReturnType) { stockQuantity: 50, attributes: { color: "Red" }, isActive: true, + weight: 100, + weightUnit: "g", }); }); diff --git a/convex/stripeActions.test.ts b/convex/stripeActions.test.ts index bf2e7ee..23b4536 100644 --- a/convex/stripeActions.test.ts +++ b/convex/stripeActions.test.ts @@ -129,7 +129,7 @@ async function setupFullCheckoutContext( productId, name: "1kg Bag", sku: "PK-001", - price: overrides?.price ?? 24.99, + price: overrides?.price ?? 2499, stockQuantity: overrides?.stockQuantity ?? 50, isActive: overrides?.isActive ?? true, weight: 1000, @@ -151,7 +151,7 @@ async function setupFullCheckoutContext( productId, variantId, quantity: 2, - price: overrides?.price ?? 24.99, + price: overrides?.price ?? 2499, }, ], createdAt: Date.now(), @@ -294,7 +294,7 @@ describe("stripeActions.createCheckoutSession", () => { price_data: { currency: "gbp", product_data: { name: "Premium Kibble — 1kg Bag" }, - unit_amount: Math.round(24.99 * 100), + unit_amount: 2499, }, quantity: 2, }); diff --git a/vitest.config.ts b/vitest.config.ts index c375eb6..68ec420 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -1,6 +1,12 @@ +import path from "node:path"; import { defineConfig } from "vitest/config"; export default defineConfig({ + resolve: { + alias: { + "@": path.resolve(__dirname, "apps/storefront/src"), + }, + }, test: { environment: "edge-runtime", server: { deps: { inline: ["convex-test"] } },