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

162
Views
How to get toHaveBeenCalledTimes(9) with a nested function in jasmine?

I'm trying to make a unit test to check if a function is called a maximum of 9 times, the following code is an approximation of the real code:

function method1( event )
{
     method2(event);
}

function method3( event )
{
    input.value += event.key;
}

function method2(event)
{
    if (input.length < 9)
        method3(event);
}

As you can see the method3() is supposed to be executed only 9 times.

This is an approximation too of the Unit test I'm trying to do:

it("method3 should be called 9 times",() => {
spyOn(component.method1).and.callThrough;
spyOn(component.method2).and.callThrough;
spyOn(component.method3).and.callThrough;

for (var i = 0; i < 20; i++) {
    component.method1(new KeyboardEvent('keydown', {key:'1'}));
}

expect(component.method1).toHaveBeenCalled();
expect(component.method2).toHaveBeenCalled();
expect(component.method3).toHaveBeenCalled();
expect(component.method3).toHaveBeenCalledTimes(9); // with this the TEST DON'T PASS!
});

After execute the tests, the tests fails in the toHaveBeenCalledTimes, because it says: expected to be 9 times, it was called 0 times. I don't know if the reason is because it is a nested function.

Another thing that ocurred too, is that if I do: expect(component.method1).toHaveBeenCalledTimes(20) instead of the (9) with the method3, that test PASS and it is called 20 times.

Can you help me to solve this?

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

0

You are not calling the callThrough() method of the spy. spyOn makes a fake implementation of the method that you want to spy on. Therefore, your call to method2 and method3 never happens from fake method1.

and.callThrough() is used to call the actual method that you are spying on.

In your case, you are not calling the callThrough() method. Instead, you are just referring to callThrough function:

spyOn(component.method1).and.callThrough();//Make the actual call

Why this assertion passesspyOn(component.method1).toHaveBeenCalledTimes(20)?

You are calling the method1 20 times in your for loop. Note that, you are calling the fake implementation 20 times and your test case asserts- Hey, is this method called 20 times?

Karma runner says - Yes, it's called 20 times (fake or real method call, it doesn't matter to him).

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!