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

241
Views
How can I write useEffect in test code (jest)?
// MyCode.tsx
const MyCode = (props) => {
    const { useEffect, useRef } = React;
    const { count, setCount } = useState(0)
    const elementRef = useRef(null);

    useEffect(() => {
        setCount(1);
    }, [elementRef]);

    const getFunc() {
        return {
            name: 'Mike',
        }
    }

    return (
        <React.Fragment>
            <div className="section" ref={elementRef}>
                ....
            </div>
        </React.Fragment>
    );
}
// MyCode.test.js
import * as React from 'react';
import { render, mount, shallow, shallowWrapper } from 'enzyme';
import MyCode from '../MyCode';

describe('TEST START', function() {
    let wrapper;

    beforeEach(() => {
        wrapper = shallow(
            <MyCode />,
        );
    });

    it('TEST1', () => {
        expect(wrapper.find('.section').length).toEqual(1);
    });

    it('TEST2', () => {
        // useEffect(() => {
        //     setCount(1);
        // }, [elementRef]);
    });

    it('TEST3', () => {
        // const getFunc() {
        //     return {
        //         name: 'Mike',
        //     }
        // }
    });
});

I have some questions. It is difficult to write a test code.

Q1. (TEST2) How can I write useEffect in test code (jest)? I want to get the changed state value.
Q2. (TEST3) How can I get the return value of the getFunc function? I can't do it like this.

expect(wrapper.getFunc()).toBe({ name: 'Mike' })

help me...

about 4 years ago · Juan Pablo Isaza
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!