1

I know the title is really bad, but I'm just now sure how to explain the situation. Feel free to suggest a better title and I'll change it.

So I just starting to get into testing for the first time, and I'm using Jest. was doing okay till I ran into a construction like this

function f(n) {
    let g;
    if (n === 1) {
        g = () => console.log(`ok`);
    } else {
        g = () => {throw `not okay`;};
    }

    someEvent.listen(async () => {
        g();
    });
}

I'm stuck on how to test that, when I put in something other than 1 to f, it will throw not okay. From what I gather, just the simplest expect(...).toBe(...) will work if the callback to the event isn't async, but I haven't been able to figure out how to do it with it being async.

2
  • your code seems complicated to be tested like the way you want, because in any time, your f function is retuning anything, so you should separate the f function from the someEvent listener. Commented Apr 11, 2020 at 4:30
  • Where does the someEvent comes from?
    – Teneff
    Commented Apr 11, 2020 at 6:15

1 Answer 1

1

Presuming the someEvent is defined as global function

global.someEvent = {
  listen: jest.fn(),
};

you can test it like this:

  1. call the f function with something different than 1 you can assert
  2. that the .listen method have been called with a function retrieve
  3. the given handler from .mock.calls assert that if it's called
  4. should return rejected promise with "not okay"
describe("the f function", () => {
  describe("called with something else", () => {
    beforeAll(() => {
      global.someEvent.listen.mockClear();
      f("x");
    });

    it("set the someEvent handler", () => {
      expect(global.someEvent.listen).toHaveBeenCalledWith(
        expect.any(Function)
      );
    });

    describe("when the someEvent triggers the handler", () => {
      let handler;
      beforeAll(() => {
        [[handler]] = global.someEvent.listen.mock.calls;
      });

      it("should return rejected promise with `not okay`", async () => {
        await expect(handler()).rejects.toEqual("not okay");
      });
    });
  });
});

working example

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.