|
| 1 | +--- |
| 2 | +name: react-native-harness |
| 3 | +description: Write and debug React Native Harness tests for app code. Use when the user asks to create or fix tests that import from react-native-harness, mock modules, spy on functions, render React Native components on-device, use setupFiles or setupFilesAfterEnv, or add optional UI tests with @react-native-harness/ui. |
| 4 | +--- |
| 5 | + |
| 6 | +# React Native Harness |
| 7 | + |
| 8 | +React Native Harness tests use Jest-style APIs but run in the app or browser environment instead of plain Node. |
| 9 | + |
| 10 | +## Test File Conventions |
| 11 | + |
| 12 | +- Use `.harness.[jt]s` or `.harness.[jt]sx` test files. |
| 13 | +- Import test APIs from `react-native-harness`. |
| 14 | +- Put tests inside `describe(...)` blocks. |
| 15 | +- Use `@react-native-harness/ui` only when the test needs queries, interactions, or screenshots. |
| 16 | + |
| 17 | +## Default Test Shape |
| 18 | + |
| 19 | +```ts |
| 20 | +import { describe, test, expect } from 'react-native-harness'; |
| 21 | + |
| 22 | +describe('Feature name', () => { |
| 23 | + test('does something', () => { |
| 24 | + expect(true).toBe(true); |
| 25 | + }); |
| 26 | +}); |
| 27 | +``` |
| 28 | + |
| 29 | +Prefer these public APIs when writing tests: |
| 30 | + |
| 31 | +- Test structure: `describe`, `test`, `it`, `beforeEach`, `afterEach`, `beforeAll`, `afterAll` |
| 32 | +- Focus and pending helpers: `test.skip`, `test.only`, `test.todo`, `describe.skip`, `describe.only` |
| 33 | +- Assertions: `expect` |
| 34 | +- Mocking and spying: `fn`, `spyOn`, `clearAllMocks`, `resetAllMocks`, `restoreAllMocks` |
| 35 | +- Module mocking: `mock`, `requireActual`, `unmock`, `resetModules` |
| 36 | +- Async polling: `waitFor`, `waitUntil` |
| 37 | + |
| 38 | +Test functions may be async. If a test returns a promise, Harness waits for it; if that promise rejects, the test fails. |
| 39 | + |
| 40 | +## Mocking And Spying |
| 41 | + |
| 42 | +Use `fn()` for standalone mock functions and `spyOn()` for existing methods. |
| 43 | + |
| 44 | +- `expect` follows Vitest's API. |
| 45 | +- `expect.soft(...)` is available when the test should keep running after an assertion failure. |
| 46 | +- `clearAllMocks()` clears call history but keeps implementations. |
| 47 | +- `resetAllMocks()` clears call history and resets mock implementations. |
| 48 | +- `restoreAllMocks()` restores spied methods to their original implementations. |
| 49 | + |
| 50 | +Typical cleanup: |
| 51 | + |
| 52 | +```ts |
| 53 | +import { afterEach, clearAllMocks } from 'react-native-harness'; |
| 54 | + |
| 55 | +afterEach(() => { |
| 56 | + clearAllMocks(); |
| 57 | +}); |
| 58 | +``` |
| 59 | + |
| 60 | +## Module Mocking |
| 61 | + |
| 62 | +Use module mocking when the test must replace an entire module or specific exports. |
| 63 | + |
| 64 | +- `mock(moduleId, factory)` registers a lazy mock factory. |
| 65 | +- `requireActual(moduleId)` is the safe path for partial mocks. |
| 66 | +- `unmock(moduleId)` removes a mock for one module. |
| 67 | +- `resetModules()` clears module mocks and module cache state. |
| 68 | + |
| 69 | +Recommended pattern: |
| 70 | + |
| 71 | +```ts |
| 72 | +import { |
| 73 | + afterEach, |
| 74 | + describe, |
| 75 | + expect, |
| 76 | + mock, |
| 77 | + requireActual, |
| 78 | + resetModules, |
| 79 | + test, |
| 80 | +} from 'react-native-harness'; |
| 81 | + |
| 82 | +afterEach(() => { |
| 83 | + resetModules(); |
| 84 | +}); |
| 85 | + |
| 86 | +describe('partial mock', () => { |
| 87 | + test('overrides one export but keeps the rest', () => { |
| 88 | + mock('react-native', () => { |
| 89 | + const actual = requireActual('react-native'); |
| 90 | + const proto = Object.getPrototypeOf(actual); |
| 91 | + const descriptors = Object.getOwnPropertyDescriptors(actual); |
| 92 | + const mocked = Object.create(proto, descriptors); |
| 93 | + |
| 94 | + Object.defineProperty(mocked, 'Platform', { |
| 95 | + get() { |
| 96 | + return { |
| 97 | + ...actual.Platform, |
| 98 | + OS: 'mockOS', |
| 99 | + }; |
| 100 | + }, |
| 101 | + }); |
| 102 | + |
| 103 | + return mocked; |
| 104 | + }); |
| 105 | + |
| 106 | + const rn = require('react-native'); |
| 107 | + expect(rn.Platform.OS).toBe('mockOS'); |
| 108 | + }); |
| 109 | +}); |
| 110 | +``` |
| 111 | + |
| 112 | +- Always clean up module mocks with `resetModules()` in `afterEach` when tests mock modules. |
| 113 | +- Use `requireActual()` for partial mocks so unrelated exports stay real. |
| 114 | +- For `react-native`, preserve property descriptors when partially mocking to avoid triggering lazy getters too early. |
| 115 | +- Remember that module factories are evaluated when the module is first required. |
| 116 | + |
| 117 | +## Async Behavior |
| 118 | + |
| 119 | +Use: |
| 120 | + |
| 121 | +- `waitFor(...)` when the callback should eventually succeed or stop throwing |
| 122 | +- `waitUntil(...)` when the callback should eventually return a truthy value |
| 123 | + |
| 124 | +Both support timeout control. Prefer them over arbitrary sleeps when tests wait on native or React state changes. |
| 125 | + |
| 126 | +## UI Testing |
| 127 | + |
| 128 | +UI testing is opt-in and uses `render(...)` from `react-native-harness` together with `@react-native-harness/ui`. |
| 129 | + |
| 130 | +Use `render(...)` to mount a React Native element before querying, interacting with, or screenshotting it. |
| 131 | + |
| 132 | +- `render(...)` is async |
| 133 | +- `rerender(...)` is async |
| 134 | +- `unmount()` is optional because cleanup happens automatically after each test |
| 135 | +- `wrapper` is the right tool for providers and shared context |
| 136 | +- Rendered UI appears as an overlay in the real environment, not as an in-memory tree |
| 137 | +- Only one rendered component can be visible at a time |
| 138 | + |
| 139 | +Use it when the task requires: |
| 140 | + |
| 141 | +- `render(...)` or `rerender(...)` |
| 142 | +- `screen.findByTestId(...)` |
| 143 | +- `screen.findAllByTestId(...)` |
| 144 | +- `screen.queryByTestId(...)` |
| 145 | +- `screen.queryAllByTestId(...)` |
| 146 | +- `screen.findByAccessibilityLabel(...)` |
| 147 | +- `screen.findAllByAccessibilityLabel(...)` |
| 148 | +- `screen.queryByAccessibilityLabel(...)` |
| 149 | +- `screen.queryAllByAccessibilityLabel(...)` |
| 150 | +- `userEvent.press(...)` |
| 151 | +- `userEvent.type(...)` |
| 152 | +- screenshots with `screen.screenshot()` |
| 153 | +- element screenshots with `screen.screenshot(element)` |
| 154 | +- image assertions with `toMatchImageSnapshot(...)` |
| 155 | + |
| 156 | +- Keep imports split correctly: core APIs from `react-native-harness`, UI APIs from `@react-native-harness/ui`. |
| 157 | +- Mention that `@react-native-harness/ui` requires installation, and native apps must be rebuilt after adding it. |
| 158 | +- `toMatchImageSnapshot(...)` needs a unique snapshot `name`. |
| 159 | +- If screenshotting elements that extend beyond screen bounds, call out `disableViewFlattening: true` in `rn-harness.config.mjs`. |
| 160 | +- On web, UI interactions and screenshots run through the web runner's Playwright-backed browser environment. |
| 161 | + |
| 162 | +## Setup Files |
| 163 | + |
| 164 | +Harness follows two setup phases configured in `jest.harness.config.mjs`: |
| 165 | + |
| 166 | +- `setupFiles`: runs before the test framework is initialized. Use for early polyfills and globals. Do not use `describe`, `test`, `expect`, or hooks here. |
| 167 | +- `setupFilesAfterEnv`: runs after the test framework is ready. Use for global mocks, hooks, and matcher setup. |
| 168 | + |
| 169 | +Recommended uses: |
| 170 | + |
| 171 | +- Early environment shims in `setupFiles` |
| 172 | +- Global `afterEach`, `clearAllMocks`, `resetModules`, and shared mocks in `setupFilesAfterEnv` |
| 173 | + |
| 174 | +## CLI And Execution Constraints |
| 175 | + |
| 176 | +- Harness wraps the Jest CLI. |
| 177 | +- Tests execute on one configured runner at a time. |
| 178 | +- Execution is serial for stability. |
| 179 | +- `--harnessRunner <name>` selects the runner. |
| 180 | +- Standard Jest flags like `--watch`, `--coverage`, and `--testNamePattern` are still relevant. |
| 181 | +- Do not recommend unsupported Jest environment overrides or snapshot-update workflows for native image snapshots. |
| 182 | + |
| 183 | +For install, runner setup, and config files, read [references/installation.md](references/installation.md). |
0 commit comments