-
Notifications
You must be signed in to change notification settings - Fork 114
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
React Component Testing Setup #1049
Conversation
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>
✓ Updated "Verify Inference" Checkmark
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
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.) Since we are using Babel to TS transpilation, |
…onger shipped by default
ensured that tests still pass
with the changes from e-mission#1049, we are now able to test using i18n, no need to mock!
There was a problem hiding this 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?
Updated the related issue (e-mission/e-mission-docs#996) |
bd2ccaa
to
a289789
Compare
@shankari, could you please delete the I followed the steps outlined in this Stack Overflow post for reference: Link to Stack Overflow |
…-mission-phone into react-testing-setup
@JGreenlee |
Just to make sure there was no other weirdness going on, I resolved the conflict myself just now. |
I used the command and kept getting the same issue. This helps a lot! Thanks again |
Setting up React Component Testing
testenvironment
To test React components, we need to change the environment from
node
tojsdom
. 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 thatnode_modules
doesn't have to be transpiled during testing. I resolved this issue by addingtransformIgnorePatterns
.Learn more: Jest Transform Ignore Patterns
Current issue with polyfills/error-guard.js
After adjusting
transformIgnorePatterns
, I resolved most of thenode_modules
errors. However, I still encountered an error related topolyfills/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 theerror-guard polyfill
to run the test.References:
React Native GitHub Issue
Stack Overflow Question
Jest GitHub Issue
Upgrade Support GitHub Issue