Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
}
Comment on lines +188 to +190
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just assert this is truthy

Suggested change
if (!current.userId) {
throw 'Invalid state';
}
expect(current.userId).toBeTruthy();

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this in order to have typescript narrow down the type of current.has below and make not undefined.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear that's why this logic is here then. Why not just use optional chaining below

const result = current.has?.({ permission: 'test' });


const result = current.has({ permission: 'test' });
expect(result).toBe(false);
expectTypeOf(result).toBeBoolean();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type assertion isn't really needed as you are already asserting that result is strictly false

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted this expectTypeOf(result).toBeBoolean(); which performs a type check. I can simply revert tbh.

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
Loading