1

Writing unit tests for a new component in React

Profile picture
Anonymous User at Taro Community2 years ago

Hello,

I am an Entry Level Engineer at a mid sized company. The thing is, my team has a very strict deadline which we have to meet at all costs. For more context, I have been with this team for around a year now. I had done some menial tasks of building small easy features and styling the components in React. I have a task now where I am supposed to write unit tests for a particular component from scratch in jest. ( I think this is a learning opportunity in disguise but concerned if I might not able to finish the task, as I already got a call out in team standup that my area is lagging behind )

The thing is I have not done this kind of task before. I am super overwhelmed with this kind of situation and everyone in the team is expecting me to deliver it asap as we have a strict deadline.

I tried to convey that this is something of a new task I am trying to a senior engineer to understand how they solve these kind of problems, but was mocked in return and was told I should already know (I agree to some extent ).

The approach I am trying right now is to understand the functionality of that part of component, trying to understand the code written for that and start making my own tests cases. I am also looking at the other unit tests written for other components but feeling super overwhelmed as I am clueless what to do. Could someone please advise me how to approach this problem ?

248
4

Discussion

(4 comments)
  • 5
    Profile picture
    Senior Software Engineer [E5] at Meta
    2 years ago
    • Give feedback to your manager if you felt put down by the senior engineer. That is not normal - their responsibility is to help. Sorry you had to deal with that

    • When writing unit tests for a react component, I would break it down into two steps:

    • First, get the simplest test you can think of to run. For example, if your component included an element with id #my-component, verify that after mounting that component, container.getElementById(#my-component) returns nonnull. This will get you able to understand how the tests are ran, and the particular nuances of whatever test runner you're dealing with.

    • Once youve got a simple test, then you can start looking at the possible test cases to include. I would again start simple here - make sure that the most basic pieces of functionality work, and that all elements are visible.

    In both steps I would look for a lot of help if you're feeling lost. If possible ask other team members besides the engineer who was rude, but ask your manager if no one is able to help. Google and stackoverflow and docs are always your friends.

    You're not alone - writing a unit test for a new component is tricky. Good luck for solving the problem

  • 5
    Profile picture
    Senior Software Engineer [L5] at Google
    2 years ago

    Want to echo everyone else's point that the senior engineer is abusing their position and should be helping you instead - either directly on these problems or indirectly by pointing you to the correct resources.

    From your perspective though, blaming/harboring bitterness toward your senior engineer might not be helpful. I also want to echo Michael's point -

    Trying to figure it out all on your right now is the worst thing you can do

    If you are overwhelmed and don't know how to accomplish your assignment, you have to tell your team/manager/peers you need help. That you can't figure it out on your own. It is stressful to admit it, potentially in front of everyone at a standup, but you have to look at the bigger picture - both you and your team benefits by you learning how to do it properly. Sometimes, it doesn't matter what "should" be the case - it's not the case now, so you and your team have to muster the courage face reality like adults and solve the problem together.

    If your seniors/managers are unable/unwilling to help you even after this, do you have any peers on your team who have written react tests before? If there's no one on your direct team, are there anyone in adjacent teams? Ask for a favor from them, "bribe" them with coffee or whatever, and have them show you how.

    Later - if you wish address this relationship with your senior engineer, checkout https://link.jointaro.com/gKieVqcwbH2SYc1t8 and other resources here on Taro about resolving conflicts between peers.

  • 4
    Profile picture
    Startup Engineer
    2 years ago

    The approach I am trying right now is to understand the functionality of that part of component, trying to understand the code written for that and start making my own tests cases. I am also looking at the other unit tests written for other components but feeling super overwhelmed as I am clueless what to do. Could someone please advise me how to approach this problem ?

    This is probably not an effective enough way to get started. What I probably would do is ask for 30 minutes from someone who has experience writing unit tests (and is really nice) and write some tests together. You need some help unstucking yourself if you're getting called out in standup and if you're entry-level, why not take advantage of the fact that you're "still learning"?

    Trying to figure it out all on your own right now is the worst thing you can do because:

    1. It'll stunt your growth as you'll need practice in communicating that you need help.
    2. It doesn't feel good to not be absolutely crushing it at your job and the stress will negatively affect your health.
    3. It's really inefficient to thrash on "solved" problems.
    4. Our own feelings of anxiety can weigh down other people.
    5. You have to respect other people's hard work and match them to some degree, even at different seniority levels. If the gap in expectations is too wide, you might want to ask whether the company is too demanding or if you need to put in more hours—and then ask yourself whether that is the kind of WLB you want.
  • 0
    Profile picture
    Anonymous User [OP]
    Taro Community
    2 years ago

    Thanks a lot for your inputs. This really helps me. I will try to follow this.