React testing improvements
See original GitHub issueDescription
I’m normally used to using Enzyme but I noticed Nx used react-testing-library in our project w/ no option to change it within Nx.
I started trying to learn react-testing-library & then ran into this issue because ~the jest custom matchers are not setup.~ I guess I just need to import this at the top of every test
Motivation
~Better testing out of the box w/o having to debug the setup~ Some users may want to use Enzyme
Suggested Implementation
- offer an option for Enzyme?
- flesh out the react-testing-library setup a bit more
yarn add @testing-library/jest-dom --dev
- add
import '@testing-library/jest-dom';
to the test because the example on https://testing-library.com/docs/react-testing-library/example-intro produces type error about missing.toHaveTextContent()
method, and only changing it to this seems to fix it
Alternate Implementations
~You could omit tests in the react builder? Although that would take away some of the benefits of using Nx in the first place.~
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:7 (1 by maintainers)
Top Results From Across the Web
Improving React Testing Library tests | Alex Khomenko
Properly written tests can not only help to prevent regressions or buggy code but in the case of RTL also improve the accessibility...
Read more >Optimizing Performance - React
If you're benchmarking or experiencing performance problems in your React apps, make sure you're testing with the minified production build.
Read more >Straightforward React UI Testing - Toptal
Using React.js props your application's front-end. In particular, testing user interactions and viewing renderings is vastly improved with React UI testing.
Read more >React Testing Library: The Modern Way to ... - Bits and Pieces
Let's have a look a the challenges with the current testing approach using a practical example. Just imagine, you write your unit tests...
Read more >Testing In React, Part 2: React Testing Library | by Bryn Bennett
The headline for React Testing Library is, “Test functionality, not implementation”. The idea behind this concept is that implementation is constantly being ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
To anyone interested: I managed to add types for custom matchers (such as
toHaveTextContent
) by modifying thetsconfig.spec.json
in the directory I’m testing:With this change, you don’t need to import
@testing-library/jest-dom
in every test file.(Make sure to install
@types/testing-library__jest-dom
first.)I had the same issue and in my case in my library folder
libs/<mylib>
I createdjest.setup.ts
with import, same as in this comment, but I didn’t make any type imports, instead, intsconfig.spec.json
I added toinclude
like this:and had no issues. I wanted to use
jest.preset.js
that NX provides but didn’t manage to make it work. I wish I could do this with preset, so that I don’t have to createjest.setup.ts
in every lib/app I have and do the sameinclude
s intsconfig
s everywhere.