-
Notifications
You must be signed in to change notification settings - Fork 61
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
Add a basic framework for testing and a single demo test #133
base: master
Are you sure you want to change the base?
Conversation
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.
Hi, thanks for the pr : ) I just have a few questions
package.json
Outdated
@@ -96,7 +104,7 @@ | |||
"start-js": "react-scripts-ts start", | |||
"start": "npm-run-all -p watch-css start-js", | |||
"build": "yarn build-css && react-scripts-ts build && ncp build dist", | |||
"test": "react-scripts-ts test --env=jsdom", |
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.
Why did you replace this with the jest command? Doesn't react-scripts-ts use jest for testing?
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.
There seems to be some problem with the jest config of react-scripts-ts (jestjs/jest#6393). It doesn't really matter since both commands just run jest in watch mode.
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.
Not exactly, react-scripts-ts runs ts-jest not jest
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.
I am using ts-jest as a preset in the jest config
package.json
Outdated
@@ -64,11 +64,19 @@ | |||
"utility-types": "^3.4.1" | |||
}, | |||
"devDependencies": { | |||
"@types/enzyme": "^3.9.1", | |||
"@types/enzyme-adapter-react-16": "^1.0.5", | |||
"enzyme": "^3.9.0", |
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.
Why do we need enzyme? Doesn't react-testing-library cover our use case?
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.
I would also have preferred to use react-testing-library. But I had trouble getting it to work with typescript. Its documentation and examples are not as good or mature as enzyme.
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.
I think jest along with react-testing-library serves the purpose
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.
I like the approach that react-testing library takes to testing. But like I said, it is not as well documented or mature as enzyme is
src/App.test.tsx
Outdated
@@ -1,9 +0,0 @@ | |||
import React from "react"; |
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.
Why did you drop this test?
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.
This test will have to be replaced with an enzyme based one. But that requires some more work for getting the router and store to work in a testing environment. This PR is meant to just establish a base framework.
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.
react-testing-library is an official replacement for enzyme. There's a big blog written by kent c dodds why he discourages enzyme and it's shallow render feature
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.
What do you mean by "official"? React doesn't have an official testing suite. Enzyme is still there along with others in the official react docs. If you can point me to the react-testing-library docs for typescript, I am more than happy to try it out.
ReferenceError: document is not defined
is the error I am getting when I try const { getByTestId } = render(<FixmeNavbar />);
. A quick google for the error doesn't show any helpful results. Any idea why this happens?
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.
What do you mean by "official"? React doesn't have an official testing suite. Enzyme is still there along with others in the official react docs. If you can point me to the react-testing-library docs for typescript, I am more than happy to try it out.
Why do you have to write your tests in that? I guess its not mandatory for writing your tests in typescript
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.
I'll push this configuration as a PR and will also share my fork with you in a moment
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.
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.
When I replace Spinner
with App
, this is the error that I get. This one of the errors I got when I was setting up react-testing-library. Then I followed the official react-testing-library docs regarding react-router but that didn't work either. A similar error occurs when I try to render FixMeNavbar
as well.
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.
@aswinmprabhu can you check if this is still happening at #134 . Though if the test is a snapshot test then switching the component should break the test. You can read more about snapshots here https://jestjs.io/docs/en/snapshot-testing
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.
@aswinmprabhu Could you add this one back? It isn't a huge test but it's important to test that the app doesn't crash
I don't think we are using css modules so I don't feel the need of fileMock |
fileMock is for svg images not css modules.
…On Fri, Mar 22, 2019, 11:27 AM siddhant ***@***.***> wrote:
I don't think we are using css modules so I don't feel the need of fileMock
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#133 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AeGKdn7KKzS-ZsWbBgbBMHpB8HdR11U-ks5vZHDQgaJpZM4b_9Jv>
.
|
I am personally not in favour of react-testing-library , I think we should
use enzyme.
…On Sat, 23 Mar, 2019, 7:13 PM Nikhil Sharma, ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In src/App.test.tsx
<#133 (comment)>:
> @@ -1,9 +0,0 @@
-import React from "react";
I'll push this configuration as a PR and will also share my fork with you
in a moment
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#133 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AdJoBggO38nm49lOMICwdh3IPNwVVj6Qks5vZi9zgaJpZM4b_9Jv>
.
|
Enzyme is good but it poses problems in the long run. |
I don't see any problem with companies working with enzyme , Airbnb uses it and it's not an issue for them. |
Even though enzyme is used by Airbnb and others it doesn't mean that it's always the correct solution. Enzyme is a huge project and a lot of times it's hurtful for the codebase. The tests shouldn't become coupled with the implementation of the component (which a lot of times is happening with enzyme) the tests should test the component as a user interacting with the component so they can be reused if the implementation changes. I would much prefer to use react-testing-library because it's a simpler project and it aligns more with the philosophy of "don't couple your tests with the implementation of the component". |
I managed to get react-testing-library working with typescript. I added back the test for App component as well after setting it up with react-router. |
#126