Rishabh-malhotraa / caucus

Realtime Collaborate Editor with Embedded Compiler
https://caucus.rishabhmalhotra.in/
MIT License
295 stars 70 forks source link

Add basic Unit Tests #45

Open Rishabh-malhotraa opened 2 years ago

Rishabh-malhotraa commented 2 years ago

Requirement

GregDevProjects commented 2 years ago

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

Rishabh-malhotraa commented 2 years ago

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 commented 2 years ago

@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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

That's great, go ahead.