From 22ace7083e440039490473c59677edfa442eb87a Mon Sep 17 00:00:00 2001 From: panteliselef Date: Wed, 8 Jan 2025 14:08:05 +0200 Subject: [PATCH] fix(clerk-react,nextjs): Type checking in vitest unit tests (#4844) --- .changeset/afraid-deers-decide.md | 2 ++ .../src/server/__tests__/clerkMiddleware.test.ts | 2 +- packages/nextjs/vitest.config.mts | 5 +++++ packages/react/src/hooks/__tests__/useAuth.test.tsx | 12 +++++++++--- .../{useAuth.types.test.ts => useAuth.type.test.ts} | 3 +-- packages/react/tsconfig.test.json | 4 +++- packages/react/vitest.config.mts | 5 +++++ 7 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 .changeset/afraid-deers-decide.md rename packages/react/src/hooks/__tests__/{useAuth.types.test.ts => useAuth.type.test.ts} (97%) diff --git a/.changeset/afraid-deers-decide.md b/.changeset/afraid-deers-decide.md new file mode 100644 index 00000000000..a845151cc84 --- /dev/null +++ b/.changeset/afraid-deers-decide.md @@ -0,0 +1,2 @@ +--- +--- diff --git a/packages/nextjs/src/server/__tests__/clerkMiddleware.test.ts b/packages/nextjs/src/server/__tests__/clerkMiddleware.test.ts index 9d06d9e10a5..4d0f936a1a3 100644 --- a/packages/nextjs/src/server/__tests__/clerkMiddleware.test.ts +++ b/packages/nextjs/src/server/__tests__/clerkMiddleware.test.ts @@ -591,7 +591,7 @@ describe('clerkMiddleware(params)', () => { describe('debug', () => { beforeEach(() => { - global.console.log.mockClear(); + (global.console.log as ReturnType).mockClear(); }); it('outputs debug logs when used with only params', async () => { diff --git a/packages/nextjs/vitest.config.mts b/packages/nextjs/vitest.config.mts index b28aa67a588..ffe3c21de12 100644 --- a/packages/nextjs/vitest.config.mts +++ b/packages/nextjs/vitest.config.mts @@ -3,6 +3,11 @@ import { defineConfig } from 'vitest/config'; export default defineConfig({ plugins: [], test: { + typecheck: { + enabled: true, + tsconfig: './tsconfig.test.json', + include: ['**/*.{test.ts,test.tsx}'], + }, env: { CLERK_SECRET_KEY: 'TEST_SECRET_KEY', }, diff --git a/packages/react/src/hooks/__tests__/useAuth.test.tsx b/packages/react/src/hooks/__tests__/useAuth.test.tsx index c4272cff3b4..96ecccf535e 100644 --- a/packages/react/src/hooks/__tests__/useAuth.test.tsx +++ b/packages/react/src/hooks/__tests__/useAuth.test.tsx @@ -3,7 +3,7 @@ import { ClerkInstanceContext } from '@clerk/shared/react'; import type { LoadedClerk } from '@clerk/types'; import { render, renderHook } from '@testing-library/react'; import React from 'react'; -import { afterAll, beforeAll, beforeEach, describe, expect, it, test, vi } from 'vitest'; +import { afterAll, beforeAll, beforeEach, describe, expect, expectTypeOf, it, test, vi } from 'vitest'; import { AuthContext } from '../../contexts/AuthContext'; import { errorThrower } from '../../errors/errorThrower'; @@ -175,7 +175,7 @@ describe('useDerivedAuth', () => { }); it('uses provided has function if available', () => { - const mockHas = vi.fn().mockReturnValue('mocked-result'); + const mockHas = vi.fn().mockReturnValue(false); const authObject = { sessionId: 'session123', userId: 'user123', @@ -185,7 +185,13 @@ describe('useDerivedAuth', () => { result: { current }, } = renderHook(() => useDerivedAuth(authObject)); - expect(current.has?.({ permission: 'test' })).toBe('mocked-result'); + if (!current.userId) { + throw 'Invalid state'; + } + + const result = current.has({ permission: 'test' }); + expect(result).toBe(false); + expectTypeOf(result).toBeBoolean(); expect(mockHas).toHaveBeenCalledWith({ permission: 'test' }); }); }); diff --git a/packages/react/src/hooks/__tests__/useAuth.types.test.ts b/packages/react/src/hooks/__tests__/useAuth.type.test.ts similarity index 97% rename from packages/react/src/hooks/__tests__/useAuth.types.test.ts rename to packages/react/src/hooks/__tests__/useAuth.type.test.ts index b7f43577419..6f1b5ad78b8 100644 --- a/packages/react/src/hooks/__tests__/useAuth.types.test.ts +++ b/packages/react/src/hooks/__tests__/useAuth.type.test.ts @@ -1,5 +1,4 @@ -import { expectTypeOf } from 'expect-type'; -import { describe, it } from 'vitest'; +import { describe, expectTypeOf, it } from 'vitest'; import type { useAuth } from '../useAuth'; diff --git a/packages/react/tsconfig.test.json b/packages/react/tsconfig.test.json index db42c689e65..1422309b11e 100644 --- a/packages/react/tsconfig.test.json +++ b/packages/react/tsconfig.test.json @@ -7,5 +7,7 @@ "strict": false, "importHelpers": false, "sourceMap": true - } + }, + "include": ["src/globals.d.ts", "src/**/*.test.ts", "src/**/*.test.tsx"], + "exclude": [] } diff --git a/packages/react/vitest.config.mts b/packages/react/vitest.config.mts index b28aa67a588..5af69ae3bec 100644 --- a/packages/react/vitest.config.mts +++ b/packages/react/vitest.config.mts @@ -3,6 +3,11 @@ import { defineConfig } from 'vitest/config'; export default defineConfig({ plugins: [], test: { + typecheck: { + enabled: true, + tsconfig: './tsconfig.test.json', + include: ['**/*.test.{ts,tsx}'], + }, env: { CLERK_SECRET_KEY: 'TEST_SECRET_KEY', },