Mason
Mason

Reputation: 888

How to test this specific situation in Jest

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.

Upvotes: 1

Views: 55

Answers (1)

Teneff
Teneff

Reputation: 32148

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

Upvotes: 1

Related Questions