-
Notifications
You must be signed in to change notification settings - Fork 409
fix(clerk-react,nextjs): Type checking in vitest unit tests #4844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| --- | ||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted this |
||
| expect(mockHas).toHaveBeenCalledWith({ permission: 'test' }); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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