question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React testing improvements

See original GitHub issue

Description

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

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:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

12reactions
karol-majewskicommented, Mar 24, 2021

To anyone interested: I managed to add types for custom matchers (such as toHaveTextContent ) by modifying the tsconfig.spec.json in the directory I’m testing:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "../../dist/out-tsc",
    "module": "commonjs",
-   "types": ["jest", "node"]
+   "types": ["jest", "node", "testing-library__jest-dom"]
  },
  "include": ["**/*.spec.ts", "**/*.spec.tsx"]
}

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.)

7reactions
Shaglockcommented, Sep 20, 2021

I had the same issue and in my case in my library folder libs/<mylib> I created jest.setup.ts with import, same as in this comment, but I didn’t make any type imports, instead, in tsconfig.spec.json I added to include like this:

{
...
  "include": [
    "./jest.setup.ts",
    ...
...
  ]
}

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 create jest.setup.ts in every lib/app I have and do the same includes in tsconfigs everywhere.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found