抛出 Jest 错误:“测试超时超过 5000 毫秒。为此测试添加超时值以增加超时

问题描述 投票:0回答:1

我试图找出article.controller.js 文件测试失败的原因。但是,找不到原因。

[文件:article.controller.js]

const {
  saveArticle,
  updateArticle,
  removeArticle,
  getArticle,
  getAllTags,
  getAllArticles,
  feedArticles,
  favoriteArticle,
  unFavoriteArticle,
} = require('../services/article.service');

exports.editArticle = async (req, res, next) => {
  const slugText = req.params.slug;
  if (slugText) {
    return updateArticle(slugText, req.user._id, req.body.article)
      .then((isUpdated) => {
        if (isUpdated) {
          return res.status(200).json({ article: isUpdated });
        }
      })
      .catch(next);
  }
};

[文件:article.controller.test.js]

const articleServiceHelper = require("../../../services/article.service");

const mockResponse = () => {
  return {
    status: jest.fn().mockReturnThis(),
    json: jest.fn().mockReturnThis()
  };
};

const mockNext = jest.fn();

const mockUser = {
  _id: "6368ad4347dkhs"
};

const mockUpdatedArticle = {
  title: "Updated article title",
  description: "Updated description",
  body: "Updated body"
}

describe("Unit tests for article.controller.js file", () => {
  describe("editArticle tests", () => {
    test("If user can edit an article", async () => {
      //Arrange
      const mockReq = { params: { slug: 'test-article-slug' }, user: { _id: "6368ad4347dkhs" }, body: { article: mockUpdatedArticle } };
      const mockRes = mockResponse();
      const updateArticleSpy = jest.spyOn(articleServiceHelper, "updateArticle").mockResolvedValue(mockUpdatedArticle);
      const { editArticle } = require("../../../controllers/article.controller");
      //Act
      const updatedArticle = await editArticle(mockReq, mockRes, mockNext);
      //Assert
      expect(updatedArticle).toBe(mockUpdatedArticle);
      expect(updateArticleSpy).toHaveBeenCalledTimes(1);
      expect(updateArticleSpy).toHaveBeenCalledWith(mockReq.params.slug, mockUser._id, mockReq.body.article);
      expect(mockRes.status).toHaveBeenCalledWith(200);
      expect(mockRes.json).toHaveBeenCalledWith({
        article: mockUpdatedArticle
      });
      expect(mockNext).not.toHaveBeenCalled();
    });
  });
});

[输出]

抛出:“测试超时超过 5000 毫秒。 如果这是长时间运行的测试,请为此测试添加超时值以增加超时。请参阅 https://jestjs.io/docs/api#testname-fn-timeout。”

  47 |   });
  48 |   describe("editArticle tests", () => {
> 49 |     test("If user can edit an article", async () => {

我想测试 editArticle 函数,但出现超时错误。请帮忙。

node.js unit-testing jestjs
1个回答
0
投票

终于,我解决了我的问题。我将 updateArticleSpy 放在测试用例之外。另外,稍微更改了测试代码。

下面是 article.controller.test.js 文件的更新代码。

    const articleServiceHelper = require("../../../services/article.service");

    const mockResponse = () => {
      return {
        status: jest.fn().mockReturnThis(),
        json: jest.fn().mockReturnThis()
      };
    };

    const mockNext = jest.fn();

    const mockUser = {
      _id: "6368ad4347dkhs"
    };

    const mockArticle = {
      title: "Sample article title",
      description: "Sample article description",
      body: "Sample article body"
    }

    const mockUpdatedArticle = {
      title: "Updated article title",
      description: "Updated description",
      body: "Updated body"
    }
    const updateArticleSpy = jest.spyOn(articleServiceHelper, "updateArticle").mockResolvedValue(mockUpdatedArticle);
    describe("Unit tests for article.controller.js file", () => {
      describe("editArticle tests", () => {
        test("If user can edit an article", async () => {
          //Arrange
          const mockReq = { params: { slug: 'test-article-slug' }, user: { _id: "6368ad4347dkhs" }, body: { article: mockUpdatedArticle } };
          const mockRes = mockResponse();
          const { editArticle } = require("../../../controllers/article.controller");
          //Act
          await editArticle(mockReq, mockRes, mockNext);
          //Assert
          expect(updateArticleSpy).toHaveBeenCalledTimes(1);
          expect(updateArticleSpy).toHaveBeenCalledWith(mockReq.params.slug, mockUser._id, mockReq.body.article);
          expect(mockRes.status).toHaveBeenCalledWith(200);
          expect(mockRes.json).toHaveBeenCalledWith({
            article: mockUpdatedArticle
          });
          expect(mockNext).not.toHaveBeenCalled();
        });
      });
    });
© www.soinside.com 2019 - 2024. All rights reserved.