Skip to content

feat(tools): setup react testing library in ui-components - #42221

Merged
ojeytonwilliams merged 8 commits into
freeCodeCamp:mainfrom
huyenltnguyen:feat/setup-rtl
Jun 7, 2021
Merged

ojeytonwilliams merged 8 commits into
freeCodeCamp:mainfrom
huyenltnguyen:feat/setup-rtl

Conversation

@huyenltnguyen

@huyenltnguyen huyenltnguyen commented May 22, 2021 •

Copy link
Copy Markdown
Member

Checklist:

  • I have read freeCodeCamp's contribution guidelines.
  • My pull request has a descriptive title (not a vague title like Update index.md)
  • My pull request targets the main branch of freeCodeCamp.
  • I have tested these changes either locally on my machine, or GitPod.

Ref #41580

This PR:

  • Adds the following RTL packages as dev dependencies:
    • @testing-library/react
    • @testing-library/user-event
    • @testing-library/dom (user-event package above requires this to be installed)
    • eslint-plugin-jest-dom
    • eslint-plugin-testing-library
  • Adds a test file to the example button component to verify that the config works

To run test on the component library:

npm run test-ui-components

Screenshots

Screen Shot 2021-05-23 at 05 41 32

@gitpod-io

gitpod-io Bot commented May 22, 2021

Copy link
Copy Markdown

@github-actions github-actions Bot added the scope: tools/scripts Scripts for supporting dev work, generating config and build artifacts, etc. label May 22, 2021
@huyenltnguyen
huyenltnguyen force-pushed the feat/setup-rtl branch 2 times, most recently from abe6bac to 1f613e6 Compare May 22, 2021 22:55
Comment thread package.json Resolved
Comment thread package.json Resolved
Comment thread package.json
"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",

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RTL recommends this plugin. It helps guide us to choose the appropriate assertion for our unit tests.

Doc: https://testing-library.com/docs/ecosystem-eslint-plugin-jest-dom

Comment thread package.json
"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",

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also recommended by RTL. It helps guide us to use the most appropriate query API to find the element we need in our tests.

Doc: https://testing-library.com/docs/ecosystem-eslint-plugin-testing-library

"@storybook/addon-links": "6.2.9",
"@storybook/addon-postcss": "2.0.0",
"@storybook/react": "6.2.9",
"@testing-library/react": "11.2.7",

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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 😞

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. That makes perfect sense - thanks for figuring that out.

Comment thread .eslintrc.json Outdated
Comment on lines +55 to +56
"files": ["./tools/ui-components/**/*.test.[jt]s?(x)"],
"extends": ["plugin:testing-library/react", "plugin:jest-dom/recommended"]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread package.json
Comment on lines +106 to +109
"@testing-library/dom": "7.31.0",
"@testing-library/jest-dom": "5.12.0",
"@testing-library/user-event": "13.1.9",

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RTL recommends using user-event over it built-in fireEvent method for user interaction simulation (ref). @testing-library/user-event lists @testing-library/dom as a peer dependency, so we need to install it as well (ref).

I'm happy to take them out if we think they are too bloated.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +11 to +17
it("should have the role 'button' and the correct text", () => {
render(<Button label='Hello world' />);

expect(
screen.getByRole('button', { name: /hello world/i })
).toBeInTheDocument();
});

@huyenltnguyen huyenltnguyen May 22, 2021 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Comment on lines +1 to +2
/* global expect */
/* global jest */

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@huyenltnguyen
huyenltnguyen marked this pull request as ready for review May 22, 2021 23:29
@huyenltnguyen
huyenltnguyen requested a review from a team May 22, 2021 23:29
@huyenltnguyen huyenltnguyen mentioned this pull request May 22, 2021
7 of 43 tasks
Comment thread package.json Resolved
Comment thread package.json
Comment on lines +106 to +109
"@testing-library/dom": "7.31.0",
"@testing-library/jest-dom": "5.12.0",
"@testing-library/user-event": "13.1.9",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions github-actions Bot added platform: learn UI side of the client application that needs familiarity with React, Gatsby etc. and removed platform: learn UI side of the client application that needs familiarity with React, Gatsby etc. labels May 24, 2021
@raisedadead
raisedadead requested a review from ahmaxed June 3, 2021 14:22
@ahmaxed ahmaxed self-assigned this Jun 4, 2021
@ahmaxed
ahmaxed self-requested a review June 6, 2021 17:09
@ojeytonwilliams
ojeytonwilliams enabled auto-merge (squash) June 7, 2021 07:00

@ojeytonwilliams ojeytonwilliams left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Nice work @huyenltnguyen, apologies for the delay merging.

@ojeytonwilliams
ojeytonwilliams merged commit fdf1de3 into freeCodeCamp:main Jun 7, 2021
@huyenltnguyen
huyenltnguyen deleted the feat/setup-rtl branch June 7, 2021 11:48
@huyenltnguyen

Copy link
Copy Markdown
Member Author

@ojeytonwilliams No worries. I was mostly inactive anyway.

Thanks for reviewing the PR 😄

@ahmaxed ahmaxed mentioned this pull request Jan 4, 2022
37 of 49 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: tools/scripts Scripts for supporting dev work, generating config and build artifacts, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants