From 00e4631c098e44fb4939efd114a6e466fc7f1635 Mon Sep 17 00:00:00 2001 From: Andrew Haines Date: Mon, 13 Jan 2025 10:22:11 +0000 Subject: [PATCH] chore: Automatically clear mocks between tests Signed-off-by: Andrew Haines --- .../src/matrix-react/cerbos-provider.test.tsx | 21 ++++++++++++------- private/test/src/matrix-react/hooks.test.tsx | 5 ----- private/test/vitest.config.mts | 1 + 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/private/test/src/matrix-react/cerbos-provider.test.tsx b/private/test/src/matrix-react/cerbos-provider.test.tsx index ef8ef7e6..5a8d2b15 100644 --- a/private/test/src/matrix-react/cerbos-provider.test.tsx +++ b/private/test/src/matrix-react/cerbos-provider.test.tsx @@ -80,36 +80,41 @@ function App({ children }: PropsWithChildren): ReactElement { describe("", () => { const container = document.createElement("div"); + let rerender: () => void; - const { rerender } = renderHook(useCerbos, { wrapper: App, container }); it("correctly initializes ClientWithPrincipal", () => { - expect(client.withPrincipal).toHaveBeenLastCalledWith( + ({ rerender } = renderHook(useCerbos, { wrapper: App, container })); + + expect(client.withPrincipal).toHaveBeenCalledOnce(); + expect(client.withPrincipal).toHaveBeenCalledWith( getPrincipal(false), getAuxData(false), ); - expect(client.withPrincipal).toHaveBeenCalledTimes(1); }); it("re-initializes ClientWithPrincipal when principal changes", async () => { await userEvent.click(getByText(container, "switch user")); - expect(client.withPrincipal).toHaveBeenLastCalledWith( + + expect(client.withPrincipal).toHaveBeenCalledOnce(); + expect(client.withPrincipal).toHaveBeenCalledWith( getPrincipal(true), getAuxData(false), ); - expect(client.withPrincipal).toHaveBeenCalledTimes(2); }); it("re-initializes ClientWithPrincipal when auxData changes", async () => { await userEvent.click(getByText(container, "switch aux data")); - expect(client.withPrincipal).toHaveBeenLastCalledWith( + + expect(client.withPrincipal).toHaveBeenCalledOnce(); + expect(client.withPrincipal).toHaveBeenCalledWith( getPrincipal(true), getAuxData(true), ); - expect(client.withPrincipal).toHaveBeenCalledTimes(3); }); it("re-renders without parameter values does not cause re-initialization of ClientWithPrincipal", () => { rerender(); - expect(client.withPrincipal).toHaveBeenCalledTimes(3); + + expect(client.withPrincipal).not.toHaveBeenCalled(); }); }); diff --git a/private/test/src/matrix-react/hooks.test.tsx b/private/test/src/matrix-react/hooks.test.tsx index ef9444f1..65897c77 100644 --- a/private/test/src/matrix-react/hooks.test.tsx +++ b/private/test/src/matrix-react/hooks.test.tsx @@ -7,7 +7,6 @@ import { afterAll, afterEach, beforeAll, - beforeEach, describe, expect, it, @@ -140,10 +139,6 @@ function testCerbosHook( cleanup(); }); - beforeEach(() => { - vi.clearAllMocks(); - }); - function render( client: Pick, ): ReturnType, TParams>> { diff --git a/private/test/vitest.config.mts b/private/test/vitest.config.mts index d28c3424..63a00217 100644 --- a/private/test/vitest.config.mts +++ b/private/test/vitest.config.mts @@ -2,6 +2,7 @@ import { defineConfig } from "vitest/config"; export default defineConfig({ test: { + clearMocks: true, setupFiles: "src/setup.ts", testTimeout: 20000, },