jestjs 的超时问题

Timeout issue with jestjs

我正在使用 jest 为一个函数编写测试,但我一直遇到超时问题。我试图通过向测试函数中的第二个参数添加更大的数字以及更改 package.json 文件中的 jest 配置设置来解决此问题,但两者均无效。任何帮助将不胜感激。

测试文件

const favoriteBlog = require('../utils/list_helper').favoriteBlog

describe(("fav blog"), () => {

    const listWithOneBlog = [
        {
          _id: "5a422a851b54a676234d17f7",
          title: "React patterns",
          author: "Michael Chan",
          url: "https://reactpatterns.com/",
          likes: 7,
          __v: 0
        },
        {
          _id: "5a422aa71b54a676234d17f8",
          title: "Go To Statement Considered Harmful",
          author: "Edsger W. Dijkstra",
          url: "http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html",
          likes: 5,
          __v: 0
        },
        {
          _id: "5a422b3a1b54a676234d17f9",
          title: "Canonical string reduction",
          author: "Edsger W. Dijkstra",
          url: "http://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD808.html",
          likes: 12,
          __v: 0
        },
        {
          _id: "5a422b891b54a676234d17fa",
          title: "First class tests",
          author: "Robert C. Martin",
          url: "http://blog.cleancoder.com/uncle-bob/2017/05/05/TestDefinitions.htmll",
          likes: 10,
          __v: 0
        },
        {
          _id: "5a422ba71b54a676234d17fb",
          title: "TDD harms architecture",
          author: "Robert C. Martin",
          url: "http://blog.cleancoder.com/uncle-bob/2017/03/03/TDD-Harms-Architecture.html",
          likes: 0,
          __v: 0
        },
        {
          _id: "5a422bc61b54a676234d17fc",
          title: "Type wars",
          author: "Robert C. Martin",
          url: "http://blog.cleancoder.com/uncle-bob/2016/05/01/TypeWars.html",
          likes: 2,
          __v: 0
        }  
      ]
    test("find the blog with the most likes", (blog) => {
        
        const expected = {
            title: "Canonical string reduction",
            author: "Edsger W. Dijkstra",
            likes: 12
        }
        const result = favoriteBlog(listWithOneBlog)
        expect(result).toEqual(expected)
    })
})

函数文件

const favoriteBlog = (blog) => {
    console.log("before is",blog.map(a => a.likes) )
    const preArray = blog.map(a => a.likes)
    console.log("Math max thing is", Math.max(...preArray))
    const objArr = Math.max(...preArray)

    console.log("objArr is",objArr)

    const favBlog = blog.filter((x) => x.likes == objArr);
    const favBlogObj = favBlog[0]

    delete favBlogObj._id;
    delete favBlogObj.__v;
    delete favBlogObj.url;

    console.log("Fav blog is now",favBlog)


    return favBlogObj

}

module.exports = {
    favoriteBlog
}

测试失败,因为您在测试回调中有一个 blog 参数,Jest 将其解释为您必须调用以结束测试的回调函数。测试没有使用该参数,所以我认为这是一个错误,应该删除。如果您打算使用回调,则只需将 blog() 添加为测试中的最后一条语句(也许将其重命名为 done 这是惯例)。

完成回调用于一些 API 样式,但在大多数情况下你不需要这个参数并且可以结束测试,或者 return 如果你期待一些东西稍后结束。

    test("find the blog with the most likes", (blog) => {
        //                                     ^^^^ HERE
        const expected = {
            title: "Canonical string reduction",
            author: "Edsger W. Dijkstra",
            likes: 12
        }
        const result = favoriteBlog(listWithOneBlog)
        expect(result).toEqual(expected)
    })