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 basic Unit Tests #45

Open
Rishabh-malhotraa opened this issue Oct 5, 2021 · 7 comments
Open

Add basic Unit Tests #45

Rishabh-malhotraa opened this issue Oct 5, 2021 · 7 comments

Comments

@Rishabh-malhotraa
Copy link
Owner

Requirement

  • Currently Caucus does not have any unit testing
  • Implement basic unit testing.
@GregDevProjects
Copy link

@Rishabh-malhotraa we could use Jest to mock services and generate snapshots for each page. Was this what you had in mind?

@Rishabh-malhotraa
Copy link
Owner Author

Well, I haven't really done a lot of testing myself. I need to do some research about this; I was just thinking of a basic unit test to tell if the updates crash the app or not. Do you mind elaborating your approach a bit?

@GregDevProjects
Copy link

GregDevProjects commented Oct 13, 2021

@Rishabh-malhotraa sure!

Using this approach, Jest will render a React component using a lightweight test render and generate an HTML output, this output is referred to as a snapshot. Asynchronous operations like API responses should be mocked to ensure that the tests run quickly and only app functionality is tested, not the APIs the app calls.

Each time a test is run, the component is rendered and compared to the snapshot. If they are not equal, the test fails. At this point you'll need to decide if the changes to the HTML were intended, if they were, you'll need to update the snapshot to the render result of the latest test run.

Pros of snapshot testing:

  1. Tests can be created quickly
  2. Unintended changes are quickly revealed
  3. Component logic can be tested without extracting the code into separate functions

Cons of snapshot testing:

  1. Repository size will increase significantly (the snapshots must be stored in the repo)
  2. It can be difficult to track down why the test failed, especially if the tested component has alot of logic

Sorry if I'm not doing a great job explaining this, check the docs for more info

@GregDevProjects
Copy link

GregDevProjects commented Oct 13, 2021

I suggested this because it would (imo) be the fastest way to setup some initial tests that can verify if new changes break the app or have unintended side effects.

Ideally you'd have unit tests as well to test individual classes/methods, I just thought snapshots might be a good starting point. I'm fairly new to client side testing too though - I'm interested to hear what you think 😄

@MarufSharifi
Copy link
Collaborator

MarufSharifi commented Oct 14, 2021

I suggested this because it would (imo) be the fastest way to setup some initial tests that can verify if new changes break the app or have unintended side effects.

Ideally you'd have unit tests as well to test individual classes/methods, I just thought snapshots might be a good starting point. I'm fairly new to client side testing too though - I'm interested to hear what you think smile

Using just snapshot testing is not quite helpful, try to test all API calls, fire events, state of app, existence of children in different renders and other functionality of the app, i will join you to write unit test for all the components in coming days, please spend a little time on React Testing Library, it will help to guide you in a right way.

https://testing-library.com/docs/react-testing-library/intro/

@GregDevProjects
Copy link

Using just snapshot testing is not quite helpful, try to test all API calls, fire events, state of app, existence of children in different renders and other functionality of the app, i will join you to write unit test for all the components in coming days, please spend a little time on React Testing Library, it will help to guide you in a right way.

https://testing-library.com/docs/react-testing-library/intro/

Sounds good, I'll start playing around with React Testing Library 😄

This will be alot of work! Might I suggest breaking this down into smaller issues so the PRs are easier to review?

@MarufSharifi
Copy link
Collaborator

That's great, go ahead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants