|
| 1 | +--- |
| 2 | +id: migration-jest-native |
| 3 | +title: Migration from Jest Native matchers |
| 4 | +--- |
| 5 | + |
| 6 | +This guide describes steps necessary to migrate from [legacy Jest Native matchers](https://github.com/testing-library/jest-native) `v5` to [built-in Jest matchers](JestMatchers.md). |
| 7 | + |
| 8 | +import TOCInline from '@theme/TOCInline'; |
| 9 | + |
| 10 | +<TOCInline toc={toc} /> |
| 11 | + |
| 12 | +## General notes |
| 13 | + |
| 14 | +All of built-in Jest matchers provided by React Native Testing Library are supporting only host elements. This should not be an issue, as all RNTL v12 queries already return only host elements. When this guide states that given matcher should work the same it assumes behavior only only host elements. If you need to assert status of composite elements use Jest Native matchers in [legacy mode](#gradual-migration). |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +You can use the built-in matchers by adding following line to your `jest-setup.ts` file: |
| 19 | + |
| 20 | +```ts |
| 21 | +import '@testing-library/react-native/extend-expect'; |
| 22 | +``` |
| 23 | + |
| 24 | +### Gradual migration |
| 25 | + |
| 26 | +You can use the built-in matchers alongside with legacy Jest Native matchers by chaning their import in your `jest-setup.ts` file: |
| 27 | + |
| 28 | +```ts |
| 29 | +// Replace this: |
| 30 | +// import '@testing-library/jest-native/extend-expect'; |
| 31 | + |
| 32 | +// With this: |
| 33 | +import '@testing-library/react-native/extend-expect'; |
| 34 | +import '@testing-library/jest-native/legacy-extend-expect'; |
| 35 | +``` |
| 36 | + |
| 37 | +In such case legacy matchers will be available using `legacy_` prefix, e.g.: |
| 38 | + |
| 39 | +```ts |
| 40 | +expect(element).legacy_toHaveAccessibilityState({ busy: true }); |
| 41 | +``` |
| 42 | + |
| 43 | +## Migration details |
| 44 | + |
| 45 | +### Matchers not requiring changes |
| 46 | + |
| 47 | +Following matchers should work the same: |
| 48 | +* [`toBeEmptyElement()`](JestMatchers.md#tobeemptyelement) |
| 49 | +* [`toBeEnabled()` / `toBeDisabled()`](JestMatchers.md#tobeenabled) |
| 50 | +* [`toBeOnTheScreen()`](JestMatchers.md#tobeonthescreen) |
| 51 | +* [`toBeVisible()`](JestMatchers.md#tobevisible) |
| 52 | +* [`toContainElement()`](JestMatchers.md#tocontainelement) |
| 53 | +* [`toHaveDisplayValue()`](JestMatchers.md#tohavedisplayvalue) |
| 54 | +* [`toHaveProp()`](JestMatchers.md#tohaveprop) |
| 55 | +* [`toHaveStyle()`](JestMatchers.md#tohavestyle) |
| 56 | +* [`toHaveTextContent()`](JestMatchers.md#tohavetextcontent) |
| 57 | + |
| 58 | +### Renamed matchers |
| 59 | + |
| 60 | +`toHaveAccessibilityValue()` matcher has been renamed to [`toHaveAccessibleValue()`](JestMatchers.md#tohaveaccessiblevalue) but otherwise should work the same. |
| 61 | + |
| 62 | +### Removed matchers |
| 63 | + |
| 64 | +`toHaveAccessibilityState()` matcher has been replaced by following matchers: |
| 65 | +* enabled state: [`toBeEnabled()` / `toBeDisabled()`](JestMatchers.md#tobeenabled) |
| 66 | +* checked state: [`toBeChecked()` / `toBePartiallyChecked()`](JestMatchers.md#tobechecked) |
| 67 | +* selected state: [`toBeSelected()`](JestMatchers.md#tobeselected) |
| 68 | +* expanded state: [`toBeExpanded()` / `toBeCollapsed()`](JestMatchers.md#tobeexpanded) |
| 69 | +* busy state: [`toBeBusy()`](JestMatchers.md#tobebusy) |
| 70 | + |
| 71 | +The new matchers support both `accessbililityState` and `aria-*` props. |
| 72 | + |
| 73 | +You should be aware of following changes: |
| 74 | +* [`toBeEnabled()` / `toBeDisabled()`](JestMatchers.md#tobeenabled) matchers also check the disabled state for element ancestors and not only the element itself |
| 75 | +* [`toBeChecked()`](JestMatchers.md#tobechecked) matcher supports only elements with `checkbox` or `radio` role |
| 76 | +* [`toBePartiallyChecked()`](JestMatchers.md#tobechecked) matchers supports only elements with `checkbox` role |
0 commit comments