Skip to content

Commit

Permalink
fix(clerk-react,nextjs): Type checking in vitest unit tests (#4844)
Browse files Browse the repository at this point in the history
  • Loading branch information
panteliselef authored Jan 8, 2025
1 parent a3de23c commit 22ace70
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .changeset/afraid-deers-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ describe('clerkMiddleware(params)', () => {

describe('debug', () => {
beforeEach(() => {
global.console.log.mockClear();
(global.console.log as ReturnType<typeof vi.fn>).mockClear();
});

it('outputs debug logs when used with only params', async () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/nextjs/vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
Expand Down
12 changes: 9 additions & 3 deletions packages/react/src/hooks/__tests__/useAuth.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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',
Expand All @@ -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' });
});
});
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
4 changes: 3 additions & 1 deletion packages/react/tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
"strict": false,
"importHelpers": false,
"sourceMap": true
}
},
"include": ["src/globals.d.ts", "src/**/*.test.ts", "src/**/*.test.tsx"],
"exclude": []
}
5 changes: 5 additions & 0 deletions packages/react/vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
Expand Down

0 comments on commit 22ace70

Please sign in to comment.