javascript 使用 Node 进行 Jest 测试 - 超时 - 在 jest.setTimeout 指定的 5000 毫秒超时内未调用异步回调
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53397695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jest testing with Node - Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout
提问by mokiliii Lo
I'm starting to test my code with Jest, and I can't make a seemingly simple test to pass. I am simply trying to check if what I receive from a Maogoose database request is an object.
我开始用 Jest 测试我的代码,但我无法通过一个看似简单的测试。我只是想检查我从Maogose 数据库请求中收到的内容是否是一个对象。
The function fetchPosts()is working because I hooked it up with a React frontend and it is displaying the data correctly.
该函数fetchPosts()正在运行,因为我将它与 React 前端连接在一起,并且它正确显示了数据。
This is my function fetchPosts():
这是我的功能fetchPosts():
module.exports = {
fetchPosts() {
return new Promise((resolve, reject) => {
Posts.find({}).then(posts => {
if (posts) {
resolve(posts)
} else {
reject()
}
})
})
}
}
And my test:
我的测试:
it('should get a list of posts', function() {
return posts.fetchPosts().then(result => {
expect(typeof result).toBe('object')
})
})
This makes the test fail, and Jest says
这使得测试失败,Jest 说
'Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.'
'超时 - 在 jest.setTimeout 指定的 5000 毫秒超时内未调用异步回调。'
QUESTION: How can I make this test pass?
问题:我怎样才能通过这个测试?
回答by Patrick Hund
You can expect asynchronous results using resolves, as shown in the Jest documentation.
您可以使用 期待异步结果resolves,如Jest 文档中所示。
In your case:
在你的情况下:
it('should get a list of posts', function() {
const result = posts.fetchPosts();
expect(result).resolves.toEqual(expect.any(Object));
})
…although I have a suspicion your list of posts is actually an array, so you probably want this:
...虽然我怀疑你的帖子列表实际上是一个数组,所以你可能想要这个:
it('should get a list of posts', function() {
const result = posts.fetchPosts();
expect(result).resolves.toEqual(expect.any(Array));
})
Another tip: You don't need to wrap the body of your fetchPostin an additional promise, you can simply return the promise you get from Posts.findand add a thento it, like this:
另一个提示:您不需要将您的主体包装fetchPost在额外的承诺中,您可以简单地返回您从中获得的承诺并向其Posts.find添加一个then,如下所示:
module.exports = {
fetchPosts() {
return Posts.find({}).then(posts => {
if (posts) {
return posts;
}
throw new Error('no posts'); // this will cause a promise rejection
})
}
}
回答by shmit
Also if you are simply looking to increase the timeout, then you can do that by setting
此外,如果您只是想增加超时,那么您可以通过设置来做到这一点
jest.setTimeout(10000);
You can use this statement in beforeEach if you want to change the timeout for all your tests in that describe block or in the test/it/spec block if you want it for a single test.
如果您想更改该描述块中所有测试的超时,或者如果您希望它用于单个测试,则可以在 test/it/spec 块中使用此语句。
回答by typo
It's also highly possible that you're not getting a response back from the DB at all from your test suite. Test suite's can call different environmental variables / configs that lead to different calls. This error can also be seen if no response is returned, as in - if someone blocks your IP from connecting, on and on.
您也很可能根本没有从测试套件中从数据库中得到响应。测试套件可以调用导致不同调用的不同环境变量/配置。如果没有返回响应,也可以看到此错误,例如 - 如果有人阻止您的 IP 连接,等等。

