Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React Component Testing Setup #1049

Merged
merged 18 commits into from
Oct 17, 2023

Conversation

jiji14
Copy link
Contributor

@jiji14 jiji14 commented Oct 3, 2023

Setting up React Component Testing

testenvironment

To test React components, we need to change the environment from node to jsdom. Since we perform both non-UI function tests and UI function tests, it's better to change the environment in each component test file by adding a @jest-environment docblock at the beginning of the file.

Learn more: Jest Test Environment Configuration

transformIgnorePatterns

During testing, I encountered multiple errors related to node_modules. This is because some react-native libraries ship uncompiled ES5 code, but Jest requires ES6 code for testing. It's important to ensure that node_modules doesn't have to be transpiled during testing. I resolved this issue by adding transformIgnorePatterns.

Learn more: Jest Transform Ignore Patterns

Current issue with polyfills/error-guard.js

After adjusting transformIgnorePatterns, I resolved most of the node_modules errors. However, I still encountered an error related to polyfills/error-guard.js. This issue appears to be common, and several people have faced similar challenges. While there are suggestions available, nothing worked for my scenario.

The additional code in polyfills/error-guard.js is not Typescript, it is flow (flow is what FB uses instead of Typescript). We need to remove any flow syntax on the error-guard polyfill to run the test.

References:
React Native GitHub Issue
Stack Overflow Question
Jest GitHub Issue
Upgrade Support GitHub Issue

the-bay-kay and others added 8 commits September 28, 2023 14:23
See [Issue e-mission#979](e-mission/e-mission-docs#979 (comment)) for more details.
Only slight difference in comment; removed the `-0.2rem` in this commit,
the buttons look crowded otherwise.
Updated the button to only appear if you can verify the trip.
Button color now matches the "inferred trip" data.
…ept_2023

📈 👔 ✅ Dashboard rewrite (complete) + profile logic improvements + initial testing framework!
See conversation in PR
 `rem` doesn't work properly in react-native, updated

Co-authored-by: Jack Greenlee <JackAGreenlee@gmail.com>
iconButtonProp & Border Color
Changed prop to use native property, adjusted border color.

Co-authored-by: Jack Greenlee <JackAGreenlee@gmail.com>
@jiji14 jiji14 marked this pull request as draft October 4, 2023 19:36
shankari and others added 2 commits October 4, 2023 21:07
Some of the core React and React Native code is written in Flow, and the official, public packages have untranspiled Flow code. Bad!
Jest can't understand this when it tries to test our React code. So unfortunately we have to transpile it ourselves before running tests, using Babel with a couple different presets and plugins

- add babel.config.js
- change Jest config from json -> js
- add packages to support the Babel transpilation
@JGreenlee
Copy link
Collaborator

It was necessary to use Babel with Jest because otherwise, Jest cannot understand the Flow code that comes from the core React and React Native packages. (Why FB doesn't transpile it themselves before publishing it, I do not know.)
The Babel presets and plugins now used are: @babel/preset-env, @babel/preset-typescript, @babel/preset-react, and @babel/plugin-transform-flow-strip-types.

Since we are using Babel to TS transpilation, ts-jest is moot and can be removed.

@jiji14
Copy link
Contributor Author

jiji14 commented Oct 5, 2023

I also added "jest-environment-jsdom" in configuration since "jest-environment-jsdom" is no longer shipped by default.

Screenshot 2023-10-05 at 10 38 43 AM

Now we are ready for React Component testing! 🥳 🥳

@jiji14 jiji14 marked this pull request as ready for review October 5, 2023 17:46
Abby-Wheelis pushed a commit to Abby-Wheelis/e-mission-phone that referenced this pull request Oct 13, 2023
ensured that tests still pass
Abby-Wheelis pushed a commit to Abby-Wheelis/e-mission-phone that referenced this pull request Oct 13, 2023
with the changes from e-mission#1049, we are now able to test using i18n, no need to mock!
Copy link
Contributor

@shankari shankari left a comment

Choose a reason for hiding this comment

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

@jiji14 great job taking this on and getting it to work! I am really excited to merge this and make sure that we can start testing components.

This is almost ready to review - here are a couple of high level comments:

  • Can we document this some more?
    - Is this the standard option to test react components?
    - which other testing options did you consider?
    - what was the reason for picking this one?
  • Can you also resolve the merge conflict so that I merge?

jest.config.js Show resolved Hide resolved
@jiji14 jiji14 marked this pull request as draft October 16, 2023 16:54
@jiji14
Copy link
Contributor Author

jiji14 commented Oct 16, 2023

@jiji14 great job taking this on and getting it to work! I am really excited to merge this and make sure that we can start testing components.

This is almost ready to review - here are a couple of high level comments:

  • Can we document this some more?
    • Is this the standard option to test react components?
    • which other testing options did you consider?
    • what was the reason for picking this one?
  • Can you also resolve the merge conflict so that I merge?

Updated the related issue (e-mission/e-mission-docs#996)

@jiji14 jiji14 marked this pull request as ready for review October 16, 2023 23:34
@jiji14
Copy link
Contributor Author

jiji14 commented Oct 17, 2023

jest.config.json file was deleted, leading to a conflict error when attempting to resolve it. I tried adding the file with empty content and then removing it, but the conflict error still persists, and I find myself stuck with this issue 😢

@shankari, could you please delete the jest.config.json file when you merge this?

I followed the steps outlined in this Stack Overflow post for reference: Link to Stack Overflow

@jiji14
Copy link
Contributor Author

jiji14 commented Oct 17, 2023

@JGreenlee
Thanks! How did you resolve the conflict with jest.config.json file?

@JGreenlee
Copy link
Collaborator

Just to make sure there was no other weirdness going on, I resolved the conflict myself just now.
@jiji14 Below, you can see the process by which I did this. There are many ways you can work with Git, including git commands or Github desktop, but the built-in source control tab in VSCode is my personal preference.

image image image image

@jiji14
Copy link
Contributor Author

jiji14 commented Oct 17, 2023

Just to make sure there was no other weirdness going on, I resolved the conflict myself just now. @jiji14 Below, you can see the process by which I did this. There are many ways you can work with Git, including git commands or Github desktop, but the built-in source control tab in VSCode is my personal preference.

image image image image

I used the command and kept getting the same issue. This helps a lot! Thanks again

@shankari shankari merged commit e2a2fde into e-mission:service_rewrite_2023 Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants