Skip to content

Commit a8e7794

Browse files
authored
chore(vue): Improve error message when Clerk plugin is not installed (#6719)
1 parent f689d99 commit a8e7794

15 files changed

Lines changed: 72 additions & 13 deletions

.changeset/fuzzy-rivers-tell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@clerk/vue": patch
3+
---
4+
5+
Improved error message when Clerk plugin is not installed

packages/vue/src/components/controlComponents.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const ClerkLoading = defineComponent((_, { slots }) => {
3737
});
3838

3939
export const RedirectToSignIn = defineComponent((props: RedirectOptions) => {
40-
const { sessionCtx, clientCtx } = useClerkContext();
40+
const { sessionCtx, clientCtx } = useClerkContext('RedirectToSignIn');
4141

4242
useClerkLoaded(clerk => {
4343
const hasSignedInSessions = clientCtx.value?.signedInSessions && clientCtx.value.signedInSessions.length > 0;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { render } from '@testing-library/vue';
2+
import { vi } from 'vitest';
3+
import { defineComponent } from 'vue';
4+
5+
import { clerkPlugin } from '../../plugin';
6+
import { useClerkContext } from '../useClerkContext';
7+
8+
describe('useClerkContext', () => {
9+
it('should throw an error if the Clerk plugin is not installed', () => {
10+
// Hide missing injection key warning
11+
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
12+
13+
const Component = defineComponent(() => {
14+
useClerkContext('useAuth');
15+
return () => null;
16+
});
17+
18+
expect(() => render(Component)).toThrow(
19+
'@clerk/vue: useAuth can only be used when the Vue plugin is installed. Learn more: https://clerk.com/docs/references/vue/clerk-plugin',
20+
);
21+
22+
consoleSpy.mockRestore();
23+
});
24+
25+
it('should return the context if the Clerk plugin is installed', () => {
26+
const Component = defineComponent(() => {
27+
useClerkContext('useAuth');
28+
return () => null;
29+
});
30+
31+
expect(() =>
32+
render(Component, {
33+
global: {
34+
plugins: [
35+
[
36+
clerkPlugin,
37+
{
38+
publishableKey: 'pk_xxx',
39+
},
40+
],
41+
],
42+
},
43+
}),
44+
).not.toThrow();
45+
});
46+
});

packages/vue/src/composables/useAuth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ type UseAuth = (options?: PendingSessionOptions) => ToComputedRefs<UseAuthReturn
7474
* </template>
7575
*/
7676
export const useAuth: UseAuth = (options = {}) => {
77-
const { clerk, authCtx } = useClerkContext();
77+
const { clerk, authCtx } = useClerkContext('useAuth');
7878

7979
const getToken: GetToken = createGetToken(clerk);
8080
const signOut: SignOut = createSignOut(clerk);

packages/vue/src/composables/useClerk.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { useClerkContext } from './useClerkContext';
1818
* </template>
1919
*/
2020
export const useClerk = () => {
21-
const { clerk } = useClerkContext();
21+
const { clerk } = useClerkContext('useClerk');
2222

2323
return clerk;
2424
};

packages/vue/src/composables/useClerkContext.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { inject } from 'vue';
22

3+
import { errorThrower } from '../errors/errorThrower';
34
import { ClerkInjectionKey } from '../keys';
45

5-
export function useClerkContext() {
6+
export function useClerkContext(source: string) {
67
const ctx = inject(ClerkInjectionKey);
78

89
if (!ctx) {
9-
throw new Error(
10-
'This component/composable can only be used when the Vue plugin is installed. Learn more: https://clerk.com/docs/quickstarts/vue',
10+
return errorThrower.throw(
11+
`${source} can only be used when the Vue plugin is installed. Learn more: https://clerk.com/docs/references/vue/clerk-plugin`,
1112
);
1213
}
1314

packages/vue/src/composables/useOrganization.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type UseOrganization = () => ToComputedRefs<UseOrganizationReturn>;
5353
* </template>
5454
*/
5555
export const useOrganization: UseOrganization = () => {
56-
const { clerk, organizationCtx } = useClerkContext();
56+
const { clerk, organizationCtx } = useClerkContext('useOrganization');
5757
const { session } = useSession();
5858

5959
const result = computed<UseOrganizationReturn>(() => {

packages/vue/src/composables/useSession.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type UseSession = () => ToComputedRefs<UseSessionReturn>;
3333
* </template>
3434
*/
3535
export const useSession: UseSession = () => {
36-
const { sessionCtx, clerk } = useClerkContext();
36+
const { sessionCtx, clerk } = useClerkContext('useSession');
3737

3838
const result = computed<UseSessionReturn>(() => {
3939
if (sessionCtx.value === undefined) {

packages/vue/src/composables/useSessionList.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type UseSessionList = () => ToComputedRefs<UseSessionListReturn>;
3232
* </template>
3333
*/
3434
export const useSessionList: UseSessionList = () => {
35-
const { clerk, clientCtx } = useClerkContext();
35+
const { clerk, clientCtx } = useClerkContext('useSessionList');
3636

3737
const result = computed<UseSessionListReturn>(() => {
3838
if (!clientCtx.value) {

packages/vue/src/composables/useSignIn.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type UseSignIn = () => ToComputedRefs<UseSignInReturn>;
3030
* </template>
3131
*/
3232
export const useSignIn: UseSignIn = () => {
33-
const { clerk, clientCtx } = useClerkContext();
33+
const { clerk, clientCtx } = useClerkContext('useSignIn');
3434

3535
const unwatch = watch(clerk, value => {
3636
if (value) {

0 commit comments

Comments
 (0)