-
Notifications
You must be signed in to change notification settings - Fork 237
fix(react-native): expo-file-system detection broken on Expo SDK 54 stable #3156
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'posthog-react-native': patch | ||
| --- | ||
|
|
||
| fix: expo-file-system detection broken on Expo SDK 54 stable |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // Test for Expo SDK 54 stable where legacy methods exist but throw deprecation errors | ||
| // and the new File/Paths API should be used instead. | ||
| // See https://github.com/PostHog/posthog-js/issues/3151 | ||
|
|
||
| const mockFileWrite = jest.fn() | ||
| const mockFileText = jest.fn().mockResolvedValue('stored-value') | ||
| const mockDocument = { uri: 'file:///mock-doc-dir/' } | ||
|
|
||
| // Mock expo-file-system as it appears in SDK 54 stable: | ||
| // - Legacy methods exist but throw deprecation errors | ||
| // - New File/Paths API is available | ||
| jest.mock('../src/optional/OptionalExpoFileSystem', () => ({ | ||
| OptionalExpoFileSystem: { | ||
| // Legacy methods that throw deprecation errors (SDK 54 stable behavior) | ||
| readAsStringAsync: () => { | ||
| throw new Error('Method readAsStringAsync imported from "expo-file-system" is deprecated') | ||
| }, | ||
| writeAsStringAsync: () => { | ||
| throw new Error('Method writeAsStringAsync imported from "expo-file-system" is deprecated') | ||
| }, | ||
| documentDirectory: '/mock-doc-dir/', | ||
| // New API (SDK 54+) | ||
| Paths: { | ||
| document: mockDocument, | ||
| }, | ||
| // File constructor accepts (Directory, ...strings) and joins them | ||
| File: jest.fn().mockImplementation((_dir: any, _key: string) => ({ | ||
| text: mockFileText, | ||
| write: mockFileWrite, | ||
| })), | ||
| }, | ||
| })) | ||
|
|
||
| jest.mock('../src/optional/OptionalExpoFileSystemLegacy', () => ({ | ||
| OptionalExpoFileSystemLegacy: undefined, | ||
| })) | ||
|
|
||
| jest.mock('../src/optional/OptionalAsyncStorage', () => ({ | ||
| OptionalAsyncStorage: undefined, | ||
| })) | ||
|
|
||
| jest.mock('react-native', () => ({ | ||
| Platform: { OS: 'ios' }, | ||
| })) | ||
|
|
||
| import { buildOptimisticAsyncStorage } from '../src/native-deps' | ||
| import { OptionalExpoFileSystem } from '../src/optional/OptionalExpoFileSystem' | ||
|
|
||
| describe('Expo SDK 54 stable - new File API detection', () => { | ||
| jest.useRealTimers() | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks() | ||
| mockFileText.mockResolvedValue('stored-value') | ||
| }) | ||
|
|
||
| it('should use new File/Paths API when both new and deprecated legacy APIs are present', () => { | ||
| const storage = buildOptimisticAsyncStorage() | ||
| expect(storage).toBeDefined() | ||
| }) | ||
|
|
||
| it('should pass Paths.document directory and key to File constructor', async () => { | ||
| const storage = buildOptimisticAsyncStorage()! | ||
|
|
||
| const result = await storage.getItem('test-key') | ||
| expect(result).toBe('stored-value') | ||
| // File should be constructed with (Paths.document, key) per expo docs | ||
| expect((OptionalExpoFileSystem as any).File).toHaveBeenCalledWith(mockDocument, 'test-key') | ||
| }) | ||
|
|
||
| it('should use File.write for setItem', () => { | ||
| const storage = buildOptimisticAsyncStorage()! | ||
|
|
||
| storage.setItem('test-key', 'test-value') | ||
| expect((OptionalExpoFileSystem as any).File).toHaveBeenCalledWith(mockDocument, 'test-key') | ||
| expect(mockFileWrite).toHaveBeenCalledWith('test-value') | ||
| }) | ||
|
|
||
| it('should return null when getItem fails', async () => { | ||
| mockFileText.mockRejectedValue(new Error('File not found')) | ||
|
|
||
| const storage = buildOptimisticAsyncStorage()! | ||
| const result = await storage.getItem('nonexistent-key') | ||
| expect(result).toBeNull() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| // Test for Expo SDK 55 where both the new File/Paths API and a working legacy subpath exist. | ||
| // The new API should always be preferred over the legacy subpath. | ||
|
|
||
| const mockFileWrite = jest.fn() | ||
| const mockFileText = jest.fn().mockResolvedValue('stored-value') | ||
| const mockDocument = { uri: 'file:///mock-doc-dir/' } | ||
|
|
||
| const mockLegacyReadAsStringAsync = jest.fn() | ||
| const mockLegacyWriteAsStringAsync = jest.fn() | ||
|
|
||
| // Mock expo-file-system main module (SDK 55): has new File/Paths API + deprecated legacy stubs | ||
| jest.mock('../src/optional/OptionalExpoFileSystem', () => ({ | ||
| OptionalExpoFileSystem: { | ||
| // Deprecated legacy methods that throw at runtime | ||
| readAsStringAsync: () => { | ||
| throw new Error('Method readAsStringAsync imported from "expo-file-system" is deprecated') | ||
| }, | ||
| writeAsStringAsync: () => { | ||
| throw new Error('Method writeAsStringAsync imported from "expo-file-system" is deprecated') | ||
| }, | ||
| documentDirectory: '/mock-doc-dir/', | ||
| // New API | ||
| Paths: { | ||
| document: mockDocument, | ||
| }, | ||
| File: jest.fn().mockImplementation((_dir: any, _key: string) => ({ | ||
| text: mockFileText, | ||
| write: mockFileWrite, | ||
| })), | ||
| }, | ||
| })) | ||
|
|
||
| // Mock expo-file-system/legacy subpath (SDK 55): has working legacy methods | ||
| jest.mock('../src/optional/OptionalExpoFileSystemLegacy', () => ({ | ||
| OptionalExpoFileSystemLegacy: { | ||
| readAsStringAsync: mockLegacyReadAsStringAsync, | ||
| writeAsStringAsync: mockLegacyWriteAsStringAsync, | ||
| documentDirectory: '/mock-legacy-doc-dir/', | ||
| }, | ||
| })) | ||
|
|
||
| jest.mock('../src/optional/OptionalAsyncStorage', () => ({ | ||
| OptionalAsyncStorage: undefined, | ||
| })) | ||
|
|
||
| jest.mock('react-native', () => ({ | ||
| Platform: { OS: 'ios' }, | ||
| })) | ||
|
|
||
| import { buildOptimisticAsyncStorage } from '../src/native-deps' | ||
| import { OptionalExpoFileSystem } from '../src/optional/OptionalExpoFileSystem' | ||
|
|
||
| describe('Expo SDK 55 - prefers new File API over working legacy subpath', () => { | ||
| jest.useRealTimers() | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks() | ||
| mockFileText.mockResolvedValue('stored-value') | ||
| }) | ||
|
|
||
| it('should use new File/Paths API even when legacy subpath is available', () => { | ||
| const storage = buildOptimisticAsyncStorage() | ||
| expect(storage).toBeDefined() | ||
|
|
||
| // Verify it uses new API | ||
| storage!.setItem('test-key', 'test-value') | ||
| expect((OptionalExpoFileSystem as any).File).toHaveBeenCalledWith(mockDocument, 'test-key') | ||
| expect(mockFileWrite).toHaveBeenCalledWith('test-value') | ||
|
|
||
| // Legacy methods should NOT be called | ||
| expect(mockLegacyReadAsStringAsync).not.toHaveBeenCalled() | ||
| expect(mockLegacyWriteAsStringAsync).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('should read using new File API, not legacy', async () => { | ||
| const storage = buildOptimisticAsyncStorage()! | ||
|
|
||
| const result = await storage.getItem('test-key') | ||
| expect(result).toBe('stored-value') | ||
| expect((OptionalExpoFileSystem as any).File).toHaveBeenCalledWith(mockDocument, 'test-key') | ||
| expect(mockLegacyReadAsStringAsync).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We are completely silent here, should we log something?
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.
mm why? its a valid path/supported path using older versions