Files
expo-popcore-app/jest.setup.js
imeepos 6e85460b2e test: 为 app/auth.tsx 添加完整的测试覆盖
- 新增 app/auth.test.tsx,包含 10 个测试用例
- 测试覆盖加载状态、认证状态、未认证状态、导航行为和副作用
- 优化 Jest 配置,添加 skipLibCheck 和 useLocalSearchParams mock
- 添加必要的组件 mock(FlashList、icon、HomeSkeleton 等)
- 添加 coverage 目录到 .gitignore
- 修复 index.tsx 中多余的 key 属性
- 更新 tsconfig.json 添加 esModuleInterop 和 skipLibCheck

测试覆盖率: app/auth.tsx 达到 100%

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-19 12:41:10 +08:00

113 lines
2.4 KiB
JavaScript

require('@testing-library/jest-native/extend-expect')
// Mock react-native modules
// Note: NativeAnimatedHelper may not be needed in newer React Native versions
// jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper')
// Mock expo modules
jest.mock('expo-constants', () => ({
default: {},
}))
jest.mock('expo-linking', () => ({
createURL: (url) => url,
parse: (url) => ({ path: url }),
}))
jest.mock('expo-secure-store', () => ({
getItemAsync: jest.fn(),
setItemAsync: jest.fn(),
deleteItemAsync: jest.fn(),
}))
jest.mock('expo-font', () => ({
useFonts: () => [true, null],
}))
jest.mock('expo-splash-screen', () => ({
preventAutoHideAsync: jest.fn(),
hideAsync: jest.fn(),
}))
// Mock react-native-reanimated
jest.mock('react-native-reanimated', () => {
const Reanimated = require('react-native-reanimated/mock')
Reanimated.default.call = () => {}
return Reanimated
})
// Mock nativewind
jest.mock('nativewind', () => ({
styled: jest.fn(),
}))
// Mock @react-navigation/native
jest.mock('@react-navigation/native', () => ({
useNavigation: () => ({
navigate: jest.fn(),
goBack: jest.fn(),
reset: jest.fn(),
}),
useRoute: () => ({
params: {},
}),
NavigationContainer: ({ children }) => children,
}))
// Mock expo-router
jest.mock('expo-router', () => ({
useRouter: () => ({
push: jest.fn(),
replace: jest.fn(),
back: jest.fn(),
}),
useLocalSearchParams: () => ({}),
useSegments: () => [],
usePathname: () => '/',
}))
// Global mock for console methods to reduce noise in tests
global.console = {
...console,
error: jest.fn(),
warn: jest.fn(),
log: jest.fn(),
}
// Mock @repo/core and @repo/sdk
jest.mock('@repo/core', () => ({
root: {
get: jest.fn(),
},
}))
jest.mock('@repo/sdk', () => ({
CategoryController: class MockCategoryController {},
}))
// Mock lib/auth to avoid TypeScript compilation errors
jest.mock('@/lib/auth', () => ({
OWNER_ID: 'test-owner-id',
}))
// Mock components
jest.mock('@/components/icon', () => ({
DownArrowIcon: () => null,
PointsIcon: () => null,
SearchIcon: () => null,
}))
jest.mock('@/components/skeleton/HomeSkeleton', () => ({
HomeSkeleton: () => null,
}))
// Mock FlashList to actually render items
jest.mock('@shopify/flash-list', () => {
const { View } = require('react-native')
return {
FlashList: ({ data, renderItem }: any) => {
return data.map((item: any, index: number) => renderItem({ item, index }))
},
}
})