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

Add a basic framework for testing and a single demo test #133

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

aswinmprabhu
Copy link

Copy link
Member

@alexdor alexdor left a 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",
Copy link
Member

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?

Copy link
Author

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.

Copy link
Member

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

Copy link
Author

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",
Copy link
Member

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?

Copy link
Author

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.

Copy link
Member

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

Copy link
Author

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";
Copy link
Member

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?

Copy link
Author

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.

Copy link
Member

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

Copy link
Author

@aswinmprabhu aswinmprabhu Mar 23, 2019

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?

Copy link
Member

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

Copy link
Member

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

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

image
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.

Copy link
Member

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

Copy link
Member

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

@siddhant1
Copy link
Collaborator

I don't think we are using css modules so I don't feel the need of fileMock

@aswinmprabhu
Copy link
Author

aswinmprabhu commented Mar 22, 2019 via email

@siddhant1
Copy link
Collaborator

siddhant1 commented Mar 23, 2019 via email

@nik72619c
Copy link
Member

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 <github.com/notifications/unsubscribe-auth/AdJoBggO38nm49lOMICwdh3IPNwVVj6Qks5vZi9zgaJpZM4b_9Jv> .

Enzyme is good but it poses problems in the long run.

@siddhant1
Copy link
Collaborator

I don't see any problem with companies working with enzyme , Airbnb uses it and it's not an issue for them.

@alexdor
Copy link
Member

alexdor commented Mar 25, 2019

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

@aswinmprabhu
Copy link
Author

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.

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.

None yet

4 participants