Files
lnreader/TESTING.md
T
Patrick Loser 1803cd071d feat: Add testing support for react code (#1763)
* first attempt

* first attempt

* added first useNovel tests

* more tests

* more tests

* resolved all test issues

* added test-utils

* add a few component tests

* add TESTIGN.md

* add testing gh action

* fixed minor issues

* fixed minor issues
2026-02-25 14:51:46 +01:00

2.0 KiB

Testing Guide for LNReader

This guide explains how to write tests in this React Native project using Jest and React Testing Library.

Existing Mocks

Global Mocks

The project has global mocks configured in Jest. These are automatically applied:

  • __mocks__/ - Global mocks for native modules (react-native-mmkv, react-navigation, all database queries, etc.)
  • src/hooks/__mocks__/index.ts - Hook-specific mocks (showToast, getString, parseChapterNumber, etc.)
  • src/hooks/__tests__/mocks.ts - Extended mocks for persisted hooks

Using @test-utils

There's a custom render wrapper at __tests-modules__/test-utils.tsx with:

  • render - wraps with GestureHandlerRootView, SafeAreaProvider, PaperProvider, etc.
  • renderNovel - includes NovelContextProvider
  • AllTheProviders - the full provider wrapper

Usage:

import { render, renderNovel } from '@test-utils';

Common Issues

1. ESM Modules Not Transforming

If you see Cannot use import statement outside a module, you need to add mocks for the module:

jest.mock('@hooks/persisted/usePlugins');
// Add more specific mocks as needed

2. Mock Functions Not Working

If mockReturnValue throws "not a function", create mock functions at module level:

// CORRECT: Module-level mock functions
const mockUseTheme = jest.fn();
jest.mock('@hooks/persisted', () => ({
  useTheme: () => mockUseTheme(),
}));

// INCORRECT: Trying to use jest.Mock type casting
// (useTheme as jest.Mock).mockReturnValue(...) // This fails!

3. Test Isolation

Tests must mock at module level, not in beforeEach:

// CORRECT
const mockFn = jest.fn();
jest.mock('module', () => ({ useHook: () => mockFn() }));

// INCORRECT - mocks get reset between tests
jest.mock('module');
beforeEach(() => {
  // This doesn't work properly
});

Running Tests

pnpm test           # Run all tests
pnpm test:watch     # Watch mode
pnpm test:coverage  # With coverage
pnpm test:rn        # React Native only
pnpm test:db        # Database only