Repository navigation
feat(tools): setup react testing library in ui-components - #42221
Conversation
abe6bac to
1f613e6
Compare
| "eslint-config-prettier": "8.3.0", | ||
| "eslint-plugin-import": "2.23.3", | ||
| "eslint-plugin-import": "2.23.2", | ||
| "eslint-plugin-jest-dom": "3.9.0", |
There was a problem hiding this comment.
RTL recommends this plugin. It helps guide us to choose the appropriate assertion for our unit tests.
| "eslint-plugin-prettier": "3.4.0", | ||
| "eslint-plugin-react": "7.23.2", | ||
| "eslint-plugin-react-hooks": "2.5.1", | ||
| "eslint-plugin-testing-library": "4.5.1", |
There was a problem hiding this comment.
Also recommended by RTL. It helps guide us to use the most appropriate query API to find the element we need in our tests.
| "@storybook/addon-links": "6.2.9", | ||
| "@storybook/addon-postcss": "2.0.0", | ||
| "@storybook/react": "6.2.9", | ||
| "@testing-library/react": "11.2.7", |
There was a problem hiding this comment.
Installed this with uncertainty.
It is inside ui-components, but I see @testing-library/cypress being in the root package.json, so I assume we want to move shared dev dependencies to the root instead.
There was a problem hiding this comment.
Yes please. Ideally all dev dependencies should be in the root if possible. It cuts down on installation time and reduces the chance we forget to remove a dependency we no-longer need.
There was a problem hiding this comment.
@ojeytonwilliams I revisited the doc as I was fighting the CI failure, and found that @testing-library/react lists react as a peer dependency.
Sorry, I should have read that more carefully, but that means @testing-library/react needs to stay here inside /ui-components or we will need to install react in the root 😞
There was a problem hiding this comment.
Ah, I see. That makes perfect sense - thanks for figuring that out.
| "files": ["./tools/ui-components/**/*.test.[jt]s?(x)"], | ||
| "extends": ["plugin:testing-library/react", "plugin:jest-dom/recommended"] |
There was a problem hiding this comment.
It is required to enable eslint-plugin-testing-library for test files only (ref). I tested it myself, the plugin will throw errors on non-test files if we don't specify this rule.
As for the jest-dom plugin, it was looking at test files only but a little too eager still, as it errored out on some tests in /api-server.
These plugins can be useful for /client as well, but I decided to scope them down to the component library for now.
| "@testing-library/dom": "7.31.0", | ||
| "@testing-library/jest-dom": "5.12.0", | ||
| "@testing-library/user-event": "13.1.9", |
There was a problem hiding this comment.
There was a problem hiding this comment.
That's fine with me. We need to be careful about the dependencies that make it into production, but we can be much more relaxed with dev dependencies.
| it("should have the role 'button' and the correct text", () => { | ||
| render(<Button label='Hello world' />); | ||
|
|
||
| expect( | ||
| screen.getByRole('button', { name: /hello world/i }) | ||
| ).toBeInTheDocument(); | ||
| }); |
There was a problem hiding this comment.
This isn't in the scope of the PR, but I'm doing a walkthrough anyway, so that you know what my testing approach is.
RTL recommends choosing queries in this order. getByRole is at the top, which means it should be the first go-to query.
In this test, getByRole looks through the DOM tree and finds an element with the ARIA role button and the text Hello world. The role search is important, as it helps prevent cases where a non-button element (div, span, p,...) is used in place of a real button but is not given appropriate ARIA attributes, resulting screen readers not being able to announce the element correctly.
Here are a few implementation examples that will pass this test
<button>Hello world</button>
<div role="button">Hello world</div>
<span role="button">Hello world</span>| /* global expect */ | ||
| /* global jest */ |
There was a problem hiding this comment.
I think I will raise a PR to clean these up for the entire codebase (with eslint-plugin-jest), unless anyone has already worked on this.
| "@testing-library/dom": "7.31.0", | ||
| "@testing-library/jest-dom": "5.12.0", | ||
| "@testing-library/user-event": "13.1.9", |
There was a problem hiding this comment.
That's fine with me. We need to be careful about the dependencies that make it into production, but we can be much more relaxed with dev dependencies.
| "@storybook/addon-links": "6.2.9", | ||
| "@storybook/addon-postcss": "2.0.0", | ||
| "@storybook/react": "6.2.9", | ||
| "@testing-library/react": "11.2.7", |
There was a problem hiding this comment.
Yes please. Ideally all dev dependencies should be in the root if possible. It cuts down on installation time and reduces the chance we forget to remove a dependency we no-longer need.
Co-authored-by: Oliver Eyton-Williams <[email protected]>
ojeytonwilliams
left a comment
There was a problem hiding this comment.
LGTM 👍
Nice work @huyenltnguyen, apologies for the delay merging.
|
@ojeytonwilliams No worries. I was mostly inactive anyway. Thanks for reviewing the PR 😄 |
Checklist:
Update index.md)mainbranch of freeCodeCamp.Ref #41580
This PR:
user-eventpackage above requires this to be installed)To run test on the component library:
Screenshots