0

I have a situation where I have 2 functions, A and B, where A is dependent on B. For example, let's say this is my file under test:

myModule.js:

const A = async () => {
  return "foo"
}

const B = async () => {
  return "bar-" + (await A())
}

I would like to write a unit test suite to test this module. I would like to write 2 unit tests:

  1. A unit test for A that ensures that A always returns "foo".
  2. A unit test for B that mocks A using a Jest mock function, changes A's return value to something else (e.g. "baz"), and ensures that B's return value is "bar-baz" and that A has been called (using Jest's mock calls functionality).

How do I do this? I've tried a whole bunch of things and the closest I've gotten is that I've been able to mock A but B still calls the original A.

(note that A and B are both async functions, in my real code, which is obviously not posted here, A and B do real async operations)

Ertai87
  • 898
  • 1
  • 10
  • 25

0 Answers0