While Jest: spyOn is listening on a method, other test also calls the listening method and causes the test to fail

method to be tested

   

question

  1. does the result prove that Test 2 during monitoring the k method, if other tests also call k , they will also be logged, so they are called twice, causing the test to fail?
  2. but if I swap Test 1 and Test 2 to ok again, why?
  3. is there a way for Test 1 and Test 2 to be executed in Synchronize? So there will be no problem

Thank you, big shot ~

Mar.05,2022

1. According to the code execution process, listening is executed at the beginning, followed by test 1 of 100ms, and then test 2 calls the sum.k (), of a 200ms, so the toBeCalledTimes after callback should be equal to 2
2. I don't know whether it is because the time of settimeout is not long enough
3, I think it is effective for promise to use async await to wait for asynchronism, but whether it is effective for settimeout to be tested

Menu