jest multiple method calls in tests cause wrong expectations

Viewed 3371

just starting to dive into testing world and one thing confuses me. I have class like this:

class TestClass {
  static staticMethod () {
    methodOne();
    methodTwo();
}

and test like this:

test('should call methodOne function', () => {
  TestClass.staticMethod();
  expect(methodOne).toHaveBeenCalled();
});

test('should call methodTwo function', () => {
  Test.staticMethod();
  expect(methodTwo).toHaveBeenCalled();
  expect(methodTwo).toHaveBeenCalledTimes(1);
});

Jest throws an error, that says methodTwo has been called two times instead of one. I Figured, it's because I'm running two tests that calls class static method two times (one time in first test, and second time in second test), hence methodTwo is called two times.

So my question, is it possible somehow to isolate those tests ? When I'm running test one (calling some class method) it shouldn't influence other test results.

Thanks!

1 Answers
Related