Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
Is it is possible to test only one function from component with Jest?

I have react component, for example something like this:

const MyComponent = (props) => {
  const [state, setState] = useState(true);
  const {data} = useContext(myContext);
  const location = useLocation();

  //A lot of code here

  const myFunction = () => {
    return { dataFromContext: data, locationFromUseLocation: location, state: state }
  }

  return <>A lot of other components here</>
}

And I'm trying to write test that should looks like this:

describe('Component test', () => {
 it('myFunction test', () => {
    const wrapper = shallow(<MyComponent/>);
    const expectedResult = {
      dataFromContext: 'any data here', 
      locationFromUseLocation: 'any location here', 
      state: false
};
    expect(wrapper.dive().instance().myFunction()).toEqual(expectedResult);
  })
})

Can I mock useState, useContext and useLocation from <MyComponent/> and pass my custom data instead of real data from real component?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

After deeper researching I've understood, that in such situation I can't write test only for function in my component. So, I've created unit-test, that tests all my component, not only one function.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!