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

147
Views
How to test if an imported ES6 function is called on useEffect inside the component?

I'm trying to create a simple test in Jest that should check if an imported function is called inside useEffect in the component's lifecycle.

index.js

import { myFunc } from './myFunctions';


const myComponent = () => {

   useEffect(() => {
      myFunc()
   }, [])

   return ()
}

 export default MyComponent

And now I want to create a test index.test.js file to test if that myFunc was called one time. I've got a special function that renders the mocked component (it's called prepare()).

index.test.js

import * as myFunctions from './myFunctions';

describe('when the component renders', () => {

   it('the function should be called once', () => {

       const spy = jest.spyOn(myFunctions, 'myFunc');
       prepare(); // Function rendering myComponent
       expect(spy).toHaveBeenCalledTimes(1);
  });

myFunctions.js

export const myFunc = () => {

   doSomething()

}

...

My error

expect(jest.fn()).toHaveBeenCalledTimes(expected)

    Expected number of calls: 1
    Received number of calls: 0
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!