为什么Jest看不到函数调用?

huangapple go评论53阅读模式
英文:

Why doesn't Jest see the function call?

问题

我有一个带有 login 方法的类:

import authService from "../service/auth";

class AuthController {
  async login(req, res, next) {
    try {
      const { email, password } = req.body;
      const token = await authService.login(email, password);
      res.cookie("token", token, {
        maxAge: 30 * 24 * 60 * 60 * 1000,
        httpOnly: true,
      });
      return res.status(200).json();
    } catch (e) {
      next(e);
    }
  }
}

export = new AuthController();

以及测试:

import AuthController from "./auth.controller";
import authService from "../service/auth";

describe("AuthController.login", () => {
  it("如果成功获取到令牌,应该调用res.status(200).json()", () => {
    const next = jest.fn();
    const req = {
      body: {
        email: "email@example.com",
        password: "1234",
      },
    };
    const res = { cookie: jest.fn() };

    jest.spyOn(authService, "login").mockResolvedValue("t0ken");

    AuthController.login(req, res, next);

    expect(res.cookie).toBeCalledWith("token", "t0ken", {
      maxAge: 30 * 24 * 60 * 60 * 1000,
      httpOnly: true,
    });
  });
});

问题在于 Jest 声明 res.cookie 未被调用

但在调试时,您可以看到调用正在发生:

如果我注释掉:

const token = await authService.login(email, password);

并将 token 常量的值传递给:

测试将会通过。

我认为我在异步函数 await autoservice.login(email, password) 上错误地进行了监听。您能告诉我在哪里出错以及为什么我的测试不起作用吗?

英文:

I have a class with the login method:

import authService from "../service/auth";

class AuthController {
  async login(req, res, next) {
    try {
      const { email, password } = req.body;
      const token = await authService.login(email, password);
      res.cookie("token", token, {
        maxAge: 30 * 24 * 60 * 60 * 1000,
        httpOnly: true,
      });
      return res.status(200).json();
    } catch (e) {
      next(e);
    }
  }
}

export = new AuthController();

And the test:

import AuthController from "./auth.controller";
import authService from "../service/auth";

describe("AuthController.login", () => {
  it("If it turned out to get a token, then a call should occur res.status(200).json()", () => {
    const next = jest.fn();
    const req = {
      body: {
        email: "email@example.com",
        password: "1234",
      },
    };
    const res = { cookie: jest.fn() };

     jest.spyOn(authService, "login").mockResolvedValue("t0ken");

    AuthController.login(req, res, next);

    expect(res.cookie).toBeCalledWith("token", "t0ken", {
      maxAge: 30 * 24 * 60 * 60 * 1000,
      httpOnly: true,
    });
  });
});

The problem is that Jest claims that res.cookie is not called.

为什么Jest看不到函数调用?

But when debugging, you can see that the call is happening:

为什么Jest看不到函数调用?

If I comment out:

const token = await authService.login(email, password);

and I will enter the value of the token constant in:

为什么Jest看不到函数调用?

The test will be passed.

I assume that I am incorrectly spying on the asynchronous function await autoservice.login(email, password). Can you tell me where I'm wrong and why my test doesn't work?

答案1

得分: 1

以下是翻译好的部分:

有一个问题 - AuthController.login 是一个异步方法 async login(req, res, next),而我编写了一个同步测试:

it('如果成功获取令牌,那么应该调用 res.status(200).json()', () => {test...}

因此,正确的测试应该如下:

import AuthController from "./auth.controller";
import authService from "../service/auth";

describe("AuthController.login", () => {
  it("如果成功获取令牌,那么应该调用 res.status(200).json()", async () => {
    const next = jest.fn();
    const req = {
      body: {
        email: "email@example.com",
        password: "1234",
      },
    };
    const res = { cookie: jest.fn() };

    jest.spyOn(authService, "login").mockResolvedValue("token");

    await AuthController.login(req, res, next);

    expect(res.cookie).toBeCalledWith("token", "token", {
      maxAge: 30 * 24 * 60 * 60 * 1000,
      httpOnly: true,
    });
  });
});

为什么Jest看不到函数调用?

英文:

There was only one problem -AuthController.login is an asynchronous method async login(req, res, next) , and I wrote a
synchronous test:

it('If it turned out to get a token, then a call should occur res.status(200).json()', () => {test...}

Therefore, the correct test will look like this:

import AuthController from "./auth.controller";
import authService from "../service/auth";

describe("AuthController.login", () => {
  it("If it turned out to get a token, then a call should occur res.status(200).json()", async () => {
    const next = jest.fn();
    const req = {
      body: {
        email: "email@example.com",
        password: "1234",
      },
    };
    const res = { cookie: jest.fn() };

    jest.spyOn(authService, "login").mockResolvedValue("t0ken");

    await AuthController.login(req, res, next);

    expect(res.cookie).toBeCalledWith("token", "t0ken", {
      maxAge: 30 * 24 * 60 * 60 * 1000,
      httpOnly: true,
    });
  });
});

为什么Jest看不到函数调用?

huangapple
  • 本文由 发表于 2023年6月13日 16:06:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462869.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定