├── .all-contributorsrc ├── .gitattributes ├── .github ├── ISSUE_TEMPLATE │ ├── Bug_Report.md │ ├── Feature_Request.md │ └── Question.md ├── PULL_REQUEST_TEMPLATE.md ├── dependabot.yml └── workflows │ └── validate.yml ├── .gitignore ├── .husky └── pre-commit ├── .npmrc ├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── LICENSE ├── README.md ├── babel.config.js ├── extend-expect.d.ts ├── extend-expect.js ├── jest.config.js ├── legacy-extend-expect.d.ts ├── legacy-extend-expect.js ├── other └── eagle.png ├── package.json ├── setup-tests.ts ├── src ├── __tests__ │ ├── __snapshots__ │ │ ├── to-be-visible.tsx.snap │ │ └── to-have-style.tsx.snap │ ├── component-tree.tsx │ ├── legacy-extend-expect.tsx │ ├── to-be-disabled.tsx │ ├── to-be-empty-element.tsx │ ├── to-be-empty.tsx │ ├── to-be-on-the-screen-import.tsx │ ├── to-be-on-the-screen.tsx │ ├── to-be-visible.tsx │ ├── to-contain-element.tsx │ ├── to-have-accessibility-state.tsx │ ├── to-have-accessibility-value.tsx │ ├── to-have-prop.tsx │ ├── to-have-style.tsx │ ├── to-have-text-content.tsx │ └── utils.ts ├── __types__ │ ├── jest-explicit-extend.test-d.ts │ └── jest-implicit-extend.test-d.ts ├── component-tree.ts ├── extend-expect.ts ├── index.ts ├── legacy-extend-expect.ts ├── to-be-disabled.ts ├── to-be-empty-element.ts ├── to-be-on-the-screen.ts ├── to-be-visible.ts ├── to-contain-element.ts ├── to-have-accessibility-state.ts ├── to-have-accessibility-value.ts ├── to-have-prop.ts ├── to-have-style.ts ├── to-have-text-content.ts └── utils.ts ├── tsconfig.json └── tsconfig.prod.json /.all-contributorsrc: -------------------------------------------------------------------------------- 1 | { 2 | "projectName": "jest-native", 3 | "projectOwner": "testing-library", 4 | "repoType": "github", 5 | "repoHost": "https://github.com", 6 | "files": [ 7 | "README.md" 8 | ], 9 | "imageSize": 100, 10 | "commit": true, 11 | "commitConvention": "angular", 12 | "contributors": [ 13 | { 14 | "login": "bcarroll22", 15 | "name": "Brandon Carroll", 16 | "avatar_url": "https://avatars2.githubusercontent.com/u/11020406?v=4", 17 | "profile": "https://github.com/bcarroll22", 18 | "contributions": [ 19 | "code", 20 | "doc", 21 | "infra", 22 | "test" 23 | ] 24 | }, 25 | { 26 | "login": "SantiMA10", 27 | "name": "Santi", 28 | "avatar_url": "https://avatars2.githubusercontent.com/u/7255298?v=4", 29 | "profile": "http://santiagomartin.dev", 30 | "contributions": [ 31 | "code" 32 | ] 33 | }, 34 | { 35 | "login": "marnusw", 36 | "name": "Marnus Weststrate", 37 | "avatar_url": "https://avatars0.githubusercontent.com/u/971499?v=4", 38 | "profile": "https://github.com/marnusw", 39 | "contributions": [ 40 | "code" 41 | ] 42 | }, 43 | { 44 | "login": "Shywim", 45 | "name": "Matthieu Harlé", 46 | "avatar_url": "https://avatars3.githubusercontent.com/u/1584563?v=4", 47 | "profile": "https://github.com/Shywim", 48 | "contributions": [ 49 | "code" 50 | ] 51 | }, 52 | { 53 | "login": "acatalina", 54 | "name": "Alvaro Catalina", 55 | "avatar_url": "https://avatars3.githubusercontent.com/u/23233812?v=4", 56 | "profile": "https://github.com/acatalina", 57 | "contributions": [ 58 | "code" 59 | ] 60 | }, 61 | { 62 | "login": "ilkeryilmaz", 63 | "name": "ilker Yılmaz", 64 | "avatar_url": "https://avatars1.githubusercontent.com/u/1588236?v=4", 65 | "profile": "http://www.ilkeryilmaz.com", 66 | "contributions": [ 67 | "doc" 68 | ] 69 | }, 70 | { 71 | "login": "donovanhiland", 72 | "name": "Donovan Hiland", 73 | "avatar_url": "https://avatars2.githubusercontent.com/u/17991396?v=4", 74 | "profile": "https://github.com/donovanhiland", 75 | "contributions": [ 76 | "code", 77 | "test" 78 | ] 79 | } 80 | ], 81 | "contributorsPerLine": 7, 82 | "skipCi": true 83 | } 84 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.js text eol=lf 3 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/Bug_Report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: 🐛 Bug Report 3 | about: I think something is broken 😡 4 | --- 5 | 6 | 14 | 15 | - `react-native` or `expo`: 16 | - `@testing-library/react-native` version: 17 | - `jest-preset`: 18 | - `react-native` version: 19 | - `node` version: 20 | 21 | ### Relevant code or config: 22 | 23 | ```js 24 | const your = (code) => 'here'; 25 | ``` 26 | 27 | ### What you did: 28 | 29 | 30 | 31 | ### What happened: 32 | 33 | 34 | 35 | ### Reproduction: 36 | 37 | 38 | 39 | ### Problem description: 40 | 41 | 42 | 43 | ### Suggested solution: 44 | 45 | 46 | 47 | ### Can you help us fix this issue by submitting a pull request? 48 | 49 | 50 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/Feature_Request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: 💡 Feature Request 3 | about: I think something could be better 🤔 4 | --- 5 | 6 | 15 | 16 | **Describe the feature you'd like:** 17 | 18 | 19 | 20 | **Suggested implementation:** 21 | 22 | 23 | 24 | **Describe alternatives you've considered:** 25 | 26 | 27 | 28 | **Teachability, Documentation, Adoption, Migration Strategy:** 29 | 30 | 31 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/Question.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: ❓ Support Question 3 | about: I think I don't understand how to do something 🤨 4 | --- 5 | 6 | 7 | 8 | Issues on GitHub are intended to be related to problems and feature requests so we ask you not use 9 | issues to ask for support. 10 | 11 | Remember that this library is almost entirely the same as `react-testing-library` so you're likely 12 | to get some great advice by searching for how to do something with it. 13 | 14 | --- 15 | 16 | ## ❓ React Testing Library Resources 17 | 18 | - Discord https://discord.gg/testing-library 19 | - Stack Overflow https://stackoverflow.com/questions/tagged/react-testing-library 20 | 21 | ## ❓ Testing Library Resources 22 | 23 | - Stack Overflow https://stackoverflow.com/questions/tagged/native-testing-library 24 | 25 | **ISSUES WHICH ARE QUESTIONS WILL BE CLOSED** 26 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 10 | 11 | **What**: 12 | 13 | 14 | 15 | **Why**: 16 | 17 | 18 | 19 | **How**: 20 | 21 | 22 | 23 | **Checklist**: 24 | 25 | 26 | 27 | 28 | 29 | - [ ] Documentation added to the [docs](https://github.com/testing-library/jest-native/README.md) 30 | - [ ] Typescript definitions updated 31 | - [ ] Tests 32 | - [ ] Ready to be merged 33 | 34 | 35 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: github-actions 4 | directory: / 5 | schedule: 6 | interval: daily 7 | 8 | - package-ecosystem: npm 9 | directory: / 10 | schedule: 11 | interval: daily 12 | -------------------------------------------------------------------------------- /.github/workflows/validate.yml: -------------------------------------------------------------------------------- 1 | name: validate 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | pull_request: 7 | branches: ['**'] 8 | 9 | concurrency: 10 | group: ${{ github.workflow }}-${{ github.ref }} 11 | cancel-in-progress: true 12 | 13 | jobs: 14 | main: 15 | strategy: 16 | matrix: 17 | node: [14, 16, 18] 18 | runs-on: ubuntu-latest 19 | steps: 20 | - name: ⬇️ Checkout repo 21 | uses: actions/checkout@v3 22 | with: 23 | fetch-depth: 0 24 | 25 | - name: ⎔ Setup node 26 | uses: actions/setup-node@v3 27 | with: 28 | node-version: ${{ matrix.node }} 29 | 30 | - name: 📥 Download deps 31 | run: yarn install 32 | 33 | - name: ▶️ Run linter 34 | run: yarn lint 35 | 36 | - name: ▶️ Run typecheck 37 | run: yarn typecheck 38 | 39 | - name: ▶️ Run tests with coverage 40 | run: yarn test:coverage 41 | 42 | - name: ⬆️ Upload coverage report 43 | uses: codecov/codecov-action@v3 44 | with: 45 | fail_ci_if_error: true 46 | flags: node-${{ matrix.node }} 47 | 48 | release: 49 | needs: main 50 | runs-on: ubuntu-latest 51 | if: 52 | ${{ github.repository == 'testing-library/jest-native' && github.ref == 'refs/heads/main' && 53 | github.event_name == 'push' }} 54 | steps: 55 | - name: ⬇️ Checkout repo 56 | uses: actions/checkout@v3 57 | 58 | - name: ⎔ Setup node 59 | uses: actions/setup-node@v3 60 | with: 61 | node-version: 18 62 | 63 | - name: 📥 Download deps 64 | uses: bahmutov/npm-install@v1 65 | with: 66 | useLockFile: false 67 | 68 | - name: 🚀 Release 69 | run: yarn semantic-release 70 | env: 71 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 72 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }} 73 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | coverage 3 | dist 4 | .idea 5 | .DS_Store 6 | 7 | yarn-error.log 8 | 9 | package-lock.json 10 | yarn.lock 11 | -------------------------------------------------------------------------------- /.husky/pre-commit: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | . "$(dirname -- "$0")/_/husky.sh" 3 | 4 | yarn pretty-quick --staged 5 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | registry=https://registry.npmjs.org/ 2 | package-lock=false 3 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # CHANGELOG 2 | 3 | The changelog is automatically updated using 4 | [semantic-release](https://github.com/semantic-release/semantic-release). You can see it on the 5 | [releases page](../../releases). 6 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers 6 | pledge to making participation in our project and our community a harassment-free experience for 7 | everyone, regardless of age, body size, disability, ethnicity, sex characteristics, gender identity 8 | and expression, level of experience, education, socio-economic status, nationality, personal 9 | appearance, race, religion, or sexual identity and orientation. 10 | 11 | ## Our Standards 12 | 13 | Examples of behavior that contributes to creating a positive environment include: 14 | 15 | - Using welcoming and inclusive language 16 | - Being respectful of differing viewpoints and experiences 17 | - Gracefully accepting constructive criticism 18 | - Focusing on what is best for the community 19 | - Showing empathy towards other community members 20 | 21 | Examples of unacceptable behavior by participants include: 22 | 23 | - The use of sexualized language or imagery and unwelcome sexual attention or advances 24 | - Trolling, insulting/derogatory comments, and personal or political attacks 25 | - Public or private harassment 26 | - Publishing others' private information, such as a physical or electronic address, without explicit 27 | permission 28 | - Other conduct which could reasonably be considered inappropriate in a professional setting 29 | 30 | ## Our Responsibilities 31 | 32 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are 33 | expected to take appropriate and fair corrective action in response to any instances of unacceptable 34 | behavior. 35 | 36 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, 37 | code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or 38 | to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, 39 | threatening, offensive, or harmful. 40 | 41 | ## Scope 42 | 43 | This Code of Conduct applies both within project spaces and in public spaces when an individual is 44 | representing the project or its community. Examples of representing a project or community include 45 | using an official project e-mail address, posting via an official social media account, or acting as 46 | an appointed representative at an online or offline event. Representation of a project may be 47 | further defined and clarified by project maintainers. 48 | 49 | ## Enforcement 50 | 51 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting 52 | the project team at brandonvcarroll@gmail.com. All complaints will be reviewed and investigated and 53 | will result in a response that is deemed necessary and appropriate to the circumstances. The project 54 | team is obligated to maintain confidentiality with regard to the reporter of an incident. Further 55 | details of specific enforcement policies may be posted separately. 56 | 57 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face 58 | temporary or permanent repercussions as determined by other members of the project's leadership. 59 | 60 | ## Attribution 61 | 62 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at 63 | https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 64 | 65 | [homepage]: https://www.contributor-covenant.org 66 | 67 | For answers to common questions about this code of conduct, see 68 | https://www.contributor-covenant.org/faq 69 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2019 Brandon Carroll 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | > [!CAUTION] 2 | > **This package is deprecated and is no longer actively maintained.** 3 | > 4 | > We encourage you to migrate to React Native Testing Library, v12.4 or later, which includes modern [built-in Jest matchers](https://callstack.github.io/react-native-testing-library/docs/api/jest-matchers) based on the matchers for this repository. 5 | > 6 | > The migration process should be relatively straightforward, we have a [migration guide](https://callstack.github.io/react-native-testing-library/docs/migration/jest-matchers) available. 7 | 8 | 9 |
10 |

jest-native

11 | 12 | 13 | eagle 19 | 20 | 21 |

Custom jest matchers to test the state of React Native.

22 |
23 | 24 |
25 | 26 | [![version](https://img.shields.io/npm/v/@testing-library/jest-native.svg?style=flat-square)](https://www.npmjs.com/package/@testing-library/jest-native) 27 | [![Code Coverage](https://img.shields.io/codecov/c/github/testing-library/jest-native.svg?style=flat-square)](https://codecov.io/github/testing-library/jest-native) 28 | ![Build](https://github.com/testing-library/jest-native/actions/workflows/validate.yml/badge.svg) 29 | [![downloads](https://img.shields.io/npm/dm/@testing-library/jest-native.svg?style=flat-square)](http://www.npmtrends.com/@testing-library/jest-native) 30 | [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com) 31 | [![Discord](https://img.shields.io/discord/723559267868737556.svg?color=7389D8&labelColor=6A7EC2&logo=discord&logoColor=ffffff&style=flat-square)](https://discord.gg/testing-library) 32 | [![Star on GitHub](https://img.shields.io/github/stars/testing-library/jest-native.svg?style=social)](https://github.com/testing-library/jest-native/stargazers) 33 | 34 | 35 | 36 | 37 | ## Table of Contents 38 | 39 | - [The problem](#the-problem) 40 | - [This solution](#this-solution) 41 | - [Compatibility](#compatibility) 42 | - [Installation](#installation) 43 | - [Usage](#usage) 44 | - [Extending Jest matchers](#extending-jest-matchers) 45 | - [TypeScript support](#typescript-support) 46 | - [Matchers](#matchers) 47 | - [`toBeDisabled`](#tobedisabled) 48 | - [`toBeEnabled`](#tobeenabled) 49 | - [`toBeEmptyElement`](#tobeemptyelement) 50 | - [`toContainElement`](#tocontainelement) 51 | - [`toBeOnTheScreen`](#tobeonthescreen) 52 | - [`toHaveProp`](#tohaveprop) 53 | - [`toHaveTextContent`](#tohavetextcontent) 54 | - [`toHaveStyle`](#tohavestyle) 55 | - [`toBeVisible`](#tobevisible) 56 | - [`toHaveAccessibilityState`](#tohaveaccessibilitystate) 57 | - [`toHaveAccessibilityValue`](#tohaveaccessibilityvalue) 58 | - [Inspiration](#inspiration) 59 | - [Other solutions](#other-solutions) 60 | - [Contributors](#contributors) 61 | 62 | 63 | 64 | ## The problem 65 | 66 | You want to use [jest](https://facebook.github.io/jest/) to write tests that assert various things 67 | about the state of a React Native app. As part of that goal, you want to avoid all the repetitive 68 | patterns that arise in doing so like checking for a native element's props, its text content, its 69 | styles, and more. 70 | 71 | ## This solution 72 | 73 | The `jest-native` library provides a set of custom jest matchers that you can use to extend jest. 74 | These will make your tests more declarative, clear to read and to maintain. 75 | 76 | ## Compatibility 77 | 78 | These matchers should, for the most part, be agnostic enough to work with any React Native testing 79 | utilities, but they are primarily intended to be used with 80 | [React Native Testing Library](https://github.com/callstack/react-native-testing-library). Any 81 | issues raised with existing matchers or any newly proposed matchers must be viewed through 82 | compatibility with that library and its guiding principles first. 83 | 84 | ## Installation 85 | 86 | This module should be installed as one of your project's `devDependencies`: 87 | 88 | #### Using `yarn` 89 | 90 | ```sh 91 | yarn add --dev @testing-library/jest-native 92 | ``` 93 | 94 | #### Using `npm` 95 | 96 | ```sh 97 | npm install --save-dev @testing-library/jest-native 98 | ``` 99 | 100 | You will need `react-test-renderer`, `react`, and `react-native` installed in order to use this 101 | package. 102 | 103 | ## Usage 104 | 105 | ### Extending Jest matchers 106 | 107 | Import `@testing-library/jest-native/extend-expect` once (for instance in your 108 | [tests setup file](https://jestjs.io/docs/configuration#setupfilesafterenv-array)) and you're good 109 | to go: 110 | 111 | ```javascript 112 | import '@testing-library/jest-native/extend-expect'; 113 | ``` 114 | 115 | Alternatively, you can selectively import only the matchers you intend to use, and extend jest's 116 | `expect` yourself: 117 | 118 | ```javascript 119 | import { toBeEmptyElement, toHaveTextContent } from '@testing-library/jest-native'; 120 | 121 | expect.extend({ toBeEmptyElement, toHaveTextContent }); 122 | ``` 123 | 124 | ### TypeScript support 125 | 126 | In order to setup proper TypeScript type checking use either one of the following approches. 127 | 128 | #### 1. Use TypeScript Jest setup file. 129 | 130 | Use `jest-setup.ts` file (instead of `jest-setup.js` file) which is added to Jest config's `setupFilesAfterEnv` option. 131 | 132 | The Jest setup file should contain following line: 133 | 134 | ```typescript 135 | import '@testing-library/jest-native/extend-expect'; 136 | ``` 137 | 138 | This should enable TypeScript checkign for both `tsc` and VS Code intellisense. 139 | 140 | #### 2. Use `declarations.d.ts` file 141 | 142 | Alternatively, create `declarations.d.ts` file at the root level of your project, if it does not exist already. 143 | 144 | Add following line at the top of your `declarations.d.ts`: 145 | 146 | ``` 147 | /// 148 | ``` 149 | 150 | This should enable TypeScript checkign for both `tsc` and VS Code intellisense. 151 | 152 | ## Matchers 153 | 154 | `jest-native` has only been tested to work with 155 | [React Native Testing Library](https://github.com/callstack/react-native-testing-library). Keep in 156 | mind that these queries are intended only to work with elements corresponding to 157 | [host components](https://reactnative.dev/architecture/glossary#react-host-components-or-host-components). 158 | 159 | ### `toBeDisabled` 160 | 161 | ```javascript 162 | toBeDisabled(); 163 | ``` 164 | 165 | Check whether or not an element is disabled from a user perspective. 166 | 167 | This matcher will check if the element or its parent has any of the following props : 168 | 169 | - `disabled` 170 | - `accessibilityState={{ disabled: true }}` 171 | - `editable={false}` (for `TextInput` only) 172 | 173 | #### Examples 174 | 175 | ```javascript 176 | const { getByTestId } = render( 177 | 178 |