首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >跳过Jest中的实现

跳过Jest中的实现
EN

Stack Overflow用户
提问于 2020-01-29 05:22:57
回答 1查看 394关注 0票数 0

目前我有以下一段代码:

代码语言:javascript
复制
function handleConnection(socket: Socket): void {
  info(`Connection started with socketId: ${socket.id}`)

  socket.on("joinRoom", (request: string) => handleJoinRoom(socket, request));

  socket.on("shareData", (request: IShareDataRequest) => handleShareData(socket, request));

  socket.on("disconnect", () => handleDisconnect(socket));
}

我想为像joinRoomshareDatadisconnect这样的每一个事件写一个测试。为了隔离测试,我只想测试第二个socket.on("shareData", () => ...)调用并跳过第一个socket.on("joinRoom", () => ...)调用。我尝试了多种mockImplementationOnce方法,但都没有成功。

我写的测试:

代码语言:javascript
复制
it('should emit data to room', () => {
    const listenMock = listen();
    const socketMock = {
      on: jest.fn()
        .mockImplementationOnce(() => null)
        .mockImplementationOnce((event: string, callback: Function) => callback({ roomId: "abcdefg" })),
      to: jest.fn().mockReturnValue({
        emit: jest.fn()
      })
    }
    jest.spyOn(listenMock, "on").mockImplementationOnce((event: string, callback: Function) => callback(socketMock))

    startSocket();

    expect(socketMock.to).toHaveBeenCalledWith("abcdefg");
    expect(socketMock.to().emit).toHaveBeenCalledWith("receiveData", expect.any(Object));
  })

ShareData函数:

代码语言:javascript
复制
function handleShareData(socket: Socket, request: IShareDataRequest): void {
  socket.to(request.roomId).emit("receiveData", request);
}

如果有人能帮我解决这个问题,我将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-29 16:44:28

您可以尝试以下方法:

代码语言:javascript
复制
// define the mockSocket
const mockSocket = {
  // without any implementation
  on: jest.fn()
};

describe("connection handler", () => {
  // I personally like separating the test setup
  // in beforeAll blocks 
  beforeAll(() => {
    handleConnection(mockSocket);
  });

  // you can write assertions that .on
  // should have been called for each event
  // with a callback
  describe.each(["joinRoom", "shareData", "disconnect"])(
    "for event %s",
    event => {
      it("should attach joinRoom handlers", () => {
        expect(mockSocket.on.mock.calls).toEqual(
          expect.arrayContaining([[event, expect.any(Function)]])
        );
      });
    }
  );

  describe("joinRoom handler", () => {
    beforeAll(() => {
      // jest mock functions keep the calls internally
      // and you can use them to find the call with 
      // the event that you need and retrieve the callback
      const [_, joinRoomHandler] = mockSocket.on.mock.calls.find(
        ([eventName]) => eventName === "joinRoom"
      );

      // and then call it
      joinRoomHandler("mockRequestString");
    });

    it("should handle the event properly", () => {
      // your joinRoom handler assertions would go here
    });
  });
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59957239

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档