MetaMask / MetaMask/metamask-mobile
[Non-prod Bug]: Numerous invalid jest unit tests when testing queryByText of findByText toBeDefined
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 3k
- Forks
- 1.7k
- Avg merge
- 1d 14h
- Merged PRs (30d)
- 669
Description
### Describe the bug
We have numerous invalid tests where we check queryByText passes toBeDefined. This is because queryByText may return null which passes as defined.
We need to update these tests to test toBeTruthy rather than toBeDefined
Quick query search provided below currently finds 23 instances.
Considerations:
- can use queryByText rather than findByText since findByText returns a promise. We can avoid async/await in these cases.
- findByText Promise result does not return null so it is okay to use toBeDefined
Return values:
queryByText → ReactTestInstance | null
findByText → Promise
#### Example of update
##### Before
```
expect(queryByText(expected)).toBeDefined();
```
##### After
```
expect(queryByText(expected)).toBeTruthy();
```
### Expected behavior
_No response_
### Screenshots/Recordings
##### example of test failing after changing from toBeDefined → toBeTruthy
https://github.com/user-attachments/assets/d1030089-f0d1-4b36-a27f-185ed7f80f4e
### Steps to reproduce
yarn jest
quick query search:
```
expect\s*\(\s*(await\s+)?(wrapper\.)?(queryByText)\s*\(\s*(['"`].*?['"`]|\w+[\w\.()]*)\s*\)\s*\)\s*\.toBe(Defined|Truthy)\(\s*\)
```
note: this does not find e.g.
```
expect(element).toBeDefined();
```
### Error messages or log output
```shell
```
### Detection stage
During release testing
### Version
7.39.0
### Build type
None
### Device
IDE
### Operating system
iOS
### Additional context
_No response_
### Severity
_No response_
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Search the repository with the provided regex for queryByText assertions using toBeDefined, then inspect each matching test to distinguish queryByText from findByText. Update the invalid queryByText expectations and run the affected tests with yarn jest to confirm missing text now fails.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- react-native, typescript
- Domain
- mobile, testing
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100