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
5 changes: 5 additions & 0 deletions .changeset/fix-android-optional-native-module.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/expo': patch
---

Fix a crash on Android in Expo Go where rendering `<ClerkProvider>` failed with `Cannot find native module 'ClerkExpo'`, even for JavaScript-only flows that use no native components.
6 changes: 4 additions & 2 deletions packages/expo/src/specs/NativeClerkGoogleSignIn.android.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { requireNativeModule } from 'expo';
import { requireOptionalNativeModule } from 'expo';

type NativeMap = Record<string, unknown>;

Expand All @@ -10,4 +10,6 @@ interface Spec {
signOut(): Promise<void>;
}

export default requireNativeModule<Spec>('ClerkGoogleSignIn');
// Optional so getNativeModule() surfaces its actionable error rather than
// throwing at import time.
export default requireOptionalNativeModule<Spec>('ClerkGoogleSignIn');
5 changes: 3 additions & 2 deletions packages/expo/src/specs/NativeClerkModule.android.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { requireNativeModule } from 'expo';
import { requireOptionalNativeModule } from 'expo';

interface Spec {
// Exposed by Expo Modules EventEmitter for internal native client change events.
Expand All @@ -14,4 +14,5 @@ interface Spec {
): Promise<void>;
}

export default requireNativeModule<Spec>('ClerkExpo');
// Optional so it resolves to null in Expo Go instead of throwing at import time.
export default requireOptionalNativeModule<Spec>('ClerkExpo');
51 changes: 51 additions & 0 deletions packages/expo/src/specs/__tests__/androidSpecs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { beforeEach, describe, expect, test, vi } from 'vitest';

const mocks = vi.hoisted(() => ({
available: {} as Record<string, unknown>,
}));

// Only returns a module when one is registered under the exact name requested.
vi.mock('expo', () => ({
requireNativeModule: (name: string) => {
const nativeModule = mocks.available[name];
if (!nativeModule) {
throw new Error(`Cannot find native module '${name}'`);
}
return nativeModule;
},
requireOptionalNativeModule: (name: string) => mocks.available[name] ?? null,
}));

// Path is a variable because TypeScript cannot resolve the platform suffix in a
// dynamic import.
const importSpec = async (path: string) => (await import(path)).default;

const importSpecs = async () => ({
clerkExpo: await importSpec('../NativeClerkModule.android'),
googleSignIn: await importSpec('../NativeClerkGoogleSignIn.android'),
});

describe('android native module specs', () => {
beforeEach(() => {
vi.resetModules();
mocks.available = {};
});

test('resolve to null instead of throwing when not compiled in (Expo Go)', async () => {
const { clerkExpo, googleSignIn } = await importSpecs();

expect(clerkExpo).toBeNull();
expect(googleSignIn).toBeNull();
});

test('resolve the module registered under the expected name (development build)', async () => {
const clerkExpoModule = { configure: vi.fn() };
const googleSignInModule = { signIn: vi.fn() };
mocks.available = { ClerkExpo: clerkExpoModule, ClerkGoogleSignIn: googleSignInModule };

const { clerkExpo, googleSignIn } = await importSpecs();

expect(clerkExpo).toBe(clerkExpoModule);
expect(googleSignIn).toBe(googleSignInModule);
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
});
Loading